8000 [Messenger] Allow AsMessageHandler attribute on methods by mjpvandenberg · Pull Request #45273 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Messenger] Allow AsMessageHandler attribute on methods #45273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -622,11 +622,16 @@ public function load(array $configs, ContainerBuilder $container)
$container->registerAttributeForAutoconfiguration(AsController::class, static function (ChildDefinition $definition, AsController $attribute): void {
$definition->addTag('controller.service_arguments');
});
$container->registerAttributeForAutoconfiguration(AsMessageHandler::class, static function (ChildDefinition $definition, AsMessageHandler $attribute): void {
$container->registerAttributeForAutoconfiguration(AsMessageHandler::class, static function (ChildDefinition $definition, AsMessageHandler $attribute, \ReflectionClass|\ReflectionMethod $reflector): void {
$tagAttributes = get_object_vars($attribute);
$tagAttributes['from_transport'] = $tagAttributes['fromTransport'];
unset($tagAttributes['fromTransport']);

if ($reflector instanceof \ReflectionMethod) {
if (isset($tagAttributes['method'])) {
throw new LogicException(sprintf('AsMessageHandler attribute cannot declare a method on "%s::%s()".', $reflector->class, $reflector->name));
}
$tagAttributes['method'] = $reflector->getName();
}
$definition->addTag('messenger.message_handler', $tagAttributes);
});

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"symfony/http-client": "^5.4|^6.0",
"symfony/lock": "^5.4|^6.0",
"symfony/mailer": "^5.4|^6.0",
"symfony/messenger": "^5.4|^6.0",
"symfony/messenger": "^6.1",
"symfony/mime": "^5.4|^6.0",
"symfony/notifier": "^5.4|^6.0",
"symfony/process": "^5.4|^6.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @author Alireza Mirsepassi <alirezamirsepassi@gmail.com>
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
class AsMessageHandler
{
public function __construct(
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

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

6.0
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\DependencyInjection\Compiler\ResolveClassPass;
use Symfony\Component\DependencyInjection\Compiler\ResolveInstanceofConditionalsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ServiceLocator;
Expand Down Expand Up @@ -142,10 +143,16 @@ public function testHandledMessageTypeResolvedWithMethodAndNoHandlesViaTagAttrib
public function testTaggedMessageHandler()
{
$container = $this->getContainerBuilder($busId = 'message_bus');
$container->registerAttributeForAutoconfiguration(AsMessageHandler::class, static function (ChildDefinition $definition, AsMessageHandler $attribute): void {
$container->registerAttributeForAutoconfiguration(AsMessageHandler::class, static function (ChildDefinition $definition, AsMessageHandler $attribute, \ReflectionClass|\ReflectionMethod $reflector): void {
$tagAttributes = get_object_vars($attribute);
$tagAttributes['from_transport'] = $tagAttributes['fromTransport'];
unset($tagAttributes['fromTransport']);
if ($reflector instanceof \ReflectionMethod) {
if (isset($tagAttributes['method'])) {
throw new LogicException(sprintf('AsMessageHandler attribute cannot declare a method on "%s::%s()".', $reflector->class, $reflector->name));
}
$tagAttributes['method'] = $reflector->getName();
}

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

$handlerDescriptionMapping = $handlersLocatorDefinition->getArgument(0);
$this->assertCount(1, $handlerDescriptionMapping);
$this->assertCount(2, $handlerDescriptionMapping);

$this->assertHandlerDescriptor($container, $handlerDescriptionMapping, DummyMessage::class, [TaggedDummyHandler::class], [[]]);
$this->assertHandlerDescriptor(
$container,
$handlerDescriptionMapping,
SecondMessage::class,
[[TaggedDummyHandler::class, 'handleSecondMessage']]
);
}

public function testProcessHandlersByBus()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ class TaggedDummyHandler
public function __invoke(DummyMessage $message)
{
}

#[AsMessageHandler]
public function handleSecondMessage(SecondMessage $message)
{
}
}
0