Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug #28328 [Messenger][FrameworkBundle] Move commands-specifics to a …
…compiler pass in FWB (sroze) This PR was merged into the 4.1 branch. Discussion ---------- [Messenger][FrameworkBundle] Move commands-specifics to a compiler pass in FWB | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #28271 (comment) | License | MIT | Doc PR | ø We very recently [broke the tests](#28271 (comment)) by adding a legitimate BC-break in one of Messenger's command. The failure is on FrameworkBundle (on the 4.1 branch) when using the "latest" dependencies (so messenger on master): the XML configuration does not match anymore the class. Even though it makes sense to have most the compiler pass in the components, it does not make much sense to have things into them that are very coupled to what we defined in FWB' XML. This pull-request moves what is related to this command to the FWB directly. Commits ------- b5415ea Move commands-specifics to a compiler pass in FWB
- Loading branch information
Showing
7 changed files
with
156 additions
and
59 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/MessengerCommandsPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\Messenger\DependencyInjection\MessengerPass; | ||
|
||
/** | ||
* @author Samuel Roze <samuel.roze@gmail.com> | ||
*/ | ||
class MessengerCommandsPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!$container->hasDefinition('console.command.messenger_consume_messages')) { | ||
return; | ||
} | ||
|
||
$buses = array(); | ||
foreach ($container->findTaggedServiceIds('messenger.bus') as $busId => $tags) { | ||
$buses[$busId] = new Reference($busId); | ||
} | ||
|
||
$container | ||
->getDefinition('console.command.messenger_consume_messages') | ||
->replaceArgument(3, $this->findReceiverNames($container)) | ||
; | ||
} | ||
|
||
private function findReceiverNames(ContainerBuilder $container) | ||
{ | ||
$receiverNames = array(); | ||
foreach (MessengerPass::findReceivers($container, 'messenger.receiver') as $name => $reference) { | ||
$receiverNames[(string) $reference] = $name; | ||
} | ||
|
||
return array_values($receiverNames); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...y/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/MessengerCommandsPassTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\MessengerCommandsPass; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\Messenger\Command\ConsumeMessagesCommand; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
use Symfony\Component\Messenger\Tests\Fixtures\DummyReceiver; | ||
use Symfony\Component\Messenger\Transport\AmqpExt\AmqpReceiver; | ||
|
||
class MessengerCommandsPassTest extends TestCase | ||
{ | ||
public function testItRegistersMultipleReceiversAndSetsTheReceiverNamesOnTheCommand() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$container->register('my_bus_name', MessageBusInterface::class)->addTag('messenger.bus')->setArgument(0, array()); | ||
$container->register('console.command.messenger_consume_messages', ConsumeMessagesCommand::class)->setArguments(array( | ||
null, | ||
new Reference('messenger.receiver_locator'), | ||
null, | ||
null, | ||
null, | ||
)); | ||
|
||
$container->register(AmqpReceiver::class, AmqpReceiver::class)->addTag('messenger.receiver', array('alias' => 'amqp')); | ||
$container->register(DummyReceiver::class, DummyReceiver::class)->addTag('messenger.receiver', array('alias' => 'dummy')); | ||
|
||
(new MessengerCommandsPass())->process($container); | ||
|
||
$this->assertSame(array('amqp', 'dummy'), $container->getDefinition('console.command.messenger_consume_messages')->getArgument(3)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/Symfony/Component/Messenger/Tests/Fixtures/DummyReceiver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Tests\Fixtures; | ||
|
||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Transport\ReceiverInterface; | ||
|
||
class DummyReceiver implements ReceiverInterface | ||
{ | ||
public function receive(callable $handler): void | ||
{ | ||
for ($i = 0; $i < 3; ++$i) { | ||
$handler(Envelope::wrap(new DummyMessage("Dummy $i"))); | ||
} | ||
} | ||
|
||
public function stop(): void | ||
{ | ||
} | ||
} |