diff --git a/Classes/HandlersLocatorFactory.php b/Classes/HandlersLocatorFactory.php index a060ab9..f3d4a7f 100644 --- a/Classes/HandlersLocatorFactory.php +++ b/Classes/HandlersLocatorFactory.php @@ -5,6 +5,7 @@ namespace DigiComp\FlowSymfonyBridge\Messenger; use Neos\Flow\Annotations as Flow; use Neos\Flow\ObjectManagement\ObjectManagerInterface; use Neos\Flow\Reflection\ReflectionService; +use Symfony\Component\Messenger\Attribute\AsMessageHandler; use Symfony\Component\Messenger\Handler\HandlerDescriptor; use Symfony\Component\Messenger\Handler\HandlersLocator; use Symfony\Component\Messenger\Handler\MessageSubscriberInterface; @@ -50,6 +51,27 @@ class HandlersLocatorFactory ); } } + $asHandlerClasses = $this->reflectionService + ->getClassNamesByAnnotation(AsMessageHandler::class); + foreach ($asHandlerClasses as $asHandlerClass) { + /** @var AsMessageHandler $annotation */ + $annotation = $this->reflectionService->getClassAnnotation($asHandlerClass, AsMessageHandler::class); + $config['from_transport'] = $annotation->fromTransport; + $config['priority'] = $annotation->priority; + $method = $annotation->method ?? '__invoke'; + $messageName = $annotation->handles; + if ($messageName === null) { + $arguments = $this->reflectionService->getMethodParameters($asHandlerClass, $method); + $messageName = $arguments[\array_key_first($arguments)]['class']; + } + if ($annotation->bus !== null && $annotation->bus !== $busName) { + continue; + } + $handlerDescriptors[$messageName][] = new HandlerDescriptor( + $this->objectManager->get($asHandlerClass), + $config + ); + } // TODO: Maybe we can allow handlers to be added to bus or globally by configuration? return new HandlersLocator($handlerDescriptors); diff --git a/Tests/Functional/Fixtures/Message/TestMessageHandler.php b/Tests/Functional/Fixtures/Message/TestMessageHandler.php index bddf7d0..efb5f38 100644 --- a/Tests/Functional/Fixtures/Message/TestMessageHandler.php +++ b/Tests/Functional/Fixtures/Message/TestMessageHandler.php @@ -2,15 +2,11 @@ namespace DigiComp\FlowSymfonyBridge\Messenger\Tests\Functional\Fixtures\Message; -use Symfony\Component\Messenger\Handler\MessageSubscriberInterface; +use Symfony\Component\Messenger\Attribute\AsMessageHandler; -class TestMessageHandler implements MessageSubscriberInterface +#[AsMessageHandler] +class TestMessageHandler { - public static function getHandledMessages(): iterable - { - yield TestMessage::class => []; - } - public function __invoke(TestMessage $message) { //do nothing for now