-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathMiddlewareFactoryPush.php
More file actions
89 lines (76 loc) · 3.26 KB
/
MiddlewareFactoryPush.php
File metadata and controls
89 lines (76 loc) · 3.26 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
<?php
declare(strict_types=1);
namespace Yiisoft\Queue\Middleware\Push;
use Psr\Container\ContainerInterface;
use Yiisoft\Injector\Injector;
use Yiisoft\Queue\Message\MessageInterface;
use Yiisoft\Queue\Middleware\InvalidMiddlewareDefinitionException;
use Yiisoft\Queue\Middleware\MiddlewareFactory;
/**
* Creates a middleware based on the definition provided.
*
* @template-extends MiddlewareFactory<MiddlewarePushInterface>
*/
final class MiddlewareFactoryPush extends MiddlewareFactory implements MiddlewareFactoryPushInterface
{
/**
* @param MiddlewarePushInterface|callable|array|string $middlewareDefinition Middleware definition in one of
* the following formats:
*
* - A middleware object.
* - A name of a middleware class. The middleware instance will be obtained from container and executed.
* - A callable with `function(MessageInterface $message, MessageHandlerPushInterface $handler):
* MessageInterface` signature.
* - A controller handler action in format `[TestController::class, 'index']`. `TestController` instance will
* be created and `index()` method will be executed.
* - A function returning a middleware. The middleware returned will be executed.
*
* For handler action and callable
* typed parameters are automatically injected using dependency injection container.
* Current message and handler could be obtained by type-hinting for {@see MessageInterface}
* and {@see MessageHandlerPushInterface}.
*
* @throws InvalidMiddlewareDefinitionException
*
* @return MiddlewarePushInterface
*/
public function createPushMiddleware(
MiddlewarePushInterface|callable|array|string $middlewareDefinition,
): MiddlewarePushInterface {
if ($middlewareDefinition instanceof MiddlewarePushInterface) {
return $middlewareDefinition;
}
$middleware = $this->create($middlewareDefinition);
if (!$middleware instanceof MiddlewarePushInterface) {
throw new InvalidMiddlewareDefinitionException($middlewareDefinition);
}
return $middleware;
}
protected function getInterfaceName(): string
{
return MiddlewarePushInterface::class;
}
protected function wrapMiddleware(callable $callback): MiddlewarePushInterface
{
return new class ($callback, $this->container) implements MiddlewarePushInterface {
private $callback;
public function __construct(
callable $callback,
private readonly ContainerInterface $container,
) {
$this->callback = $callback;
}
public function processPush(MessageInterface $message, MessageHandlerPushInterface $handler): MessageInterface
{
$response = (new Injector($this->container))->invoke($this->callback, [$message, $handler]);
if ($response instanceof MessageInterface) {
return $response;
}
if ($response instanceof MiddlewarePushInterface) {
return $response->processPush($message, $handler);
}
throw new InvalidMiddlewareDefinitionException($this->callback);
}
};
}
}