1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Http; |
6
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
9
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
10
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
11
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
12
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
13
|
|
|
use Spiral\Core\Attribute\Proxy; |
14
|
|
|
use Spiral\Core\Container\Autowire; |
15
|
|
|
use Spiral\Http\Event\MiddlewareProcessing; |
16
|
|
|
use Spiral\Http\Exception\PipelineException; |
17
|
|
|
use Spiral\Telemetry\SpanInterface; |
18
|
|
|
use Spiral\Telemetry\TracerInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Pipeline used to pass request and response thought the chain of middleware. |
22
|
|
|
* This kind of pipeline creates middleware on the fly. |
23
|
|
|
*/ |
24
|
|
|
final class LazyPipeline implements RequestHandlerInterface, MiddlewareInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Set of middleware to be applied for every request. |
28
|
|
|
* |
29
|
|
|
* @var list<MiddlewareInterface|Autowire|string> |
|
|
|
|
30
|
|
|
*/ |
31
|
|
|
protected array $middleware = []; |
32
|
|
|
private ?RequestHandlerInterface $handler = null; |
33
|
|
|
private int $position = 0; |
34
|
|
|
/** |
35
|
|
|
* Trace span for the current pipeline run. |
36
|
|
|
*/ |
37
|
|
|
private ?SpanInterface $span = null; |
38
|
|
|
|
39
|
438 |
|
public function __construct( |
40
|
|
|
#[Proxy] private readonly ContainerInterface $container, |
41
|
|
|
private readonly ?EventDispatcherInterface $dispatcher = null, |
42
|
|
|
) { |
43
|
438 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Add middleware to the pipeline. |
47
|
|
|
* |
48
|
|
|
* @param MiddlewareInterface ...$middleware List of middleware or its definition. |
49
|
|
|
*/ |
50
|
49 |
|
public function withAddedMiddleware(MiddlewareInterface|Autowire|string ...$middleware): self |
51
|
|
|
{ |
52
|
49 |
|
$pipeline = clone $this; |
53
|
49 |
|
$pipeline->middleware = \array_merge($pipeline->middleware, $middleware); |
|
|
|
|
54
|
49 |
|
return $pipeline; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Replace middleware in the pipeline. |
59
|
|
|
* |
60
|
|
|
* @param MiddlewareInterface ...$middleware List of middleware or its definition. |
61
|
|
|
*/ |
62
|
436 |
|
public function withMiddleware(MiddlewareInterface|Autowire|string ...$middleware): self |
63
|
|
|
{ |
64
|
436 |
|
$pipeline = clone $this; |
65
|
436 |
|
$pipeline->middleware = $middleware; |
|
|
|
|
66
|
436 |
|
return $pipeline; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Configures pipeline with target endpoint. |
71
|
|
|
* |
72
|
|
|
* @throws PipelineException |
73
|
|
|
*/ |
74
|
72 |
|
public function withHandler(RequestHandlerInterface $handler): self |
75
|
|
|
{ |
76
|
72 |
|
$pipeline = clone $this; |
77
|
72 |
|
$pipeline->handler = $handler; |
78
|
72 |
|
return $pipeline; |
79
|
|
|
} |
80
|
|
|
|
81
|
54 |
|
public function process(Request $request, RequestHandlerInterface $handler): Response |
82
|
|
|
{ |
83
|
54 |
|
return $this->withHandler($handler)->handle($request); |
84
|
|
|
} |
85
|
|
|
|
86
|
72 |
|
public function handle(Request $request): Response |
87
|
|
|
{ |
88
|
72 |
|
$this->handler === null and throw new PipelineException('Unable to run pipeline, no handler given.'); |
89
|
|
|
|
90
|
|
|
/** @var CurrentRequest $currentRequest */ |
91
|
72 |
|
$currentRequest = $this->container->get(CurrentRequest::class); |
92
|
|
|
|
93
|
72 |
|
$previousRequest = $currentRequest->get(); |
94
|
72 |
|
$currentRequest->set($request); |
95
|
|
|
try { |
96
|
|
|
// There is no middleware to process, let's pass the request to the handler |
97
|
72 |
|
if (!\array_key_exists($this->position, $this->middleware)) { |
98
|
70 |
|
return $this->handler->handle($request); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
45 |
|
$middleware = $this->resolveMiddleware($this->position); |
102
|
45 |
|
$this->dispatcher?->dispatch(new MiddlewareProcessing($request, $middleware)); |
103
|
|
|
|
104
|
45 |
|
$span = $this->span; |
105
|
|
|
|
106
|
45 |
|
$middlewareTitle = \is_string($this->middleware[$this->position]) |
107
|
45 |
|
&& $this->middleware[$this->position] !== $middleware::class |
108
|
12 |
|
? \sprintf('%s=%s', $this->middleware[$this->position], $middleware::class) |
109
|
45 |
|
: $middleware::class; |
110
|
|
|
// Init a tracing span when the pipeline starts |
111
|
45 |
|
if ($span === null) { |
112
|
|
|
/** @var TracerInterface $tracer */ |
113
|
45 |
|
$tracer = $this->container->get(TracerInterface::class); |
114
|
45 |
|
return $tracer->trace( |
115
|
45 |
|
name: 'HTTP Pipeline', |
116
|
45 |
|
callback: function (SpanInterface $span) use ($request, $middleware, $middlewareTitle): Response { |
117
|
45 |
|
$span->setAttribute('http.middleware', [$middlewareTitle]); |
118
|
45 |
|
return $middleware->process($request, $this->next($span)); |
119
|
45 |
|
}, |
120
|
45 |
|
scoped: true, |
121
|
45 |
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
42 |
|
$middlewares = $span->getAttribute('http.middleware') ?? []; |
125
|
42 |
|
$middlewares[] = $middlewareTitle; |
126
|
42 |
|
$span->setAttribute('http.middleware', $middlewares); |
127
|
|
|
|
128
|
42 |
|
return $middleware->process($request, $this->next($span)); |
129
|
|
|
} finally { |
130
|
72 |
|
$currentRequest->set($previousRequest); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
45 |
|
private function next(SpanInterface $span): self |
135
|
|
|
{ |
136
|
45 |
|
$pipeline = clone $this; |
137
|
45 |
|
++$pipeline->position; |
138
|
45 |
|
$pipeline->span = $span; |
139
|
45 |
|
return $pipeline; |
140
|
|
|
} |
141
|
|
|
|
142
|
45 |
|
private function resolveMiddleware(int $position): MiddlewareInterface |
143
|
|
|
{ |
144
|
45 |
|
$middleware = $this->middleware[$position]; |
145
|
45 |
|
return $middleware instanceof MiddlewareInterface |
146
|
3 |
|
? $middleware |
147
|
45 |
|
: $this->container->get($middleware); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths