Skip to content

Commit 34cb522

Browse 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 ------- f27e594bad [Messenger] Autoconfigurable attributes
2 parents fd54cc6 + 366b387 commit 34cb522

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

Attribute/AsMessageHandler.php

Lines 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 <[email protected]>
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 <[email protected]>
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+
}

CHANGELOG.md

Lines 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.

Tests/DependencyInjection/MessengerPassTest.php

Lines 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');

Tests/Fixtures/TaggedDummyHandler.php

Lines 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)