|
21 | 21 | use Symfony\Component\DependencyInjection\Reference;
|
22 | 22 | use Symfony\Component\Messenger\Handler\ChainHandler;
|
23 | 23 | use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
|
| 24 | +use Symfony\Component\Messenger\Handler\MessageSubscriberConfiguration; |
24 | 25 | use Symfony\Component\Messenger\Handler\MessageSubscriberInterface;
|
25 | 26 | use Symfony\Component\Messenger\TraceableMessageBus;
|
26 | 27 | use Symfony\Component\Messenger\Handler\MethodOnObjectHandler;
|
@@ -79,43 +80,28 @@ private function registerHandlers(ContainerBuilder $container)
|
79 | 80 | foreach ($container->findTaggedServiceIds($this->handlerTag, true) as $serviceId => $tags) {
|
80 | 81 | foreach ($tags as $tag) {
|
81 | 82 | $r = $container->getReflectionClass($container->getDefinition($serviceId)->getClass());
|
| 83 | + $handles = isset($tag['handles']) ? array($tag['handles']) : $this->guessHandledClasses($r, $serviceId); |
82 | 84 |
|
83 |
| - if (isset($tag['handles'])) { |
84 |
| - $handles = isset($tag['method']) ? array($tag['handles'] => $tag['method']) : array($tag['handles']); |
85 |
| - } else { |
86 |
| - $handles = $this->guessHandledClasses($r, $serviceId); |
87 |
| - } |
88 |
| - |
89 |
| - $priority = $tag['priority'] ?? 0; |
90 |
| - |
91 |
| - foreach ($handles as $messageClass => $method) { |
92 |
| - if (\is_int($messageClass)) { |
93 |
| - $messageClass = $method; |
94 |
| - $method = '__invoke'; |
95 |
| - } |
96 |
| - |
97 |
| - if (\is_array($messageClass)) { |
98 |
| - $messagePriority = $messageClass[1]; |
99 |
| - $messageClass = $messageClass[0]; |
100 |
| - } else { |
101 |
| - $messagePriority = $priority; |
| 85 | + foreach ($handles as $messageSubscriberConfiguration) { |
| 86 | + if (\is_string($messageSubscriberConfiguration)) { |
| 87 | + $messageSubscriberConfiguration = new MessageSubscriberConfiguration($messageSubscriberConfiguration); |
102 | 88 | }
|
103 | 89 |
|
104 |
| - if (\is_array($method)) { |
105 |
| - $messagePriority = $method[1]; |
106 |
| - $method = $method[0]; |
| 90 | + if (!$messageSubscriberConfiguration instanceof MessageSubscriberConfiguration) { |
| 91 | + throw new RuntimeException(sprintf('Invalid handler service "%s": method "%s:%s()" must return strings or "MessageSubscriberConfiguration" objects.', $serviceId, $r->getName(), 'getHandledMessages')); |
107 | 92 | }
|
108 | 93 |
|
109 |
| - if (!\class_exists($messageClass)) { |
110 |
| - $messageClassLocation = isset($tag['handles']) ? 'declared in your tag attribute "handles"' : $r->implementsInterface(MessageHandlerInterface::class) ? sprintf('returned by method "%s::getHandledMessages()"', $r->getName()) : sprintf('used as argument type in method "%s::%s()"', $r->getName(), $method); |
| 94 | + if (!\class_exists($messageClass = $messageSubscriberConfiguration->getMessageClass())) { |
| 95 | + $messageClassLocation = isset($tag['handles']) ? 'declared in your tag attribute "handles"' : $r->implementsInterface(MessageHandlerInterface::class) ? sprintf('returned by method "%s::getHandledMessages()"', $r->getName()) : sprintf('used as argument type in method "%s::%s()"', $r->getName(), $messageSubscriberConfiguration->getMethod()); |
111 | 96 |
|
112 | 97 | throw new RuntimeException(sprintf('Invalid handler service "%s": message class "%s" %s does not exist.', $serviceId, $messageClass, $messageClassLocation));
|
113 | 98 | }
|
114 | 99 |
|
115 |
| - if (!$r->hasMethod($method)) { |
| 100 | + if (!$r->hasMethod($method = $messageSubscriberConfiguration->getMethod())) { |
116 | 101 | throw new RuntimeException(sprintf('Invalid handler service "%s": method "%s::%s()" does not exist.', $serviceId, $r->getName(), $method));
|
117 | 102 | }
|
118 | 103 |
|
| 104 | + $messagePriority = $messageSubscriberConfiguration->getPriority(); |
119 | 105 | if ('__invoke' !== $method) {
|
120 | 106 | $wrapperDefinition = (new Definition(MethodOnObjectHandler::class))->setArguments(array(new Reference($serviceId), $method));
|
121 | 107 |
|
|
0 commit comments