|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Queue\Middleware; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
|
|
9
|
|
|
final class MiddlewareDispatcher |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Contains a middleware pipeline handler. |
|
13
|
|
|
* |
|
14
|
|
|
* @var MiddlewareStack|null The middleware stack. |
|
15
|
|
|
*/ |
|
16
|
|
|
private ?MiddlewareStack $stack = null; |
|
17
|
|
|
/** |
|
18
|
|
|
* @var array[]|callable[]|MiddlewareInterface[]|string[] |
|
19
|
|
|
*/ |
|
20
|
|
|
private array $middlewareDefinitions; |
|
21
|
|
|
|
|
22
|
43 |
|
public function __construct( |
|
23
|
|
|
private MiddlewareFactoryInterface $middlewareFactory, |
|
24
|
|
|
array|callable|string|MiddlewareInterface ...$middlewareDefinitions, |
|
25
|
|
|
) { |
|
26
|
43 |
|
$this->middlewareDefinitions = array_reverse($middlewareDefinitions); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Dispatch request through middleware to get response. |
|
31
|
|
|
* |
|
32
|
|
|
* @param Request $request Request to pass to middleware. |
|
33
|
|
|
* @param MessageHandlerInterface $finishHandler Handler to use in case no middleware produced response. |
|
34
|
|
|
*/ |
|
35
|
33 |
|
public function dispatch( |
|
36
|
|
|
Request $request, |
|
37
|
|
|
MessageHandlerInterface $finishHandler |
|
38
|
|
|
): Request { |
|
39
|
33 |
|
if ($this->stack === null) { |
|
40
|
33 |
|
$this->stack = new MiddlewareStack($this->buildMiddlewares(), $finishHandler); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
33 |
|
return $this->stack->handle($request); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Returns new instance with middleware handlers replaced with the ones provided. |
|
48
|
|
|
* Last specified handler will be executed first. |
|
49
|
|
|
* |
|
50
|
|
|
* @param array[]|callable[]|MiddlewareInterface[]|string[] $middlewareDefinitions Each array element is: |
|
51
|
|
|
* |
|
52
|
|
|
* - A name of a middleware class. The middleware instance will be obtained from container executed. |
|
53
|
|
|
* - A callable with `function(ServerRequestInterface $request, RequestHandlerInterface $handler): |
|
54
|
|
|
* ResponseInterface` signature. |
|
55
|
|
|
* - A "callable-like" array in format `[FooMiddleware::class, 'index']`. `FooMiddleware` instance will |
|
56
|
|
|
* be created and `index()` method will be executed. |
|
57
|
|
|
* - A function returning a middleware. The middleware returned will be executed. |
|
58
|
|
|
* |
|
59
|
|
|
* For callables typed parameters are automatically injected using dependency injection container. |
|
60
|
|
|
* |
|
61
|
|
|
* @return self New instance of the {@see PushMiddlewareDispatcher} |
|
62
|
|
|
*/ |
|
63
|
34 |
|
public function withMiddlewares(array $middlewareDefinitions): self |
|
64
|
|
|
{ |
|
65
|
34 |
|
$instance = clone $this; |
|
66
|
34 |
|
$instance->middlewareDefinitions = array_reverse($middlewareDefinitions); |
|
67
|
|
|
|
|
68
|
|
|
// Fixes a memory leak. |
|
69
|
34 |
|
unset($instance->stack); |
|
70
|
34 |
|
$instance->stack = null; |
|
71
|
|
|
|
|
72
|
34 |
|
return $instance; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return bool Whether there are middleware defined in the dispatcher. |
|
77
|
|
|
*/ |
|
78
|
4 |
|
public function hasMiddlewares(): bool |
|
79
|
|
|
{ |
|
80
|
4 |
|
return $this->middlewareDefinitions !== []; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return Closure[] |
|
85
|
|
|
*/ |
|
86
|
33 |
|
private function buildMiddlewares(): array |
|
87
|
|
|
{ |
|
88
|
33 |
|
$middlewares = []; |
|
89
|
33 |
|
$factory = $this->middlewareFactory; |
|
90
|
|
|
|
|
91
|
33 |
|
foreach ($this->middlewareDefinitions as $middlewareDefinition) { |
|
92
|
21 |
|
$middlewares[] = static fn (): MiddlewareInterface => $factory->createMiddleware( |
|
93
|
21 |
|
$middlewareDefinition |
|
94
|
21 |
|
); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
33 |
|
return $middlewares; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.