diff --git a/DependencyInjection/MessengerPass.php b/DependencyInjection/MessengerPass.php index 03f48edf..032ec76e 100644 --- a/DependencyInjection/MessengerPass.php +++ b/DependencyInjection/MessengerPass.php @@ -106,6 +106,7 @@ private function registerHandlers(ContainerBuilder $container, array $busIds): v unset($options['handles']); $priority = $options['priority'] ?? 0; $method = $options['method'] ?? '__invoke'; + $fromTransport = $options['from_transport'] ?? ''; if (isset($options['bus'])) { if (!\in_array($options['bus'], $busIds)) { @@ -131,10 +132,10 @@ private function registerHandlers(ContainerBuilder $container, array $busIds): v throw new RuntimeException(sprintf('Invalid handler service "%s": method "%s::%s()" does not exist.', $serviceId, $r->getName(), $method)); } - if ('__invoke' !== $method) { + if ('__invoke' !== $method || '' !== $fromTransport) { $wrapperDefinition = (new Definition('Closure'))->addArgument([new Reference($serviceId), $method])->setFactory('Closure::fromCallable'); - $definitions[$definitionId = '.messenger.method_on_object_wrapper.'.ContainerBuilder::hash($message.':'.$priority.':'.$serviceId.':'.$method)] = $wrapperDefinition; + $definitions[$definitionId = '.messenger.method_on_object_wrapper.'.ContainerBuilder::hash($message.':'.$priority.':'.$serviceId.':'.$method.':'.$fromTransport)] = $wrapperDefinition; } else { $definitionId = $serviceId; } diff --git a/Tests/DependencyInjection/MessengerPassTest.php b/Tests/DependencyInjection/MessengerPassTest.php index 226c8d71..13d18993 100644 --- a/Tests/DependencyInjection/MessengerPassTest.php +++ b/Tests/DependencyInjection/MessengerPassTest.php @@ -52,6 +52,7 @@ use Symfony\Component\Messenger\Tests\Fixtures\SecondMessage; use Symfony\Component\Messenger\Tests\Fixtures\TaggedDummyHandler; use Symfony\Component\Messenger\Tests\Fixtures\TaggedDummyHandlerWithUnionTypes; +use Symfony\Component\Messenger\Tests\Fixtures\ThirdMessage; use Symfony\Component\Messenger\Tests\Fixtures\UnionBuiltinTypeArgumentHandler; use Symfony\Component\Messenger\Tests\Fixtures\UnionTypeArgumentHandler; use Symfony\Component\Messenger\Tests\Fixtures\UnionTypeOneMessage; @@ -102,7 +103,7 @@ public function testFromTransportViaTagAttribute() $container = $this->getContainerBuilder($busId = 'message_bus'); $container ->register(DummyHandler::class, DummyHandler::class) - ->addTag('messenger.message_handler', ['from_transport' => 'async']) + ->addTag('messenger.message_handler', ['from_transport' => 'async', 'method' => '__invoke']) ; (new MessengerPass())->process($container); @@ -113,7 +114,7 @@ public function testFromTransportViaTagAttribute() $handlerDescriptionMapping = $handlersLocatorDefinition->getArgument(0); $this->assertCount(1, $handlerDescriptionMapping); - $this->assertHandlerDescriptor($container, $handlerDescriptionMapping, DummyMessage::class, [DummyHandler::class], [['from_transport' => 'async']]); + $this->assertHandlerDescriptor($container, $handlerDescriptionMapping, DummyMessage::class, [[DummyHandler::class, '__invoke']], [['from_transport' => 'async']]); } public function testHandledMessageTypeResolvedWithMethodAndNoHandlesViaTagAttributes() @@ -178,7 +179,7 @@ public function testTaggedMessageHandler() $this->assertSame(HandlersLocator::class, $handlersLocatorDefinition->getClass()); $handlerDescriptionMapping = $handlersLocatorDefinition->getArgument(0); - $this->assertCount(2, $handlerDescriptionMapping); + $this->assertCount(3, $handlerDescriptionMapping); $this->assertHandlerDescriptor($container, $handlerDescriptionMapping, DummyMessage::class, [TaggedDummyHandler::class], [[]]); $this->assertHandlerDescriptor( @@ -187,6 +188,19 @@ public function testTaggedMessageHandler() SecondMessage::class, [[TaggedDummyHandler::class, 'handleSecondMessage']] ); + $this->assertHandlerDescriptor( + $container, + $handlerDescriptionMapping, + ThirdMessage::class, + [ + [TaggedDummyHandler::class, 'handleThirdMessage'], + [TaggedDummyHandler::class, 'handleThirdMessage'], + ], + [ + ['from_transport' => 'a'], + ['from_transport' => 'b'], + ], + ); } public function testTaggedMessageHandlerWithUnionTypes() diff --git a/Tests/Fixtures/TaggedDummyHandler.php b/Tests/Fixtures/TaggedDummyHandler.php index cecd6f2e..794286b2 100644 --- a/Tests/Fixtures/TaggedDummyHandler.php +++ b/Tests/Fixtures/TaggedDummyHandler.php @@ -15,4 +15,10 @@ public function __invoke(DummyMessage $message) public function handleSecondMessage(SecondMessage $message) { } + + #[AsMessageHandler(fromTransport: 'a')] + #[AsMessageHandler(fromTransport: 'b')] + public function handleThirdMessage(ThirdMessage $message): void + { + } } diff --git a/Tests/Fixtures/ThirdMessage.php b/Tests/Fixtures/ThirdMessage.php new file mode 100644 index 00000000..b40e7a9c --- /dev/null +++ b/Tests/Fixtures/ThirdMessage.php @@ -0,0 +1,7 @@ +