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