From df4d3b5fa937df1785382a99dd0932022e15095d Mon Sep 17 00:00:00 2001 From: Ferdinand Kuhl Date: Thu, 5 Jan 2023 18:40:02 +0100 Subject: [PATCH] Allow multiple "AsMessageHandler" attributes --- Classes/HandlersLocatorFactory.php | 35 ++++++++++++++++-------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/Classes/HandlersLocatorFactory.php b/Classes/HandlersLocatorFactory.php index f3d4a7f..a4f3ba5 100644 --- a/Classes/HandlersLocatorFactory.php +++ b/Classes/HandlersLocatorFactory.php @@ -54,23 +54,26 @@ 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']; + /** @var AsMessageHandler[] $annotations */ + $annotations = $this->reflectionService->getClassAnnotations($asHandlerClass, AsMessageHandler::class); + foreach ($annotations as $annotation) { + $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; + } + $handler = $this->objectManager->get($asHandlerClass); + $handlerDescriptors[$messageName][] = new HandlerDescriptor( + $this->objectManager->get($asHandlerClass), + $config + ); } - 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?