Skip to content

[FrameworkBundle][Messenger] Add AsRoutedMessage attribute #49143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class UnusedTagsPass implements CompilerPassInterface
'messenger.bus',
'messenger.message_handler',
'messenger.receiver',
'messenger.routed_message',
'messenger.transport_factory',
'mime.mime_type_guesser',
'monolog.logger',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mercure\HubRegistry;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\Attribute\AsRoutedMessage;
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsTransportFactory;
use Symfony\Component\Messenger\Bridge\Amqp\Transport\AmqpTransportFactory;
use Symfony\Component\Messenger\Bridge\Beanstalkd\Transport\BeanstalkdTransportFactory;
Expand Down Expand Up @@ -687,6 +688,14 @@ public function load(array $configs, ContainerBuilder $container)
}
$definition->addTag('messenger.message_handler', $tagAttributes);
});
$container->registerAttributeForAutoconfiguration(AsRoutedMessage::class, static function (ChildDefinition $definition, AsRoutedMessage $attribute, \ReflectionClass $reflector): void {
$tagAttributes = [
'class' => $reflector->getName(),
'transports' => $attribute->getTransports(),
];

$definition->addTag('messenger.routed_message', $tagAttributes);
});

if (!$container->getParameter('kernel.debug')) {
// remove tagged iterator argument for resource checkers
Expand Down
33 changes: 33 additions & 0 deletions src/Symfony/Component/Messenger/Attribute/AsRoutedMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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\Attribute;

/**
* Attribute to configure transports to be used to dispatch a message.
*
* @author Alexandre Daubois <alex.daubois@gmail.com>
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class AsRoutedMessage
{
private array $transports;

public function __construct(array|string $transports)
{
$this->transports = (array) $transports;
}

public function getTransports(): array
{
return $this->transports;
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CHANGELOG
`Symfony\Component\Messenger\Transport\InMemory\InMemoryTransport` and
`Symfony\Component\Messenger\Transport\InMemory\InMemoryTransportFactory`
* Allow passing a string instead of an array in `TransportNamesStamp`
* Add `#[AsRoutedMessage]` attribute

6.2
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public function process(ContainerBuilder $container)
$this->registerReceivers($container, $busIds);
}
$this->registerHandlers($container, $busIds);

if ($container->hasDefinition('messenger.senders_locator')) {
$this->registerRoutedMessages($container);
}
}

private function registerHandlers(ContainerBuilder $container, array $busIds): void
Expand Down Expand Up @@ -405,4 +409,30 @@ private function getServiceClass(ContainerBuilder $container, string $serviceId)
return $definition->getClass();
}
}

private function registerRoutedMessages(ContainerBuilder $container): void
{
$mapping = $container->getDefinition('messenger.senders_locator')->getArgument(0) ?? [];
$receiverAliases = array_map(
static fn ($tagAttributes) => $tagAttributes[0]['alias'],
$container->findTaggedServiceIds('messenger.receiver')
);

foreach ($container->findTaggedServiceIds('messenger.routed_message') as [$routedMessage]) {
$messageClass = $routedMessage['class'];
$mapping[$routedMessage['class']] ??= [];

foreach ($routedMessage['transports'] as $transport) {
if (!\in_array($transport, $receiverAliases)) {
throw new RuntimeException(sprintf('Invalid Messenger routing configuration: the "%s" class is being routed to a sender called "%s". This is not a valid transport or service id.', $messageClass, $transport));
}

$mapping[$messageClass][] = $transport;
}
}

$container->getDefinition('messenger.senders_locator')
->setArgument(0, $mapping)
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\Bridge\Amqp\Transport\AmqpReceiver;
use Symfony\Component\Messenger\Bridge\Redis\Transport\RedisReceiver;
use Symfony\Component\Messenger\Command\ConsumeMessagesCommand;
use Symfony\Component\Messenger\Command\DebugCommand;
use Symfony\Component\Messenger\Command\FailedMessagesRetryCommand;
Expand Down Expand Up @@ -918,6 +919,68 @@ public function testFailedCommandsRegisteredWithServiceLocatorArgumentReplaced()
$removeDefinition = $container->getDefinition('console.command.messenger_failed_messages_remove');
$this->assertNotNull($removeDefinition->getArgument(1));
}

public function testRegisterRoutedMessageWithEmptySenderLocator()
{
$container = $this->getContainerBuilder();
$container->register('messenger.senders_locator', ServiceLocator::class)->setArgument(0, []);
$container->register(AmqpReceiver::class, AmqpReceiver::class)->addTag('messenger.receiver', ['alias' => 'amqp']);

$container->register(DummyMessage::class)
->addTag('messenger.routed_message', [
'class' => DummyMessage::class,
'transports' => ['amqp'],
])
;

(new MessengerPass())->process($container);
$mapping = $container->getDefinition('messenger.senders_locator')->getArgument(0);

$this->assertArrayHasKey(DummyMessage::class, $mapping);
$this->assertSame($mapping[DummyMessage::class][0], 'amqp');
}

public function testRegisterRoutedMessageWithInvalidTransport()
{
$container = $this->getContainerBuilder();
$container->register('messenger.senders_locator', ServiceLocator::class)->setArgument(0, []);

$container->register(DummyMessage::class)
->addTag('messenger.routed_message', [
'class' => DummyMessage::class,
'transports' => ['amqp'],
])
;

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Invalid Messenger routing configuration: the "Symfony\Component\Messenger\Tests\Fixtures\DummyMessage" class is being routed to a sender called "amqp". This is not a valid transport or service id.');
(new MessengerPass())->process($container);
}

public function testRegisterRoutedMessageWithExistingSenders()
{
$container = $this->getContainerBuilder();
$container->register('messenger.senders_locator', ServiceLocator::class)->setArgument(0, [
DummyMessage::class => ['amqp'],
]);

$container->register(AmqpReceiver::class, AmqpReceiver::class)->addTag('messenger.receiver', ['alias' => 'amqp']);
$container->register(RedisReceiver::class, RedisReceiver::class)->addTag('messenger.receiver', ['alias' => 'redis']);

$container->register(DummyMessage::class)
->addTag('messenger.routed_message', [
'class' => DummyMessage::class,
'transports' => ['redis'],
])
;

(new MessengerPass())->process($container);
$mapping = $container->getDefinition('messenger.senders_locator')->getArgument(0);

$this->assertArrayHasKey(DummyMessage::class, $mapping);
$this->assertSame($mapping[DummyMessage::class][0], 'amqp');
$this->assertSame($mapping[DummyMessage::class][1], 'redis');
}
}

class DummyHandler
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy