Skip to content

Commit 77d2501

Browse files
srozefabpot
authored andcommitted
[Messenger][DX] Uses the adapter name instead of the service name
1 parent f44bb19 commit 77d2501

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

DependencyInjection/MessengerPass.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public function process(ContainerBuilder $container)
6060
}
6161

6262
$this->registerReceivers($container);
63+
$this->registerSenders($container);
6364
$this->registerHandlers($container);
6465
}
6566

@@ -156,10 +157,26 @@ private function registerReceivers(ContainerBuilder $container)
156157
$receiverMapping = array();
157158
foreach ($container->findTaggedServiceIds('messenger.receiver') as $id => $tags) {
158159
foreach ($tags as $tag) {
159-
$receiverMapping[$tag['id'] ?? $id] = new Reference($id);
160+
$receiverMapping[$tag['name'] ?? $id] = new Reference($id);
160161
}
161162
}
162163

163164
$container->getDefinition('messenger.receiver_locator')->replaceArgument(0, $receiverMapping);
164165
}
166+
167+
private function registerSenders(ContainerBuilder $container)
168+
{
169+
$senderLocatorMapping = array();
170+
foreach ($container->findTaggedServiceIds('messenger.sender') as $id => $tags) {
171+
foreach ($tags as $tag) {
172+
$senderLocatorMapping[$id] = new Reference($id);
173+
174+
if ($tag['name']) {
175+
$senderLocatorMapping[$tag['name']] = new Reference($id);
176+
}
177+
}
178+
}
179+
180+
$container->getDefinition('messenger.sender_locator')->replaceArgument(0, $senderLocatorMapping);
181+
}
165182
}

Tests/DependencyInjection/MessengerPassTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Symfony\Component\DependencyInjection\ContainerBuilder;
1717
use Symfony\Component\DependencyInjection\Reference;
1818
use Symfony\Component\DependencyInjection\ServiceLocator;
19+
use Symfony\Component\Messenger\Adapter\AmqpExt\AmqpReceiver;
20+
use Symfony\Component\Messenger\Adapter\AmqpExt\AmqpSender;
1921
use Symfony\Component\Messenger\ContainerHandlerLocator;
2022
use Symfony\Component\Messenger\DependencyInjection\MessengerPass;
2123
use Symfony\Component\Messenger\Handler\ChainHandler;
@@ -88,6 +90,26 @@ public function testGetClassesFromTheHandlerSubscriberInterface()
8890
$this->assertEquals(array(new Reference(PrioritizedHandler::class), new Reference(HandlerWithMultipleMessages::class)), $definition->getArgument(0));
8991
}
9092

93+
public function testItRegistersReceivers()
94+
{
95+
$container = $this->getContainerBuilder();
96+
$container->register(AmqpReceiver::class, AmqpReceiver::class)->addTag('messenger.receiver', array('name' => 'amqp'));
97+
98+
(new MessengerPass())->process($container);
99+
100+
$this->assertEquals(array('amqp' => new Reference(AmqpReceiver::class)), $container->getDefinition('messenger.receiver_locator')->getArgument(0));
101+
}
102+
103+
public function testItRegistersSenders()
104+
{
105+
$container = $this->getContainerBuilder();
106+
$container->register(AmqpSender::class, AmqpSender::class)->addTag('messenger.sender', array('name' => 'amqp'));
107+
108+
(new MessengerPass())->process($container);
109+
110+
$this->assertEquals(array('amqp' => new Reference(AmqpSender::class), AmqpSender::class => new Reference(AmqpSender::class)), $container->getDefinition('messenger.sender_locator')->getArgument(0));
111+
}
112+
91113
/**
92114
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
93115
* @expectedExceptionMessage Invalid handler service "Symfony\Component\Messenger\Tests\DependencyInjection\UndefinedMessageHandler": message class "Symfony\Component\Messenger\Tests\DependencyInjection\UndefinedMessage" used as argument type in method "Symfony\Component\Messenger\Tests\DependencyInjection\UndefinedMessageHandler::__invoke()" does not exist.
@@ -199,6 +221,11 @@ private function getContainerBuilder(): ContainerBuilder
199221
$container->setParameter('kernel.debug', true);
200222
$container->register('message_bus', ContainerHandlerLocator::class);
201223

224+
$container
225+
->register('messenger.sender_locator', ServiceLocator::class)
226+
->addArgument(new Reference('service_container'))
227+
;
228+
202229
$container
203230
->register('messenger.handler_resolver', ContainerHandlerLocator::class)
204231
->addArgument(new Reference('service_container'))

0 commit comments

Comments
 (0)