8000 Move commands-specifics to a compiler pass in FWB · symfony/symfony@407f913 · GitHub
[go: up one dir, main page]

Skip to content

Commit 407f913

Browse files
committed
Move commands-specifics to a compiler pass in FWB
1 parent bc45a0e commit 407f913

File tree

7 files changed

+166
-73
lines changed

7 files changed

+166
-73
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Reference;
18+
use Symfony\Component\Messenger\DependencyInjection\MessengerPass;
19+
20+
/**
21+
* @author Samuel Roze <samuel.roze@gmail.com>
22+
*/
23+
class MessengerCommandsPass implements CompilerPassInterface
24+
{
25+
private $busTag;
26+
private $receiverTag;
27+
28+
public function __construct(string $busTag = 'messenger.bus', string $receiverTag = 'messenger.receiver')
29+
{
30+
$this->busTag = $busTag;
31+
$this->receiverTag = $receiverTag;
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public function process(ContainerBuilder $container)
38+
{
39+
if (!$container->hasDefinition('console.command.messenger_consume_messages')) {
40+
return;
41+
}
42+
43+
$buses = array();
44+
foreach ($container->findTaggedServiceIds($this->busTag) as $busId => $tags) {
45+
$buses[$busId] = new Reference($busId);
46+
}
47+
48+
$container
49+
->getDefinition('console.command.messenger_consume_messages')
50+
->replaceArgument(0, ServiceLocatorTagPass::register($container, $buses))
51+
->replaceArgument(3, $this->findReceiverNames($container))
52+
->replaceArgument(4, array_keys($buses))
53+
;
54+
}
55+
56+
private function findReceiverNames(ContainerBuilder $container)
57+
{
58+
$receiverNames = array();
59+
foreach (MessengerPass::findReceivers($container, $this->receiverTag) as $name => $reference) {
60+
$receiverNames[(string) $reference] = $name;
61+
}
62+
63+
return array_values($receiverNames);
64+
}
65+
}

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ContainerBuilderDebugDumpPass;
2222
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\DataCollectorTranslatorPass;
2323
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\LoggingTranslatorPass;
24+
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\MessengerCommandsPass;
2425
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass;
2526
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TemplatingPass;
2627
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TestServiceContainerRealRefPass;
@@ -119,6 +120,7 @@ public function build(ContainerBuilder $container)
119120
$container->addCompilerPass(new TestServiceContainerWeakRefPass(), PassConfig::TYPE_BEFORE_REMOVING, -32);
120121
$container->addCompilerPass(new TestServiceContainerRealRefPass(), PassConfig::TYPE_AFTER_REMOVING);
121122
$this->addCompilerPassIfExists($container, MessengerPass::class);
123+
$container->addCompilerPass(new MessengerCommandsPass());
122124

123125
if ($container->getParameter('kernel.debug')) {
124126
$container->addCompilerPass(new AddDebugLogProcessorPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -32);

src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
<argument /> <!-- Message bus locator -->
7474
<argument type="service" id="messenger.receiver_locator" />
7575
<argument type="service" id="logger" on-invalid="null" />
76-
<argument>null</argument> <!-- Default receiver name -->
76+
<argument type="collection" /> <!-- Receiver names -->
7777
<argument type="collection" /> <!-- Message bus names -->
7878

7979
<tag name="console.command" command="messenger:consume-messages" />
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\MessengerCommandsPass;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Reference;
18+
use Symfony\Component\Messenger\Command\ConsumeMessagesCommand;
19+
use Symfony\Component\Messenger\MessageBusInterface;
20+
use Symfony\Component\Messenger\Tests\Fixtures\DummyReceiver;
21+
use Symfony\Component\Messenger\Transport\AmqpExt\AmqpReceiver;
22+
23+
class MessengerCommandsPassTest extends TestCase
24+
{
25+
public function testItRegistersMultipleReceiversAndSetsTheReceiverNamesOnTheCommand()
26+
{
27+
$container = new ContainerBuilder();
28+
$container->register('my_bus_name', MessageBusInterface::class)->addTag('messenger.bus')->setArgument(0, array());
29+
$container->register('console.command.messenger_consume_messages', ConsumeMessagesCommand::class)->setArguments(array(
30+
null,
31+
new Reference('messenger.receiver_locator'),
32+
null,
33+
null,
34+
null,
35+
));
36+
37+
$container->register(AmqpReceiver::class, AmqpReceiver::class)->addTag('messenger.receiver', array('alias' => 'amqp'));
38+
$container->register(DummyReceiver::class, DummyReceiver::class)->addTag('messenger.receiver', array('alias' => 'dummy'));
39+
40+
(new MessengerCommandsPass())->process($container);
41+
42+
$this->assertSame(array('amqp', 'dummy'), $container->getDefinition('console.command.messenger_consume_messages')->getArgument(3));
43+
$this->assertSame(array('my_bus_name'), $container->getDefinition('console.command.messenger_consume_messages')->getArgument(4));
44+
}
45+
}

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

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -198,17 +198,6 @@ private function registerHandlers(ContainerBuilder $container, array $busIds)
198198
}
199199
$container->getDefinition('console.command.messenger_debug')->replaceArgument(0, $debugCommandMapping);
200200
}
201-
202-
if ($container->hasDefinition('console.command.messenger_consume_messages')) {
203-
$buses = array();
204-
foreach ($busIds as $busId) {
205-
$buses[$busId] = new Reference($busId);
206-
}
207-
$container
208-
->getDefinition('console.command.messenger_consume_messages')
209-
->replaceArgument(0, ServiceLocatorTagPass::register($container, $buses))
210-
->replaceArgument(4, $busIds);
211-
}
212201
}
213202

214203
private function guessHandledClasses(\ReflectionClass $handlerClass, string $serviceId): iterable
@@ -245,30 +234,7 @@ private function guessHandledClasses(\ReflectionClass $handlerClass, string $ser
245234

246235
private function registerReceivers(ContainerBuilder $container)
247236
{
248-
$receiverMapping = array();
249-
250-
foreach ($container->findTaggedServiceIds($this->receiverTag) as $id => $tags) {
251-
$receiverClass = $container->findDefinition($id)->getClass();
252-
if (!is_subclass_of($receiverClass, ReceiverInterface::class)) {
253-
throw new RuntimeException(sprintf('Invalid receiver "%s": class "%s" must implement interface "%s".', $id, $receiverClass, ReceiverInterface::class));
254-
}
255-
256-
$receiverMapping[$id] = new Reference($id);
257-
258-
foreach ($tags as $tag) {
259-
if (isset($tag['alias'])) {
260-
$receiverMapping[$tag['alias']] = $receiverMapping[$id];
261-
}
262-
}
263-
}
264-
265-
if ($container->hasDefinition('console.command.messenger_consume_messages')) {
266-
$receiverNames = array();
267-
foreach ($receiverMapping as $name => $reference) {
268-
$receiverNames[(string) $reference] = $name;
269-
}
270-
$container->getDefinition('console.command.messenger_consume_messages')->replaceArgument(3, array_values($receiverNames));
271-
}
237+
$receiverMapping = self::findReceivers($container, $this->receiverTag);
272238

273239
$container->getDefinition('messenger.receiver_locator')->replaceArgument(0, $receiverMapping);
274240
}
@@ -337,4 +303,26 @@ private function registerBusMiddleware(ContainerBuilder $container, string $busI
337303

338304
$container->getDefinition($busId)->replaceArgument(0, $middlewareReferences);
339305
}
306+
307+
public static function findReceivers(ContainerBuilder $container, string $receiverTag)
308+
{
309+
$receiverMapping = array();
310+
311+
foreach ($container->findTaggedServiceIds($receiverTag) as $id => $tags) {
312+
$receiverClass = $container->findDefinition($id)->getClass();
313+
if (!is_subclass_of($receiverClass, ReceiverInterface::class)) {
314+
throw new RuntimeException(sprintf('Invalid receiver "%s": class "%s" must implement interface "%s".', $id, $receiverClass, ReceiverInterface::class));
315+
}
316+
317+
$receiverMapping[$id] = new Reference($id);
318+
319+
foreach ($tags as $tag) {
320+
if (isset($tag['alias'])) {
321+
$receiverMapping[$tag['alias']] = $receiverMapping[$id];
322+
}
323+
}
324+
}
325+
326+
return $receiverMapping;
327+
}
340328
}

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

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818
use Symfony\Component\DependencyInjection\ContainerBuilder;
1919
use Symfony\Component\DependencyInjection\Reference;
2020
use Symfony\Component\DependencyInjection\ServiceLocator;
21-
use Symfony\Component\Messenger\Command\ConsumeMessagesCommand;
2221
use Symfony\Component\Messenger\Command\DebugCommand;
2322
use Symfony\Component\Messenger\DataCollector\MessengerDataCollector;
2423
use Symfony\Component\Messenger\DependencyInjection\MessengerPass;
25-
use Symfony\Component\Messenger\Envelope;
2624
use Symfony\Component\Messenger\Handler\ChainHandler;
2725
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
2826
use Symfony\Component\Messenger\Handler\MessageSubscriberInterface;
@@ -35,12 +33,12 @@
3533
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
3634
use Symfony\Component\Messenger\Tests\Fixtures\DummyQuery;
3735
use Symfony\Component\Messenger\Tests\Fixtures\DummyQueryHandler;
36+
use Symfony\Component\Messenger\Tests\Fixtures\DummyReceiver;
3837
use Symfony\Component\Messenger\Tests\Fixtures\MultipleBusesMessage;
3938
use Symfony\Component\Messenger\Tests\Fixtures\MultipleBusesMessageHandler;
4039
use Symfony\Component\Messenger\Tests\Fixtures\SecondMessage;
4140
use Symfony\Component\Messenger\Transport\AmqpExt\AmqpReceiver;
4241
use Symfony\Component\Messenger\Transport\AmqpExt\AmqpSender;
43-
use Symfony\Component\Messenger\Transport\ReceiverInterface;
4442

4543
class MessengerPassTest extends TestCase
4644
{
@@ -237,26 +235,6 @@ public function testItRegistersReceiversWithoutTagName()
237235
$this->assertEquals(array(AmqpReceiver::class => new Reference(AmqpReceiver::class)), $container->getDefinition('messenger.receiver_locator')->getArgument(0));
238236
}
239237

240-
public function testItRegistersMultipleReceiversAndSetsTheReceiverNamesOnTheCommand()
241-
{
242-
$container = $this->getContainerBuilder();
243-
$container->register('console.command.messenger_consume_messages', ConsumeMessagesCommand::class)->setArguments(array(
244-
null,
245-
new Reference('messenger.receiver_locator'),
246-
null,
247-
null,
248-
null,
249-
));
250-
251-
$container->register(AmqpReceiver::class, AmqpReceiver::class)->addTag('messenger.receiver', array('alias' => 'amqp'));
252-
$container->register(DummyReceiver::class, DummyReceiver::class)->addTag('messenger.receiver', array('alias' => 'dummy'));
253-
254-
(new MessengerPass())->process($container);
255-
256-
$this->assertSame(array('amqp', 'dummy'), $container->getDefinition('console.command.messenger_consume_messages')->getArgument(3));
257-
$this->assertSame(array('message_bus'), $container->getDefinition('console.command.messenger_consume_messages')->getArgument(4));
258-
}
259-
260238
public function testItRegistersSenders()
261239
{
262240
$container = $this->getContainerBuilder();
@@ -631,20 +609,6 @@ public function __invoke(DummyMessage $message): void
631609
}
632610
}
633611

634-
class DummyReceiver implements ReceiverInterface
635-
{
636-
public function receive(callable $handler): void
637-
{
638-
for ($i = 0; $i < 3; ++$i) {
639-
$handler(Envelope::wrap(new DummyMessage("Dummy $i")));
640-
}
641-
}
642-
643-
public function stop(): void
644-
{
645-
}
646-
}
647-
648612
class InvalidReceiver
649613
{
650614
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Tests\Fixtures;
13+
14+
use Symfony\Component\Messenger\Envelope;
15+
use Symfony\Component\Messenger\Transport\ReceiverInterface;
16+
17+
class DummyReceiver implements ReceiverInterface
18+
{
19+
public function receive(callable $handler): void
20+
{
21+
for ($i = 0; $i < 3; ++$i) {
22+
$handler(Envelope::wrap(new DummyMessage("Dummy $i")));
23+
}
24+
}
25+
26+
public function stop(): void
27+
{
28+
}
29+
}

0 commit comments

Comments
 (0)
0