2021-04-18 22:24:02 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace DigiComp\FlowSymfonyBridge\Messenger;
|
|
|
|
|
|
|
|
use Neos\Flow\Annotations as Flow;
|
|
|
|
use Neos\Flow\ObjectManagement\ObjectManagerInterface;
|
|
|
|
use Neos\Flow\Reflection\ReflectionService;
|
2023-01-05 18:08:46 +01:00
|
|
|
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
|
2021-04-18 22:24:02 +02:00
|
|
|
use Symfony\Component\Messenger\Handler\HandlerDescriptor;
|
|
|
|
use Symfony\Component\Messenger\Handler\HandlersLocator;
|
|
|
|
use Symfony\Component\Messenger\Handler\MessageSubscriberInterface;
|
|
|
|
|
|
|
|
class HandlersLocatorFactory
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @Flow\InjectConfiguration
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $configuration;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Flow\Inject
|
|
|
|
* @var ObjectManagerInterface
|
|
|
|
*/
|
|
|
|
protected $objectManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Flow\Inject
|
|
|
|
* @var ReflectionService
|
|
|
|
*/
|
|
|
|
protected $reflectionService;
|
|
|
|
|
|
|
|
public function create($busName = 'default')
|
|
|
|
{
|
|
|
|
$messageHandlerClasses = $this->reflectionService
|
|
|
|
->getAllImplementationClassNamesForInterface(MessageSubscriberInterface::class);
|
|
|
|
$handlerDescriptors = [];
|
|
|
|
foreach ($messageHandlerClasses as $messageHandlerClass) {
|
|
|
|
foreach ($messageHandlerClass::getHandledMessages() as $messageName => $config) {
|
2022-09-18 13:17:53 +02:00
|
|
|
if (! \is_array($config)) {
|
2021-04-18 22:24:02 +02:00
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
'different from doctrine, we (currently) need subscribers to always have an option array'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (isset($config['bus']) && $config['bus'] !== $busName) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$handlerDescriptors[$messageName][] = new HandlerDescriptor(
|
|
|
|
$this->objectManager->get($messageHandlerClass),
|
|
|
|
$config
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2023-01-05 18:08:46 +01:00
|
|
|
$asHandlerClasses = $this->reflectionService
|
|
|
|
->getClassNamesByAnnotation(AsMessageHandler::class);
|
|
|
|
foreach ($asHandlerClasses as $asHandlerClass) {
|
2023-01-05 18:40:02 +01:00
|
|
|
/** @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
|
|
|
|
);
|
2023-01-05 18:08:46 +01:00
|
|
|
}
|
|
|
|
}
|
2021-04-18 22:24:02 +02:00
|
|
|
// TODO: Maybe we can allow handlers to be added to bus or globally by configuration?
|
|
|
|
|
|
|
|
return new HandlersLocator($handlerDescriptors);
|
|
|
|
}
|
|
|
|
}
|