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

Skip to content

Commit f4081a4

Browse files
committed
bug #59198 [Messenger] Filter out non-consumable receivers when registering ConsumeMessagesCommand (wazum)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [Messenger] Filter out non-consumable receivers when registering `ConsumeMessagesCommand` | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #51556 | License | MIT This pull request enhances the `MessengerPass` in the Symfony Framework Bundle to automatically filter out non-consumable receivers when registering the `ConsumeMessagesCommand`. The key changes are: The FrameworkExtension has been updated to mark the "sync" transport as non-consumable by default, without requiring the user to explicitly set the `is_consumable` attribute. The `MessengerPass` then checks the `is_consumable` attribute on the `messenger.receiver` tags. If the attribute is set to `false`, the receiver is considered non-consumable and will not be added to the list of receivers for the consume command. If there is only one consumable receiver, the consume command will automatically use that receiver without prompting the user to select one (I added this as a separate commit if this is too implicit). These changes improve the user experience by removing unnecessary (and non-functioning) options. This is my first pull request for the _Symfony_ project, so please let me know what I can improve. Commits ------- 34c7e6f [Messenger] Filter out non-consumable receivers when registering `ConsumeMessagesCommand`
2 parents 4f652a5 + 34c7e6f commit f4081a4

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -2282,13 +2282,17 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
22822282
$transportRateLimiterReferences = [];
22832283
foreach ($config['transports'] as $name => $transport) {
22842284
$serializerId = $transport['serializer'] ?? 'messenger.default_serializer';
2285+
$tags = [
2286+
'alias' => $name,
2287+
'is_failure_transport' => \in_array($name, $failureTransports),
2288+
];
2289+
if (str_starts_with($transport['dsn'], 'sync://')) {
2290+
$tags['is_consumable'] = false;
2291+
}
22852292
$transportDefinition = (new Definition(TransportInterface::class))
22862293
->setFactory([new Reference('messenger.transport_factory'), 'createTransport'])
22872294
->setArguments([$transport['dsn'], $transport['options'] + ['transport_name' => $name], new Reference($serializerId)])
2288-
->addTag('messenger.receiver', [
2289-
'alias' => $name,
2290-
'is_failure_transport' => \in_array($name, $failureTransports),
2291-
])
2295+
->addTag('messenger.receiver', $tags)
22922296
;
22932297
$container->setDefinition($transportId = 'messenger.transport.'.$name, $transportDefinition);
22942298
$senderAliases[$name] = $transportId;

src/Symfony/Component/Messenger/Command/ConsumeMessagesCommand.php

+6
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ protected function interact(InputInterface $input, OutputInterface $output)
136136
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
137137

138138
if ($this->receiverNames && !$input->getArgument('receivers')) {
139+
if (1 === \count($this->receiverNames)) {
140+
$input->setArgument('receivers', $this->receiverNames);
141+
142+
return;
143+
}
144+
139145
$io->block('Which transports/receivers do you want to consume?', null, 'fg=white;bg=blue', ' ', true);
140146

141147
$io->writeln('Choose which receivers you want to consume messages from in order of priority.');

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ private function registerReceivers(ContainerBuilder $container, array $busIds):
274274
}
275275
}
276276

277+
$consumableReceiverNames = [];
277278
foreach ($container->findTaggedServiceIds('messenger.receiver') as $id => $tags) {
278279
$receiverClass = $this->getServiceClass($container, $id);
279280
if (!is_subclass_of($receiverClass, ReceiverInterface::class)) {
@@ -289,6 +290,9 @@ private function registerReceivers(ContainerBuilder $container, array $busIds):
289290
$failureTransportsMap[$tag['alias']] = $receiverMapping[$id];
290291
}
291292
}
293+
if (!isset($tag['is_consumable']) || $tag['is_consumable'] !== false) {
294+
$consumableReceiverNames[] = $tag['alias'] ?? $id;
295+
}
292296
}
293297
}
294298

@@ -314,7 +318,7 @@ private function registerReceivers(ContainerBuilder $container, array $busIds):
314318
$consumeCommandDefinition->replaceArgument(0, new Reference('messenger.routable_message_bus'));
315319
}
316320

317-
$consumeCommandDefinition->replaceArgument(4, array_values($receiverNames));
321+
$consumeCommandDefinition->replaceArgument(4, $consumableReceiverNames);
318322
try {
319323
$consumeCommandDefinition->replaceArgument(6, $busIds);
320324
} catch (OutOfBoundsException) {

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

+29
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
16+
use Symfony\Component\Console\Command\Command;
1617
use Symfony\Component\DependencyInjection\ChildDefinition;
1718
use Symfony\Component\DependencyInjection\Compiler\AttributeAutoconfigurationPass;
1819
use Symfony\Component\DependencyInjection\Compiler\ResolveChildDefinitionsPass;
@@ -506,6 +507,34 @@ public function testItSetsTheReceiverNamesOnTheSetupTransportsCommand()
506507
$this->assertSame(['amqp', 'dummy'], $container->getDefinition('console.command.messenger_setup_transports')->getArgument(1));
507508
}
508509

510+
public function testOnlyConsumableTransportsAreAddedToConsumeCommand()
511+
{
512+
$container = new ContainerBuilder();
513+
514+
$container->register('messenger.transport.async', DummyReceiver::class)
515+
->addTag('messenger.receiver', ['alias' => 'async']);
516+
$container->register('messenger.transport.sync', DummyReceiver::class)
517+
->addTag('messenger.receiver', ['alias' => 'sync', 'is_consumable' => false]);
518+
$container->register('messenger.receiver_locator', ServiceLocator::class)
519+
->setArguments([[]]);
520+
521+
$container->register('console.command.messenger_consume_messages', Command::class)
522+
->setArguments([
523+
null,
524+
null,
525+
null,
526+
null,
527+
[],
528+
]);
529+
530+
(new MessengerPass())->process($container);
531+
532+
$this->assertSame(
533+
['async'],
534+
$container->getDefinition('console.command.messenger_consume_messages')->getArgument(4)
535+
);
536+
}
537+
509538
/**
510539
* @group legacy
511540
*/

0 commit comments

Comments
 (0)