Skip to content

Commit f249284

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 ------- fc19ed5852 Fix deps fe3d91247a [Messenger] Allow AsMessageHandler attribute on methods
2 parents 7c7e347 + 38cffaf commit f249284

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

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

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

0 commit comments

Comments
 (0)