@@ -27,97 +27,97 @@ |
||
27 | 27 | */ |
28 | 28 | class HandlerProviderPass implements CompilerPassInterface |
29 | 29 | { |
30 | - public const DEFAULT_SERVICE_ID = HandlerProvider::class; |
|
31 | - public const DEFAULT_HANDLER_TAG = 'message_dispatcher.handler'; |
|
32 | - public const DEFAULT_MESSAGE_PROPERTY = 'message'; |
|
33 | - public const DEFAULT_PROVIDER_CLASS = ContainerHandlerProvider::class; |
|
34 | - |
|
35 | - /** |
|
36 | - * @var string |
|
37 | - */ |
|
38 | - private $serviceId; |
|
39 | - |
|
40 | - /** |
|
41 | - * @var string |
|
42 | - */ |
|
43 | - private $handlerTag; |
|
44 | - |
|
45 | - /** |
|
46 | - * @var string |
|
47 | - */ |
|
48 | - private $messageProperty; |
|
49 | - |
|
50 | - /** |
|
51 | - * @var string |
|
52 | - */ |
|
53 | - private $providerClass; |
|
54 | - |
|
55 | - public function __construct( |
|
56 | - string $serviceId = self::DEFAULT_SERVICE_ID, |
|
57 | - string $handlerTag = self::DEFAULT_HANDLER_TAG, |
|
58 | - string $messageProperty = self::DEFAULT_MESSAGE_PROPERTY, |
|
59 | - string $providerClass = self::DEFAULT_PROVIDER_CLASS |
|
60 | - ) { |
|
61 | - $this->serviceId = $serviceId; |
|
62 | - $this->handlerTag = $handlerTag; |
|
63 | - $this->messageProperty = $messageProperty; |
|
64 | - $this->providerClass = $providerClass; |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * @inheritdoc |
|
69 | - */ |
|
70 | - public function process(ContainerBuilder $container): void |
|
71 | - { |
|
72 | - [ $mapping, $refMap ] = $this->collectHandlers($container); |
|
73 | - |
|
74 | - $container |
|
75 | - ->register($this->serviceId, $this->providerClass) |
|
76 | - ->setArguments([ |
|
77 | - ServiceLocatorTagPass::register($container, $refMap), |
|
78 | - $mapping |
|
79 | - ]); |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * @return array{0: array<string, string>, 1: array<string, TypedReference>} |
|
84 | - */ |
|
85 | - private function collectHandlers(ContainerBuilder $container): array |
|
86 | - { |
|
87 | - $handlers = $container->findTaggedServiceIds($this->handlerTag, true); |
|
88 | - $messageProperty = $this->messageProperty; |
|
89 | - $mapping = []; |
|
90 | - $refMap = []; |
|
91 | - |
|
92 | - foreach ($handlers as $id => $tags) { |
|
93 | - assert(is_string($id)); |
|
94 | - |
|
95 | - $command = $tags[0][$messageProperty] ?? null; |
|
96 | - |
|
97 | - if (!$command) { |
|
98 | - throw new InvalidArgumentException( |
|
99 | - "The `$messageProperty` property is required for service `$id`." |
|
100 | - ); |
|
101 | - } |
|
102 | - |
|
103 | - assert(is_string($command)); |
|
104 | - |
|
105 | - if (isset($mapping[$command])) { |
|
106 | - throw new LogicException( |
|
107 | - "The command `$command` already has an handler: `{$mapping[$command]}`." |
|
108 | - ); |
|
109 | - } |
|
110 | - |
|
111 | - $class = $container->getDefinition($id)->getClass(); |
|
112 | - |
|
113 | - if (!$class) { |
|
114 | - throw new LogicException("Unable to get class of service `$id`."); |
|
115 | - } |
|
116 | - |
|
117 | - $mapping[$command] = $id; |
|
118 | - $refMap[$id] = new TypedReference($id, $class); |
|
119 | - } |
|
120 | - |
|
121 | - return [ $mapping, $refMap ]; |
|
122 | - } |
|
30 | + public const DEFAULT_SERVICE_ID = HandlerProvider::class; |
|
31 | + public const DEFAULT_HANDLER_TAG = 'message_dispatcher.handler'; |
|
32 | + public const DEFAULT_MESSAGE_PROPERTY = 'message'; |
|
33 | + public const DEFAULT_PROVIDER_CLASS = ContainerHandlerProvider::class; |
|
34 | + |
|
35 | + /** |
|
36 | + * @var string |
|
37 | + */ |
|
38 | + private $serviceId; |
|
39 | + |
|
40 | + /** |
|
41 | + * @var string |
|
42 | + */ |
|
43 | + private $handlerTag; |
|
44 | + |
|
45 | + /** |
|
46 | + * @var string |
|
47 | + */ |
|
48 | + private $messageProperty; |
|
49 | + |
|
50 | + /** |
|
51 | + * @var string |
|
52 | + */ |
|
53 | + private $providerClass; |
|
54 | + |
|
55 | + public function __construct( |
|
56 | + string $serviceId = self::DEFAULT_SERVICE_ID, |
|
57 | + string $handlerTag = self::DEFAULT_HANDLER_TAG, |
|
58 | + string $messageProperty = self::DEFAULT_MESSAGE_PROPERTY, |
|
59 | + string $providerClass = self::DEFAULT_PROVIDER_CLASS |
|
60 | + ) { |
|
61 | + $this->serviceId = $serviceId; |
|
62 | + $this->handlerTag = $handlerTag; |
|
63 | + $this->messageProperty = $messageProperty; |
|
64 | + $this->providerClass = $providerClass; |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * @inheritdoc |
|
69 | + */ |
|
70 | + public function process(ContainerBuilder $container): void |
|
71 | + { |
|
72 | + [ $mapping, $refMap ] = $this->collectHandlers($container); |
|
73 | + |
|
74 | + $container |
|
75 | + ->register($this->serviceId, $this->providerClass) |
|
76 | + ->setArguments([ |
|
77 | + ServiceLocatorTagPass::register($container, $refMap), |
|
78 | + $mapping |
|
79 | + ]); |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * @return array{0: array<string, string>, 1: array<string, TypedReference>} |
|
84 | + */ |
|
85 | + private function collectHandlers(ContainerBuilder $container): array |
|
86 | + { |
|
87 | + $handlers = $container->findTaggedServiceIds($this->handlerTag, true); |
|
88 | + $messageProperty = $this->messageProperty; |
|
89 | + $mapping = []; |
|
90 | + $refMap = []; |
|
91 | + |
|
92 | + foreach ($handlers as $id => $tags) { |
|
93 | + assert(is_string($id)); |
|
94 | + |
|
95 | + $command = $tags[0][$messageProperty] ?? null; |
|
96 | + |
|
97 | + if (!$command) { |
|
98 | + throw new InvalidArgumentException( |
|
99 | + "The `$messageProperty` property is required for service `$id`." |
|
100 | + ); |
|
101 | + } |
|
102 | + |
|
103 | + assert(is_string($command)); |
|
104 | + |
|
105 | + if (isset($mapping[$command])) { |
|
106 | + throw new LogicException( |
|
107 | + "The command `$command` already has an handler: `{$mapping[$command]}`." |
|
108 | + ); |
|
109 | + } |
|
110 | + |
|
111 | + $class = $container->getDefinition($id)->getClass(); |
|
112 | + |
|
113 | + if (!$class) { |
|
114 | + throw new LogicException("Unable to get class of service `$id`."); |
|
115 | + } |
|
116 | + |
|
117 | + $mapping[$command] = $id; |
|
118 | + $refMap[$id] = new TypedReference($id, $class); |
|
119 | + } |
|
120 | + |
|
121 | + return [ $mapping, $refMap ]; |
|
122 | + } |
|
123 | 123 | } |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | */ |
70 | 70 | public function process(ContainerBuilder $container): void |
71 | 71 | { |
72 | - [ $mapping, $refMap ] = $this->collectHandlers($container); |
|
72 | + [$mapping, $refMap] = $this->collectHandlers($container); |
|
73 | 73 | |
74 | 74 | $container |
75 | 75 | ->register($this->serviceId, $this->providerClass) |
@@ -118,6 +118,6 @@ discard block |
||
118 | 118 | $refMap[$id] = new TypedReference($id, $class); |
119 | 119 | } |
120 | 120 | |
121 | - return [ $mapping, $refMap ]; |
|
121 | + return [$mapping, $refMap]; |
|
122 | 122 | } |
123 | 123 | } |
@@ -20,40 +20,40 @@ |
||
20 | 20 | |
21 | 21 | class ContainerHandlerProvider implements HandlerProvider |
22 | 22 | { |
23 | - /** |
|
24 | - * @var ContainerInterface |
|
25 | - */ |
|
26 | - private $container; |
|
27 | - |
|
28 | - /** |
|
29 | - * @var array<string, string> |
|
30 | - */ |
|
31 | - private $handlers; |
|
32 | - |
|
33 | - /** |
|
34 | - * @param array<string, string> $mapping |
|
35 | - * An array of key/value pairs where _key_ is a message class and _value_ the service |
|
36 | - * identifier of its handler. |
|
37 | - */ |
|
38 | - public function __construct(ContainerInterface $container, array $mapping) |
|
39 | - { |
|
40 | - $this->handlers = $mapping; |
|
41 | - $this->container = $container; |
|
42 | - } |
|
43 | - |
|
44 | - public function getHandlerForMessage(object $message): callable |
|
45 | - { |
|
46 | - $class = get_class($message); |
|
47 | - $id = $this->handlers[$class] ?? null; |
|
48 | - |
|
49 | - if (!$id) { |
|
50 | - throw new NotFound("No handler for messages of type `$class`."); |
|
51 | - } |
|
52 | - |
|
53 | - try { |
|
54 | - return $this->container->get($id); // @phpstan-ignore-line |
|
55 | - } catch (NotFoundExceptionInterface $e) { |
|
56 | - throw new NotFound("No handler for messages of type `$class`.", $e); |
|
57 | - } |
|
58 | - } |
|
23 | + /** |
|
24 | + * @var ContainerInterface |
|
25 | + */ |
|
26 | + private $container; |
|
27 | + |
|
28 | + /** |
|
29 | + * @var array<string, string> |
|
30 | + */ |
|
31 | + private $handlers; |
|
32 | + |
|
33 | + /** |
|
34 | + * @param array<string, string> $mapping |
|
35 | + * An array of key/value pairs where _key_ is a message class and _value_ the service |
|
36 | + * identifier of its handler. |
|
37 | + */ |
|
38 | + public function __construct(ContainerInterface $container, array $mapping) |
|
39 | + { |
|
40 | + $this->handlers = $mapping; |
|
41 | + $this->container = $container; |
|
42 | + } |
|
43 | + |
|
44 | + public function getHandlerForMessage(object $message): callable |
|
45 | + { |
|
46 | + $class = get_class($message); |
|
47 | + $id = $this->handlers[$class] ?? null; |
|
48 | + |
|
49 | + if (!$id) { |
|
50 | + throw new NotFound("No handler for messages of type `$class`."); |
|
51 | + } |
|
52 | + |
|
53 | + try { |
|
54 | + return $this->container->get($id); // @phpstan-ignore-line |
|
55 | + } catch (NotFoundExceptionInterface $e) { |
|
56 | + throw new NotFound("No handler for messages of type `$class`.", $e); |
|
57 | + } |
|
58 | + } |
|
59 | 59 | } |