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