8000 [Messenger] Allow AsMessageHandler attribute on methods · symfony/symfony@fe3d912 · GitHub
[go: up one dir, main page]

Skip to content

Commit fe3d912

Browse files
mjpvandenbergfabpot
authored andcommitted
[Messenger] Allow AsMessageHandler attribute on methods
1 parent 68a2ba2 commit fe3d912

File tree

5 files changed

+29
-5
lines changed

5 files changed

+29
-5
lines changed

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

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

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

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