Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c3e4bf9

Browse filesBrowse files
committed
feature #43588 [Messenger] Autoconfigurable attributes (alirezamirsepassi)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [Messenger] Autoconfigurable attributes | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #41106 | License | MIT | Doc PR | symfony/symfony-docs#15990 Commits ------- f27e594 [Messenger] Autoconfigurable attributes
2 parents 732e9a6 + f27e594 commit c3e4bf9
Copy full SHA for c3e4bf9

File tree

5 files changed

+87
-0
lines changed
Filter options

5 files changed

+87
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
use Symfony\Component\Mailer\Bridge\Sendinblue\Transport\SendinblueTransportFactory;
9898
use Symfony\Component\Mailer\Mailer;
9999
use Symfony\Component\Mercure\HubRegistry;
100+
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
100101
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsTransportFactory;
101102
use Symfony\Component\Messenger\Bridge\Amqp\Transport\AmqpTransportFactory;
102103
use Symfony\Component\Messenger\Bridge\Beanstalkd\Transport\BeanstalkdTransportFactory;
@@ -584,6 +585,13 @@ public function load(array $configs, ContainerBuilder $container)
584585
$container->registerAttributeForAutoconfiguration(AsController::class, static function (ChildDefinition $definition, AsController $attribute): void {
585586
$definition->addTag('controller.service_arguments');
586587
});
588+
$container->registerAttributeForAutoconfiguration(AsMessageHandler::class, static function (ChildDefinition $definition, AsMessageHandler $attribute): void {
589+
$tagAttributes = get_object_vars($attribute);
590+
$tagAttributes['from_transport'] = $tagAttributes['fromTransport'];
591+
unset($tagAttributes['fromTransport']);
592+
593+
$definition->addTag('messenger.message_handler', $tagAttributes);
594+
});
587595

588596
if (!$container->getParameter('kernel.debug')) {
589597
// remove tagged iterator argument for resource checkers
+30Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Attribute;
13+
14+
/**
15+
* Service tag to autoconfigure message handlers.
16+
*
17+
* @author Alireza Mirsepassi <alirezamirsepassi@gmail.com>
18+
*/
19+
#[\Attribute(\Attribute::TARGET_CLASS)]
20+
class AsMessageHandler
21+
{
22+
public function __construct(
23+
public ?string $bus = null,
24+
public ?string $fromTransport = null,
25+
public ?string $handles = null,
26+
public ?string $method = null,
27+
public int $priority = 0,
28+
) {
29+
}
30+
}

‎src/Symfony/Component/Messenger/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
5.4
55
---
66

7+
* Add `AsMessageHandler` attribute for declaring message handlers on PHP 8.
78
* Add `StopWorkerExceptionInterface` and its implementation `StopWorkerException` to stop the worker.
89
* Add support for resetting container services after each messenger message.
910
* Added `WorkerMetadata` class which allows you to access the configuration details of a worker, like `queueNames` and `transportNames` it consumes from.

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/Tests/DependencyInjection/MessengerPassTest.php
+35Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\DependencyInjection\ChildDefinition;
16+
use Symfony\Component\DependencyInjection\Compiler\AttributeAutoconfigurationPass;
1617
use Symfony\Component\DependencyInjection\Compiler\ResolveChildDefinitionsPass;
1718
use Symfony\Component\DependencyInjection\Compiler\ResolveClassPass;
19+
use Symfony\Component\DependencyInjection\Compiler\ResolveInstanceofConditionalsPass;
1820
use Symfony\Component\DependencyInjection\ContainerBuilder;
1921
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
2022
use Symfony\Component\DependencyInjection\Reference;
2123
use Symfony\Component\DependencyInjection\ServiceLocator;
24+
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
2225
use Symfony\Component\Messenger\Bridge\Amqp\Transport\AmqpReceiver;
2326
use Symfony\Component\Messenger\Command\ConsumeMessagesCommand;
2427
use Symfony\Component\Messenger\Command\DebugCommand;
@@ -43,6 +46,7 @@
4346
use Symfony\Component\Messenger\Tests\Fixtures\MultipleBusesMessage;
4447
use Symfony\Component\Messenger\Tests\Fixtures\MultipleBusesMessageHandler;
4548
use Symfony\Component\Messenger\Tests\Fixtures\SecondMessage;
49+
use Symfony\Component\Messenger\Tests\Fixtures\TaggedDummyHandler;
4650
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
4751

4852
class MessengerPassTest extends TestCase
@@ -101,6 +105,37 @@ public function testFromTransportViaTagAttribute()
101105
$this->assertHandlerDescriptor($container, $handlerDescriptionMapping, DummyMessage::class, [DummyHandler::class], [['from_transport' => 'async']]);
102106
}
103107

108+
/**
109+
* @requires PHP 8
110+
*/
111+
public function testTaggedMessageHandler()
112+
{
113+
$container = $this->getContainerBuilder($busId = 'message_bus');
114+
$container->registerAttributeForAutoconfiguration(AsMessageHandler::class, static function (ChildDefinition $definition, AsMessageHandler $attribute): void {
115+
$tagAttributes = get_object_vars($attribute);
116+
$tagAttributes['from_transport'] = $tagAttributes['fromTransport'];
117+
unset($tagAttributes['fromTransport']);
118+
119+
$definition->addTag('messenger.message_handler', $tagAttributes);
120+
});
121+
$container
122+
->register(TaggedDummyHandler::class, TaggedDummyHandler::class)
123+
->setAutoconfigured(true)
124+
;
125+
126+
(new AttributeAutoconfigurationPass())->process($container);
127+
(new ResolveInstanceofConditionalsPass())->process($container);
128+
(new MessengerPass())->process($container);
129+
130+
$handlersLocatorDefinition = $container->getDefinition($busId.'.messenger.handlers_locator');
131+
$this->assertSame(HandlersLocator::class, $handlersLocatorDefinition->getClass());
132+
133+
$handlerDescriptionMapping = $handlersLocatorDefinition->getArgument(0);
134+
$this->assertCount(1, $handlerDescriptionMapping);
135+
136+
$this->assertHandlerDescriptor($container, $handlerDescriptionMapping, DummyMessage::class, [TaggedDummyHandler::class], [[]]);
137+
}
138+
104139
public function testProcessHandlersByBus()
105140
{
106141
$container = $this->getContainerBuilder($commandBusId = 'command_bus');
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Symfony\Component\Messenger\Tests\Fixtures;
4+
5+
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
6+
7+
#[AsMessageHandler]
8+
class TaggedDummyHandler
9+
{
10+
public function __invoke(DummyMessage $message)
11+
{
12+
}
13+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.