8000 feature #45273 [Messenger] Allow AsMessageHandler attribute on method… · symfony/symfony@b5b48c0 · GitHub
[go: up one dir, main page]

Skip to content

Commit b5b48c0

Browse files
committed
feature #45273 [Messenger] Allow AsMessageHandler attribute on methods (mjpvandenberg, fabpot)
This PR was merged into the 6.1 branch. Discussion ---------- [Messenger] Allow AsMessageHandler attribute on methods | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | none | License | MIT | Doc PR | tbd Similar to the `#[AsEventListener]` attribute, this PR allows for using the `#[AsMessageHandler]` attribute on methods. Combining class-level and method-level usage inside a single class is supported, as shown by the adapted `MessengerPassTest::testTaggedMessageHandler` test. Commits ------- fc19ed5 Fix deps fe3d912 [Messenger] Allow AsMessageHandler attribute on methods
2 parents ba72a2f + fc19ed5 commit b5b48c0

File tree

6 files changed

+30
-6
lines changed

6 files changed

+30
-6
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -623,11 +623,16 @@ public function load(array $configs, ContainerBuilder $container)
623623
$container->registerAttributeForAutoconfiguration(AsController::class, static function (ChildDefinition $definition, AsController $attribute): void {
624624
$definition->addTag('controller.service_arguments');
625625
});
626-
$container->registerAttributeForAutoconfiguration(AsMessageHandler::class, static function (ChildDefinition $definition, AsMessageHandler $attribute): void {
626+
$container->registerAttributeForAutoconfiguration(AsMessageHandler::class, static function (ChildDefinition $definition, AsMessageHandler $attribute, \ReflectionClass|\ReflectionMethod $reflector): void {
627627
$tagAttributes = get_object_vars($attribute);
628628
$tagAttributes['from_transport'] = $tagAttributes['fromTransport'];
629629
unset($tagAttributes['fromTransport']);
630-
630+
if ($reflector instanceof \ReflectionMethod) {
631+
if (isset($tagAttributes['method'])) {
632+
throw new LogicException(sprintf('AsMessageHandler attribute cannot declare a method on "%s::%s()".', $reflector->class, $reflector->name));
633+
}
634+
$tagAttributes['method'] = $reflector->getName();
635+
}
631636
$definition->addTag('messenger.message_handler', $tagAttributes);
632637
});
633638

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"symfony/http-client": "^5.4|^6.0",
4848
"symfony/lock": "^5.4|^6.0",
4949
"symfony/mailer": "^5.4|^6.0",
50-
"symfony/messenger": "^5.4|^6.0",
50+
"symfony/messenger": "^6.1",
5151
"symfony/mime": "^5.4|^6.0",
5252
"symfony/notifier": "^5.4|^6.0",
5353
"symfony/process": "^5.4|^6.0",

src/Symfony/Component/Messenger/Attribute/AsMessageHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @author Alireza Mirsepassi <alirezamirsepassi@gmail.com>
1818
*/
19-
#[\Attribute(\Attribute::TARGET_CLASS)]
19+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
2020
class AsMessageHandler
2121
{
2222
public function __construct(

src/Symfony/Component/Messenger/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Add `SerializedMessageStamp` to avoid serializing a message when a retry occurs.
88
* Automatically resolve handled message type when method different from `__invoke` is used as handler.
9+
* Allow `#[AsMessageHandler]` attribute on methods.
910

1011
6.0
1112
---
< E7F5 div class="d-flex flex-row">

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\DependencyInjection\Compiler\ResolveClassPass;
1919
use Symfony\Component\DependencyInjection\Compiler\ResolveInstanceofConditionalsPass;
2020
use Symfony\Component\DependencyInjection\ContainerBuilder;
21+
use Symfony\Component\DependencyInjection\Exception\LogicException;
2122
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
2223
use Symfony\Component\DependencyInjection\Reference;
2324
use Symfony\Component\DependencyInjection\ServiceLocator;
@@ -142,10 +143,16 @@ public function testHandledMessageTypeResolvedWithMethodAndNoHandlesViaTagAttrib
142143
public function testTaggedMessageHandler()
143144
{
144145
$container = $this->getContainerBuilder($busId = 'message_bus');
145-
$container->registerAttributeForAutoconfiguration(AsMessageHandler::class, static function (ChildDefinition $definition, AsMessageHandler $attribute): void {
146+
$container->registerAttributeForAutoconfiguration(AsMessageHandler::class, static function (ChildDefinition $definition, AsMessageHandler $attribute, \ReflectionClass|\ReflectionMethod $reflector): void {
146147
$tagAttributes = get_object_vars($attribute);
147148
$tagAttributes['from_transport'] = $tagAttributes['fromTransport'];
148149
unset($tagAttributes['fromTransport']);
150+
if ($reflector instanceof \ReflectionMethod) {
151+
if (isset($tagAttributes['method'])) {
152+
throw new LogicException(sprintf('AsMessageHandler attribute cannot declare a method on "%s::%s()".', $reflector->class, $reflector->name));
153+
}
154+
$tagAttributes['method'] = $reflector->getName();
155+
}
149156

150157
$definition->addTag('messenger.message_handler', $tagAttributes);
151158
});
@@ -162,9 +169,15 @@ public function testTaggedMessageHandler()
162169
$this->assertSame(HandlersLocator::class, $handlersLocatorDefinition->getClass());
163170

164171
$handlerDescriptionMapping = $handlersLocatorDefinition->getArgument(0);
165-
$this->assertCount(1, $handlerDescriptionMapping);
172+
$this->assertCount(2, $handlerDescriptionMapping);
166173

167174
$this->assertHandlerDescriptor($container, $handlerDescriptionMapping, DummyMessage::class, [TaggedDummyHandler::class], [[]]);
175+
$this->assertHandlerDescriptor(
176+
$container,
177+
$handlerDescriptionMapping,
178+
SecondMessage::class,
179+
[[TaggedDummyHandler::class, 'handleSecondMessage']]
180+
);
168181
}
169182

170183
public function testProcessHandlersByBus()

src/Symfony/Component/Messenger/Tests/Fixtures/TaggedDummyHandler.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,9 @@ class TaggedDummyHandler
1010
public function __invoke(DummyMessage $message)
1111
{
1212
}
13+
14+
#[AsMessageHandler]
15+
public function handleSecondMessage(SecondMessage $message)
16+
{
17+
}
1318
}

0 commit comments

Comments
 (0)
0