Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 67b8b00

Browse filesBrowse files
committed
Move commands-specifics to a compiler pass in FWB
1 parent 30de3b8 commit 67b8b00
Copy full SHA for 67b8b00

File tree

Expand file treeCollapse file tree

7 files changed

+166
-60
lines changed
Filter options
Expand file treeCollapse file tree

7 files changed

+166
-60
lines changed
+62Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
private $busTag;
25+
private $receiverTag;
26+
27+
public function __construct(string $busTag = 'messenger.bus', string $receiverTag = 'messenger.receiver')
28+
{
29+
$this->busTag = $busTag;
30+
$this->receiverTag = $receiverTag;
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function process(ContainerBuilder $container)
37+
{
38+
if (!$container->hasDefinition('console.command.messenger_consume_messages')) {
39+
return;
40+
}
41+
42+
$buses = array();
43+
foreach ($container->findTaggedServiceIds($this->busTag) as $busId => $tags) {
44+
$buses[$busId] = new Reference($busId);
45+
}
46+
47+
$container
48+
->getDefinition('console.command.messenger_consume_messages')
49+
->replaceArgument(3, $this->findReceiverNames($container))
50+
;
51+
}
52+
53+
private function findReceiverNames(ContainerBuilder $container)
54+
{
55+
$receiverNames = array();
56+
foreach (MessengerPass::findReceivers($container, $this->receiverTag) as $name => $reference) {
57+
$receiverNames[(string) $reference] = $name;
58+
}
59+
60+
return array_values($receiverNames);
61+
}
62+
}

‎src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
+2Lines 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);
+44Lines 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+
}

‎src/Symfony/Bundle/FrameworkBundle/composer.json

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/composer.json
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@
7373
"symfony/translation": "<3.4",
7474
"symfony/twig-bridge": "<4.1.1",
7575
"symfony/validator": "<4.1",
76-
"symfony/workflow": "<4.1"
76+
"symfony/workflow": "<4.1",
77+
"symfony/messenger": "<4.1.5"
7778
},
7879
"suggest": {
7980
"ext-apcu": "For best performance of the system caches",

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/DependencyInjection/MessengerPass.php
+26-24Lines changed: 26 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,29 @@ private function registerBusMiddleware(ContainerBuilder $container, string $busI
312289

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

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/Tests/DependencyInjection/MessengerPassTest.php
+1-35Lines 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
}
+29Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.