Skip to content

Commit

Permalink
minor #27203 [Messenger][DX] Uses a default receiver when only one is…
Browse files Browse the repository at this point in the history
… defined (sroze)

This PR was squashed before being merged into the 4.1 branch (closes #27203).

Discussion
----------

[Messenger][DX] Uses a default receiver when only one is defined

| Q             | A
| ------------- | ---
| Branch?       | 4.1
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | ø
| License       | MIT
| Doc PR        | ø

When using only one receiver, as a developer, it makes no sense for me to have to precise the receiver name when using the `messenger:consume-messages` command. This is the change:

```patch
- bin/console messenger:consume-messages default
+ bin/console messenger:consume-messages
```

If I have more than one transport configured, I'll get the following message:

>
>  You have 0 or more than one receiver (no default have been found). You need to specify the receiver name with an argument.
>

Commits
-------

8315b86 [Messenger][DX] Uses a default receiver when only one is defined
  • Loading branch information
sroze committed May 9, 2018
2 parents 6207d70 + 8315b86 commit e0f225b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
Expand Up @@ -73,6 +73,7 @@
<argument type="service" id="message_bus" />
<argument type="service" id="messenger.receiver_locator" />
<argument type="service" id="logger" on-invalid="null" />
<argument>null</argument> <!-- Default receiver name -->

<tag name="console.command" command="messenger:consume-messages" />
</service>
Expand Down
Expand Up @@ -37,14 +37,16 @@ class ConsumeMessagesCommand extends Command
private $bus;
private $receiverLocator;
private $logger;
private $defaultReceiverName;

public function __construct(MessageBusInterface $bus, ContainerInterface $receiverLocator, LoggerInterface $logger = null)
public function __construct(MessageBusInterface $bus, ContainerInterface $receiverLocator, LoggerInterface $logger = null, string $defaultReceiverName = null)
{
parent::__construct();

$this->bus = $bus;
$this->receiverLocator = $receiverLocator;
$this->logger = $logger;
$this->defaultReceiverName = $defaultReceiverName;
}

/**
Expand All @@ -54,7 +56,7 @@ protected function configure(): void
{
$this
->setDefinition(array(
new InputArgument('receiver', InputArgument::REQUIRED, 'Name of the receiver'),
new InputArgument('receiver', $this->defaultReceiverName ? InputArgument::OPTIONAL : InputArgument::REQUIRED, 'Name of the receiver', $this->defaultReceiverName),
new InputOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Limit the number of received messages'),
new InputOption('memory-limit', 'm', InputOption::VALUE_REQUIRED, 'The memory limit the worker can consume'),
new InputOption('time-limit', 't', InputOption::VALUE_REQUIRED, 'The time limit in seconds the worker can run'),
Expand Down
Expand Up @@ -164,26 +164,32 @@ private function guessHandledClasses(\ReflectionClass $handlerClass, string $ser
private function registerReceivers(ContainerBuilder $container)
{
$receiverMapping = array();
foreach ($container->findTaggedServiceIds($this->receiverTag) as $id => $tags) {
foreach ($tags as $tag) {
$receiverMapping[$id] = new Reference($id);
$taggedReceivers = $container->findTaggedServiceIds($this->receiverTag);

foreach ($taggedReceivers as $id => $tags) {
$receiverMapping[$id] = new Reference($id);

foreach ($tags as $tag) {
if (isset($tag['name'])) {
$receiverMapping[$tag['name']] = $receiverMapping[$id];
}
}
}

if (1 === \count($taggedReceivers) && $container->hasDefinition('console.command.messenger_consume_messages')) {
$container->getDefinition('console.command.messenger_consume_messages')->replaceArgument(3, (string) current($receiverMapping));
}

$container->getDefinition('messenger.receiver_locator')->replaceArgument(0, $receiverMapping);
}

private function registerSenders(ContainerBuilder $container)
{
$senderLocatorMapping = array();
foreach ($container->findTaggedServiceIds($this->senderTag) as $id => $tags) {
foreach ($tags as $tag) {
$senderLocatorMapping[$id] = new Reference($id);
$senderLocatorMapping[$id] = new Reference($id);

foreach ($tags as $tag) {
if (isset($tag['name'])) {
$senderLocatorMapping[$tag['name']] = $senderLocatorMapping[$id];
}
Expand Down
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\Messenger\Command\ConsumeMessagesCommand;
use Symfony\Component\Messenger\Transport\AmqpExt\AmqpReceiver;
use Symfony\Component\Messenger\Transport\AmqpExt\AmqpSender;
use Symfony\Component\Messenger\Handler\Locator\ContainerHandlerLocator;
Expand Down Expand Up @@ -116,6 +117,41 @@ public function testItRegistersReceiversWithoutTagName()
$this->assertEquals(array(AmqpReceiver::class => new Reference(AmqpReceiver::class)), $container->getDefinition('messenger.receiver_locator')->getArgument(0));
}

public function testItRegistersOneReceiverAndSetsTheDefaultOneOnTheCommand()
{
$container = $this->getContainerBuilder();
$container->register('console.command.messenger_consume_messages', ConsumeMessagesCommand::class)->setArguments(array(
new Reference('message_bus'),
new Reference('messenger.receiver_locator'),
null,
null,
));

$container->register(AmqpReceiver::class, AmqpReceiver::class)->addTag('messenger.receiver', array('name' => 'amqp'));

(new MessengerPass())->process($container);

$this->assertSame(AmqpReceiver::class, $container->getDefinition('console.command.messenger_consume_messages')->getArgument(3));
}

public function testItRegistersMultipleReceiversAndDoesNotSetTheDefaultOneOnTheCommand()
{
$container = $this->getContainerBuilder();
$container->register('console.command.messenger_consume_messages', ConsumeMessagesCommand::class)->setArguments(array(
new Reference('message_bus'),
new Reference('messenger.receiver_locator'),
null,
null,
));

$container->register(AmqpReceiver::class, AmqpReceiver::class)->addTag('messenger.receiver', array('name' => 'amqp'));
$container->register(DummyReceiver::class, DummyReceiver::class)->addTag('messenger.receiver', array('name' => 'dummy'));

(new MessengerPass())->process($container);

$this->assertNull($container->getDefinition('console.command.messenger_consume_messages')->getArgument(3));
}

public function testItRegistersSenders()
{
$container = $this->getContainerBuilder();
Expand Down

0 comments on commit e0f225b

Please sign in to comment.