|
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
|
|
|
public function __construct( |
|
40
|
|
|
#[Proxy] private readonly ContainerInterface $container, |
|
41
|
|
|
private readonly ?EventDispatcherInterface $dispatcher = null, |
|
42
|
|
|
) { |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Add middleware to the pipeline. |
|
47
|
|
|
* |
|
48
|
|
|
* @param MiddlewareInterface ...$middleware List of middleware or its definition. |
|
49
|
|
|
*/ |
|
50
|
|
|
public function withAddedMiddleware(MiddlewareInterface|Autowire|string ...$middleware): self |
|
51
|
|
|
{ |
|
52
|
|
|
$pipeline = clone $this; |
|
53
|
|
|
$pipeline->middleware = \array_merge($pipeline->middleware, $middleware); |
|
|
|
|
|
|
54
|
|
|
return $pipeline; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Replace middleware in the pipeline. |
|
59
|
|
|
* |
|
60
|
|
|
* @param MiddlewareInterface ...$middleware List of middleware or its definition. |
|
61
|
|
|
*/ |
|
62
|
|
|
public function withMiddleware(MiddlewareInterface|Autowire|string ...$middleware): self |
|
63
|
|
|
{ |
|
64
|
|
|
$pipeline = clone $this; |
|
65
|
|
|
$pipeline->middleware = $middleware; |
|
|
|
|
|
|
66
|
|
|
return $pipeline; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Configures pipeline with target endpoint. |
|
71
|
|
|
* |
|
72
|
|
|
* @throws PipelineException |
|
73
|
|
|
*/ |
|
74
|
|
|
public function withHandler(RequestHandlerInterface $handler): self |
|
75
|
|
|
{ |
|
76
|
|
|
$pipeline = clone $this; |
|
77
|
|
|
$pipeline->handler = $handler; |
|
78
|
|
|
return $pipeline; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function process(Request $request, RequestHandlerInterface $handler): Response |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->withHandler($handler)->handle($request); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function handle(Request $request): Response |
|
87
|
|
|
{ |
|
88
|
|
|
$this->handler === null and throw new PipelineException('Unable to run pipeline, no handler given.'); |
|
89
|
|
|
|
|
90
|
|
|
/** @var CurrentRequest $currentRequest */ |
|
91
|
|
|
$currentRequest = $this->container->get(CurrentRequest::class); |
|
92
|
|
|
|
|
93
|
|
|
$previousRequest = $currentRequest->get(); |
|
94
|
|
|
$currentRequest->set($request); |
|
95
|
|
|
try { |
|
96
|
|
|
// There is no middleware to process, let's pass the request to the handler |
|
97
|
|
|
if (!\array_key_exists($this->position, $this->middleware)) { |
|
98
|
|
|
return $this->handler->handle($request); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$middleware = $this->resolveMiddleware($this->position); |
|
102
|
|
|
$this->dispatcher?->dispatch(new MiddlewareProcessing($request, $middleware)); |
|
103
|
|
|
|
|
104
|
|
|
$span = $this->span; |
|
105
|
|
|
|
|
106
|
|
|
$middlewareTitle = \is_string($this->middleware[$this->position]) |
|
107
|
|
|
&& $this->middleware[$this->position] !== $middleware::class |
|
108
|
|
|
? \sprintf('%s=%s', $this->middleware[$this->position], $middleware::class) |
|
109
|
|
|
: $middleware::class; |
|
110
|
|
|
// Init a tracing span when the pipeline starts |
|
111
|
|
|
if ($span === null) { |
|
112
|
|
|
/** @var TracerInterface $tracer */ |
|
113
|
|
|
$tracer = $this->container->get(TracerInterface::class); |
|
114
|
|
|
return $tracer->trace( |
|
115
|
|
|
name: 'HTTP Pipeline', |
|
116
|
|
|
callback: function (SpanInterface $span) use ($request, $middleware, $middlewareTitle): Response { |
|
117
|
|
|
$span->setAttribute('http.middleware', [$middlewareTitle]); |
|
118
|
|
|
return $middleware->process($request, $this->next($span)); |
|
119
|
|
|
}, |
|
120
|
|
|
scoped: true, |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$middlewares = $span->getAttribute('http.middleware') ?? []; |
|
125
|
|
|
$middlewares[] = $middlewareTitle; |
|
126
|
|
|
$span->setAttribute('http.middleware', $middlewares); |
|
127
|
|
|
|
|
128
|
|
|
return $middleware->process($request, $this->next($span)); |
|
129
|
|
|
} finally { |
|
130
|
|
|
$currentRequest->set($previousRequest); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
private function next(SpanInterface $span): self |
|
135
|
|
|
{ |
|
136
|
|
|
$pipeline = clone $this; |
|
137
|
|
|
++$pipeline->position; |
|
138
|
|
|
$pipeline->span = $span; |
|
139
|
|
|
return $pipeline; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
private function resolveMiddleware(int $position): MiddlewareInterface |
|
143
|
|
|
{ |
|
144
|
|
|
$middleware = $this->middleware[$position]; |
|
145
|
|
|
return $middleware instanceof MiddlewareInterface |
|
146
|
|
|
? $middleware |
|
147
|
|
|
: $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