Skip to content

Commit e9cf083

Browse files
committed
Move commands-specifics to a compiler pass in FWB
1 parent f62dc69 commit e9cf083

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Reference;
17+
use Symfony\Component\Messenger\DependencyInjection\MessengerPass;
18+
19+
/**
20+
* @author Samuel Roze <samuel.roze@gmail.com>
21+
*/
22+
class MessengerCommandsPass implements CompilerPassInterface
23+
{
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
public function process(ContainerBuilder $container)
28+
{
29+
if (!$container->hasDefinition('console.command.messenger_consume_messages')) {
30+
return;
31+
}
32+
33+
$buses = array();
34+
foreach ($container->findTaggedServiceIds('messenger.bus') as $busId => $tags) {
35+
$buses[$busId] = new Reference($busId);
36+
}
37+
38+
$container
39+
->getDefinition('console.command.messenger_consume_messages')
40+
->replaceArgument(3, $this->findReceiverNames($container))
41+
;
42+
}
43+
44+
private function findReceiverNames(ContainerBuilder $container)
45+
{
46+
$receiverNames = array();
47+
foreach (MessengerPass::findReceivers($container, 'messenger.receiver') as $name => $reference) {
48+
$receiverNames[(string) $reference] = $name;
49+
}
50+
51+
return array_values($receiverNames);
52+
}
53+
}

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);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
}
44+
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"symfony/asset": "<3.4",
6868
"symfony/console": "<3.4",
6969
"symfony/form": "<4.1",
70+
"symfony/messenger": "<4.1.5",
7071
"symfony/property-info": "<3.4",
7172
"symfony/serializer": "<4.1",
7273
"symfony/stopwatch": "<3.4",

0 commit comments

Comments
 (0)