Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f60e409

Browse files
committed
Only subscribe to a given bus from the MessageSubscriber
1 parent ff1727e commit f60e409

File tree

2 files changed

+97
-3
lines changed

2 files changed

+97
-3
lines changed

src/Symfony/Component/Messenger/DependencyInjection/MessengerPass.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ private function registerHandlers(ContainerBuilder $container, array $busIds)
9797
$handlerBuses = (array) ($tag['bus'] ?? $busIds);
9898

9999
foreach ($handles as $messageClass => $method) {
100+
$buses = $handlerBuses;
100101
if (\is_int($messageClass)) {
101102
$messageClass = $method;
102103
$method = '__invoke';
@@ -110,8 +111,30 @@ private function registerHandlers(ContainerBuilder $container, array $busIds)
110111
}
111112

112113
if (\is_array($method)) {
113-
$messagePriority = $method[1];
114-
$method = $method[0];
114+
if (isset($method[0]) && isset($method[1])) {
115+
$messagePriority = $method[1];
116+
$method = $method[0];
117+
} elseif (isset($method['method']) || isset($method['bus'])) {
118+
if (isset($method['bus'])) {
119+
if (!\in_array($method['bus'], $busIds)) {
120+
$messageClassLocation = isset($tag['handles']) ? 'declared in your tag attribute "handles"' : $r->implementsInterface(MessageSubscriberInterface::class) ? sprintf('returned by method "%s::getHandledMessages()"', $r->getName()) : sprintf('used as argument type in method "%s::%s()"', $r->getName(), $method);
121+
122+
throw new RuntimeException(sprintf('Invalid configuration %s for message "%s": bus "%s" does not exist.', $messageClassLocation, $messageClass, $method['bus']));
123+
}
124+
125+
$buses = array($method['bus']);
126+
}
127+
128+
if (isset($method['priority'])) {
129+
$messagePriority = $method['priority'];
130+
}
131+
132+
$method = $method['method'] ?? '__invoke';
133+
} else {
134+
$messageClassLocation = isset($tag['handles']) ? 'declared in your tag attribute "handles"' : $r->implementsInterface(MessageSubscriberInterface::class) ? sprintf('returned by method "%s::getHandledMessages()"', $r->getName()) : sprintf('used as argument type in method "%s::%s()"', $r->getName(), $method);
135+
136+
throw new RuntimeException(sprintf('Invalid configuration %s for message "%s".', $messageClassLocation, $messageClass));
137+
}
115138
}
116139

117140
if (!\class_exists($messageClass)) {
@@ -132,7 +155,7 @@ private function registerHandlers(ContainerBuilder $container, array $busIds)
132155
$definitionId = $serviceId;
133156
}
134157

135-
foreach ($handlerBuses as $handlerBus) {
158+
foreach ($buses as $handlerBus) {
136159
$handlersByBusAndMessage[$handlerBus][$messageClass][$messagePriority][] = $definitionId;
137160
}
138161
}

src/Symfony/Component/Messenger/Tests/DependencyInjection/MessengerPassTest.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,48 @@ public function testItShouldNotThrowIfGeneratorIsReturnedInsteadOfArray()
297297
$this->assertEquals(array(new Reference(HandlerWithGenerators::class), 'secondMessage'), $container->getDefinition($secondReference)->getArgument(0));
298298
}
299299

300+
public function testItRegistersHandlersOnDifferentBuses()
301+
{
302+
$container = $this->getContainerBuilder($eventsBusId = 'event_bus');
303+
$container->register($commandsBusId = 'command_bus', MessageBusInterface::class)->addTag('messenger.bus')->setArgument(0, array());
304+
305+
$container
306+
->register(HandlerOnSpecificBuses::class, HandlerOnSpecificBuses::class)
307+
->addTag('messenger.message_handler')
308+
;
309+
310+
(new MessengerPass())->process($container);
311+
312+
$eventsHandlerLocatorDefinition = $container->getDefinition($container->getDefinition($eventsBusId.'.messenger.handler_resolver')->getArgument(0));
313+
$eventsHandlerMapping = $eventsHandlerLocatorDefinition->getArgument(0);
314+
315+
$this->assertEquals(array('handler.'.DummyMessage::class), array_keys($eventsHandlerMapping));
316+
$firstReference = $eventsHandlerMapping['handler.'.DummyMessage::class]->getValues()[0];
317+
$this->assertEquals(array(new Reference(HandlerOnSpecificBuses::class), 'dummyMethodForEvents'), $container->getDefinition($firstReference)->getArgument(0));
318+
319+
$commandsHandlerLocatorDefinition = $container->getDefinition($container->getDefinition($commandsBusId.'.messenger.handler_resolver')->getArgument(0));
320+
$commandsHandlerMapping = $commandsHandlerLocatorDefinition->getArgument(0);
321+
322+
$this->assertEquals(array('handler.'.DummyMessage::class), array_keys($commandsHandlerMapping));
323+
$firstReference = $commandsHandlerMapping['handler.'.DummyMessage::class]->getValues()[0];
324+
$this->assertEquals(array(new Reference(HandlerOnSpecificBuses::class), 'dummyMethodForCommands'), $container->getDefinition($firstReference)->getArgument(0));
325+
}
326+
327+
/**
328+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
329+
* @expectedExceptionMessage Invalid configuration returned by method "Symfony\Component\Messenger\Tests\DependencyInjection\HandlerOnUndefinedBus::getHandledMessages()" for message "Symfony\Component\Messenger\Tests\Fixtures\DummyMessage": bus "some_undefined_bus" does not exist.
330+
*/
331+
public function testItThrowsAnExceptionOnUnknownBus()
332+
{
333+
$container = $this->getContainerBuilder();
334+
$container
335+
->register(HandlerOnUndefinedBus::class, HandlerOnUndefinedBus::class)
336+
->addTag('messenger.message_handler')
337+
;
338+
339+
(new MessengerPass())->process($container);
340+
}
341+
300342
/**
301343
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
302344
* @expectedExceptionMessage Invalid sender "app.messenger.sender": class "Symfony\Component\Messenger\Tests\DependencyInjection\InvalidSender" must implement interface "Symfony\Component\Messenger\Transport\SenderInterface".
@@ -747,6 +789,35 @@ public function secondMessage()
747789
}
748790
}
749791

792+
class HandlerOnSpecificBuses implements MessageSubscriberInterface
793+
{
794+
public static function getHandledMessages(): iterable
795+
{
796+
yield DummyMessage::class => array('method' => 'dummyMethodForEvents', 'bus' => 'event_bus');
797+
yield DummyMessage::class => array('method' => 'dummyMethodForCommands', 'bus' => 'command_bus');
798+
}
799+
800+
public function dummyMethodForEvents()
801+
{
802+
}
803+
804+
public function dummyMethodForCommands()
805+
{
806+
}
807+
}
808+
809+
class HandlerOnUndefinedBus implements MessageSubscriberInterface
810+
{
811+
public static function getHandledMessages(): iterable
812+
{
813+
yield DummyMessage::class => array('method' => 'dummyMethodForSomeBus', 'bus' => 'some_undefined_bus');
814+
}
815+
816+
public function dummyMethodForSomeBus()
817+
{
818+
}
819+
}
820+
750821
class UselessMiddleware implements MiddlewareInterface
751822
{
752823
public function handle($message, callable $next)

0 commit comments

Comments
 (0)