Skip to content

Commit 1f8e948

Browse files
committed
minor #26909 [Messenger][DX] Uses the adapter name instead of the service name (sroze)
This PR was squashed before being merged into the 4.1-dev branch (closes #26909). Discussion ---------- [Messenger][DX] Uses the adapter name instead of the service name | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | ø | License | MIT | Doc PR | ø As a developer, I don't get why I would need to use the service name while I give a name to the adapters. This is basically what this PR means for me in terms of configuration: ```patch framework: messenger: routing: # We want the command below to be handled async with the "messenger.default_sender" (AMQP producer). - 'App\Message\Command\CreateNumber': messenger.sender.amqp + 'App\Message\Command\CreateNumber': amqp adapters: amqp: "%env(AMQP_DSN)%" ``` And in terms of pulling the messages: ```patch - bin/console messenger:consume-messages messenger.receiver.amqp + bin/console messenger:consume-messages amqp ``` Commits ------- b5afb40441 [Messenger][DX] Uses the adapter name instead of the service name
2 parents ca55458 + 77d2501 commit 1f8e948

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;
@@ -90,6 +92,26 @@ public function testGetClassesFromTheHandlerSubscriberInterface()
9092
$this->assertEquals(array(new Reference(PrioritizedHandler::class), new Reference(HandlerWithMultipleMessages::class)), $definition->getArgument(0));
9193
}
9294

95+
public function testItRegistersReceivers()
96+
{
97+
$container = $this->getContainerBuilder();
98+
$container->register(AmqpReceiver::class, AmqpReceiver::class)->addTag('messenger.receiver', array('name' => 'amqp'));
99+
100+
(new MessengerPass())->process($container);
101+
102+
$this->assertEquals(array('amqp' => new Reference(AmqpReceiver::class)), $container->getDefinition('messenger.receiver_locator')->getArgument(0));
103+
}
104+
105+
public function testItRegistersSenders()
106+
{
107+
$container = $this->getContainerBuilder();
108+
$container->register(AmqpSender::class, AmqpSender::class)->addTag('messenger.sender', array('name' => 'amqp'));
109+
110+
(new MessengerPass())->process($container);
111+
112+
$this->assertEquals(array('amqp' => new Reference(AmqpSender::class), AmqpSender::class => new Reference(AmqpSender::class)), $container->getDefinition('messenger.sender_locator')->getArgument(0));
113+
}
114+
93115
/**
94116
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
95117
* @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.
@@ -201,6 +223,11 @@ private function getContainerBuilder(): ContainerBuilder
201223
$container->setParameter('kernel.debug', true);
202224
$container->register('message_bus', ContainerHandlerLocator::class);
203225

226+
$container
227+
->register('messenger.sender_locator', ServiceLocator::class)
228+
->addArgument(new Reference('service_container'))
229+
;
230+
204231
$container
205232
->register('messenger.handler_resolver', ContainerHandlerLocator::class)
206233
->addArgument(new Reference('service_container'))

0 commit comments

Comments
 (0)