|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace TMV\Laminas\Messenger\Factory; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
|
8
|
|
|
use Symfony\Component\Messenger\MessageBus; |
|
9
|
|
|
use Symfony\Component\Messenger\MessageBusInterface; |
|
10
|
|
|
use Symfony\Component\Messenger\Middleware\AddBusNameStampMiddleware; |
|
11
|
|
|
use Symfony\Component\Messenger\Middleware\DispatchAfterCurrentBusMiddleware; |
|
12
|
|
|
use Symfony\Component\Messenger\Middleware\FailedMessageProcessingMiddleware; |
|
13
|
|
|
use Symfony\Component\Messenger\Middleware\MiddlewareInterface; |
|
14
|
|
|
use Symfony\Component\Messenger\Middleware\RejectRedeliveredMessageMiddleware; |
|
15
|
|
|
use TMV\Laminas\Messenger\Exception\InvalidArgumentException; |
|
16
|
|
|
use TMV\Laminas\Messenger\Factory\Middleware\HandleMessageMiddlewareFactory; |
|
17
|
|
|
use TMV\Laminas\Messenger\Factory\Middleware\SendMessageMiddlewareFactory; |
|
18
|
|
|
|
|
19
|
|
|
use function array_key_exists; |
|
20
|
|
|
use function array_map; |
|
21
|
|
|
use function array_merge; |
|
22
|
|
|
use function sprintf; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @psalm-api |
|
26
|
|
|
*/ |
|
27
|
|
|
final class MessageBusFactory |
|
28
|
2 |
|
{ |
|
29
|
|
|
/** @var string */ |
|
30
|
2 |
|
private $busName; |
|
31
|
2 |
|
|
|
32
|
|
|
public function __construct(string $busName = 'messenger.bus.default') |
|
33
|
2 |
|
{ |
|
34
|
|
|
$this->busName = $busName; |
|
35
|
|
|
} |
|
36
|
2 |
|
|
|
37
|
|
|
public function __invoke(ContainerInterface $container): MessageBusInterface |
|
38
|
2 |
|
{ |
|
39
|
|
|
/** @var array<string, mixed> $busConfig */ |
|
40
|
|
|
$busConfig = $container->get('config')['messenger']['buses'][$this->busName] ?? []; |
|
41
|
2 |
|
|
|
42
|
|
|
$includeDefaults = (bool) ($busConfig['default_middleware'] ?? true); |
|
43
|
|
|
|
|
44
|
2 |
|
/** @var string[] $middleware */ |
|
45
|
|
|
$middleware = $busConfig['middleware'] ?? []; |
|
46
|
2 |
|
|
|
47
|
1 |
|
/** @var MiddlewareInterface[] $middleware */ |
|
48
|
|
|
$middleware = array_map([$container, 'get'], $middleware); |
|
49
|
1 |
|
|
|
50
|
1 |
|
if ($includeDefaults) { |
|
51
|
1 |
|
$middleware = array_merge( |
|
52
|
1 |
|
[ |
|
53
|
|
|
new AddBusNameStampMiddleware($this->busName), |
|
54
|
1 |
|
new RejectRedeliveredMessageMiddleware(), |
|
55
|
|
|
new DispatchAfterCurrentBusMiddleware(), |
|
56
|
1 |
|
new FailedMessageProcessingMiddleware(), |
|
57
|
1 |
|
], |
|
58
|
|
|
$middleware, |
|
59
|
|
|
[ |
|
60
|
|
|
(new SendMessageMiddlewareFactory($this->busName))($container), |
|
61
|
|
|
(new HandleMessageMiddlewareFactory($this->busName))($container), |
|
62
|
2 |
|
] |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return new MessageBus($middleware); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @psalm-api |
|
71
|
3 |
|
* |
|
72
|
|
|
* @param array<int, mixed> $arguments |
|
73
|
3 |
|
*/ |
|
74
|
1 |
|
public static function __callStatic(string $name, array $arguments): MessageBusInterface |
|
75
|
1 |
|
{ |
|
76
|
1 |
|
if (! array_key_exists(0, $arguments) || ! $arguments[0] instanceof ContainerInterface) { |
|
77
|
|
|
throw new InvalidArgumentException(sprintf( |
|
78
|
|
|
'The first argument must be of type %s', |
|
79
|
|
|
ContainerInterface::class |
|
80
|
2 |
|
)); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return (new static($name))($arguments[0]); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|