From f27e594bad7229eee0272c2a5be8ef34a118f852 Mon Sep 17 00:00:00 2001 From: Alireza Mirsepassi Date: Tue, 19 Oct 2021 17:43:31 +0330 Subject: [PATCH] [Messenger] Autoconfigurable attributes --- .../FrameworkExtension.php | 8 +++++ .../Messenger/Attribute/AsMessageHandler.php | 30 ++++++++++++++++ src/Symfony/Component/Messenger/CHANGELOG.md | 1 + .../DependencyInjection/MessengerPassTest.php | 35 +++++++++++++++++++ .../Tests/Fixtures/TaggedDummyHandler.php | 13 +++++++ 5 files changed, 87 insertions(+) create mode 100644 src/Symfony/Component/Messenger/Attribute/AsMessageHandler.php create mode 100644 src/Symfony/Component/Messenger/Tests/Fixtures/TaggedDummyHandler.php diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 7cf9a7a656fe3..dc4dfc940f138 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -97,6 +97,7 @@ use Symfony\Component\Mailer\Bridge\Sendinblue\Transport\SendinblueTransportFactory; use Symfony\Component\Mailer\Mailer; use Symfony\Component\Mercure\HubRegistry; +use Symfony\Component\Messenger\Attribute\AsMessageHandler; use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsTransportFactory; use Symfony\Component\Messenger\Bridge\Amqp\Transport\AmqpTransportFactory; use Symfony\Component\Messenger\Bridge\Beanstalkd\Transport\BeanstalkdTransportFactory; @@ -584,6 +585,13 @@ public function load(array $configs, ContainerBuilder $container) $container->registerAttributeForAutoconfiguration(AsController::class, static function (ChildDefinition $definition, AsController $attribute): void { $definition->addTag('controller.service_arguments'); }); + $container->registerAttributeForAutoconfiguration(AsMessageHandler::class, static function (ChildDefinition $definition, AsMessageHandler $attribute): void { + $tagAttributes = get_object_vars($attribute); + $tagAttributes['from_transport'] = $tagAttributes['fromTransport']; + unset($tagAttributes['fromTransport']); + + $definition->addTag('messenger.message_handler', $tagAttributes); + }); if (!$container->getParameter('kernel.debug')) { // remove tagged iterator argument for resource checkers diff --git a/src/Symfony/Component/Messenger/Attribute/AsMessageHandler.php b/src/Symfony/Component/Messenger/Attribute/AsMessageHandler.php new file mode 100644 index 0000000000000..8c12a80343dc2 --- /dev/null +++ b/src/Symfony/Component/Messenger/Attribute/AsMessageHandler.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Messenger\Attribute; + +/** + * Service tag to autoconfigure message handlers. + * + * @author Alireza Mirsepassi + */ +#[\Attribute(\Attribute::TARGET_CLASS)] +class AsMessageHandler +{ + public function __construct( + public ?string $bus = null, + public ?string $fromTransport = null, + public ?string $handles = null, + public ?string $method = null, + public int $priority = 0, + ) { + } +} diff --git a/src/Symfony/Component/Messenger/CHANGELOG.md b/src/Symfony/Component/Messenger/CHANGELOG.md index c402c36e8273b..28badcc6e60f6 100644 --- a/src/Symfony/Component/Messenger/CHANGELOG.md +++ b/src/Symfony/Component/Messenger/CHANGELOG.md @@ -4,6 +4,7 @@ CHANGELOG 5.4 --- + * Add `AsMessageHandler` attribute for declaring message handlers on PHP 8. * Add `StopWorkerExceptionInterface` and its implementation `StopWorkerException` to stop the worker. * Add support for resetting container services after each messenger message. * Added `WorkerMetadata` class which allows you to access the configuration details of a worker, like `queueNames` and `transportNames` it consumes from. diff --git a/src/Symfony/Component/Messenger/Tests/DependencyInjection/MessengerPassTest.php b/src/Symfony/Component/Messenger/Tests/DependencyInjection/MessengerPassTest.php index 1235bbbdcbd1d..5f49bbb3dd58e 100644 --- a/src/Symfony/Component/Messenger/Tests/DependencyInjection/MessengerPassTest.php +++ b/src/Symfony/Component/Messenger/Tests/DependencyInjection/MessengerPassTest.php @@ -13,12 +13,15 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\DependencyInjection\ChildDefinition; +use Symfony\Component\DependencyInjection\Compiler\AttributeAutoconfigurationPass; use Symfony\Component\DependencyInjection\Compiler\ResolveChildDefinitionsPass; use Symfony\Component\DependencyInjection\Compiler\ResolveClassPass; +use Symfony\Component\DependencyInjection\Compiler\ResolveInstanceofConditionalsPass; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Exception\RuntimeException; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\ServiceLocator; +use Symfony\Component\Messenger\Attribute\AsMessageHandler; use Symfony\Component\Messenger\Bridge\Amqp\Transport\AmqpReceiver; use Symfony\Component\Messenger\Command\ConsumeMessagesCommand; use Symfony\Component\Messenger\Command\DebugCommand; @@ -43,6 +46,7 @@ use Symfony\Component\Messenger\Tests\Fixtures\MultipleBusesMessage; use Symfony\Component\Messenger\Tests\Fixtures\MultipleBusesMessageHandler; use Symfony\Component\Messenger\Tests\Fixtures\SecondMessage; +use Symfony\Component\Messenger\Tests\Fixtures\TaggedDummyHandler; use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface; class MessengerPassTest extends TestCase @@ -101,6 +105,37 @@ public function testFromTransportViaTagAttribute() $this->assertHandlerDescriptor($container, $handlerDescriptionMapping, DummyMessage::class, [DummyHandler::class], [['from_transport' => 'async']]); } + /** + * @requires PHP 8 + */ + public function testTaggedMessageHandler() + { + $container = $this->getContainerBuilder($busId = 'message_bus'); + $container->registerAttributeForAutoconfiguration(AsMessageHandler::class, static function (ChildDefinition $definition, AsMessageHandler $attribute): void { + $tagAttributes = get_object_vars($attribute); + $tagAttributes['from_transport'] = $tagAttributes['fromTransport']; + unset($tagAttributes['fromTransport']); + + $definition->addTag('messenger.message_handler', $tagAttributes); + }); + $container + ->register(TaggedDummyHandler::class, TaggedDummyHandler::class) + ->setAutoconfigured(true) + ; + + (new AttributeAutoconfigurationPass())->process($container); + (new ResolveInstanceofConditionalsPass())->process($container); + (new MessengerPass())->process($container); + + $handlersLocatorDefinition = $container->getDefinition($busId.'.messenger.handlers_locator'); + $this->assertSame(HandlersLocator::class, $handlersLocatorDefinition->getClass()); + + $handlerDescriptionMapping = $handlersLocatorDefinition->getArgument(0); + $this->assertCount(1, $handlerDescriptionMapping); + + $this->assertHandlerDescriptor($container, $handlerDescriptionMapping, DummyMessage::class, [TaggedDummyHandler::class], [[]]); + } + public function testProcessHandlersByBus() { $container = $this->getContainerBuilder($commandBusId = 'command_bus'); diff --git a/src/Symfony/Component/Messenger/Tests/Fixtures/TaggedDummyHandler.php b/src/Symfony/Component/Messenger/Tests/Fixtures/TaggedDummyHandler.php new file mode 100644 index 0000000000000..6007a601ba791 --- /dev/null +++ b/src/Symfony/Component/Messenger/Tests/Fixtures/TaggedDummyHandler.php @@ -0,0 +1,13 @@ +