Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 37272af

Browse files
committed
Move commands-specifics to a compiler pass in FWB
1 parent 30de3b8 commit 37272af

File tree

6 files changed

+162
-59
lines changed

6 files changed

+162
-59
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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 <[email protected]>
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(3, $this->findReceiverNames($container))
51+
;
52+
}
53+
54+
private function findReceiverNames(ContainerBuilder $container)
55+
{
56+
$receiverNames = array();
57+
foreach (MessengerPass::findReceivers($container, $this->receiverTag) as $name => $reference) {
58+
$receiverNames[(string) $reference] = $name;
59+
}
60+
61+
return array_values($receiverNames);
62+
}
63+
}

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);
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 <[email protected]>
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+
}

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

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -220,30 +220,7 @@ private function guessHandledClasses(\ReflectionClass $handlerClass, string $ser
220220

221221
private function registerReceivers(ContainerBuilder $container)
222222
{
223-
$receiverMapping = array();
224-
225-
foreach ($container->findTaggedServiceIds($this->receiverTag) as $id => $tags) {
226-
$receiverClass = $container->findDefinition($id)->getClass();
227-
if (!is_subclass_of($receiverClass, ReceiverInterface::class)) {
228-
throw new RuntimeException(sprintf('Invalid receiver "%s": class "%s" must implement interface "%s".', $id, $receiverClass, ReceiverInterface::class));
229-
}
230-
231-
$receiverMapping[$id] = new Reference($id);
232-
233-
foreach ($tags as $tag) {
234-
if (isset($tag['alias'])) {
235-
$receiverMapping[$tag['alias']] = $receiverMapping[$id];
236-
}
237-
}
238-
}
239-
240-
if ($container->hasDefinition('console.command.messenger_consume_messages')) {
241-
$receiverNames = array();
242-
foreach ($receiverMapping as $name => $reference) {
243-
$receiverNames[(string) $reference] = $name;
244-
}
245-
$container->getDefinition('console.command.messenger_consume_messages')->replaceArgument(3, array_values($receiverNames));
246-
}
223+
$receiverMapping = self::findReceivers($container, $this->receiverTag);
247224

248225
$container->getDefinition('messenger.receiver_locator')->replaceArgument(0, $receiverMapping);
249226
}
@@ -312,4 +289,26 @@ private function registerBusMiddleware(ContainerBuilder $container, string $busI
312289

313290
$container->getDefinition($busId)->replaceArgument(0, $middlewareReferences);
314291
}
292+
293+
public static function findReceivers(ContainerBuilder $container, string $receiverTag)
294+
{
295+
$receiverMapping = array();
296+
297+
foreach ($container->findTaggedServiceIds($receiverTag) as $id => $tags) {
298+
$receiverClass = $container->findDefinition($id)->getClass();
299+
if (!is_subclass_of($receiverClass, ReceiverInterface::class)) {
300+
throw new RuntimeException(sprintf('Invalid receiver "%s": class "%s" must implement interface "%s".', $id, $receiverClass, ReceiverInterface::class));
301+
}
302+
303+
$receiverMapping[$id] = new Reference($id);
304+
305+
foreach ($tags as $tag) {
306+
if (isset($tag['alias'])) {
307+
$receiverMapping[$tag['alias']] = $receiverMapping[$id];
308+
}
309+
}
310+
}
311+
312+
return $receiverMapping;
313+
}
315314
}

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

Lines changed: 1 addition & 35 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,24 +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-
new Reference('message_bus'),
245-
new Reference('messenger.receiver_locator'),
246-
null,
247-
null,
248-
));
249-
250-
$container->register(AmqpReceiver::class, AmqpReceiver::class)->addTag('messenger.receiver', array('alias' => 'amqp'));
251-
$container->register(DummyReceiver::class, DummyReceiver::class)->addTag('messenger.receiver', array('alias' => 'dummy'));
252-
253-
(new MessengerPass())->process($container);
254-
255-
$this->assertSame(array('amqp', 'dummy'), $container->getDefinition('console.command.messenger_consume_messages')->getArgument(3));
256-
}
257-
258238
public function testItRegistersSenders()
259239
{
260240
$container = $this->getContainerBuilder();
@@ -587,20 +567,6 @@ public function __invoke(DummyMessage $message): void
587567
}
588568
}
589569

590-
class DummyReceiver implements ReceiverInterface
591-
{
592-
public function receive(callable $handler): void
593-
{
594-
for ($i = 0; $i < 3; ++$i) {
595-
$handler(Envelope::wrap(new DummyMessage("Dummy $i")));
596-
}
597-
}
598-
599-
public function stop(): void
600-
{
601-
}
602-
}
603-
604570
class InvalidReceiver
605571
{
606572
}
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 <[email protected]>
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)