@@ -16,15 +16,15 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | interface HandlerProvider |
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * @param object $message |
|
| 21 | - * A message for which to return the relevant handler. |
|
| 22 | - * |
|
| 23 | - * @return callable |
|
| 24 | - * A callable that MUST be type-compatible with $message. |
|
| 25 | - * |
|
| 26 | - * @throws NotFound |
|
| 27 | - * The handler for the message cannot the found. |
|
| 28 | - */ |
|
| 29 | - public function getHandlerForMessage(object $message): callable; |
|
| 19 | + /** |
|
| 20 | + * @param object $message |
|
| 21 | + * A message for which to return the relevant handler. |
|
| 22 | + * |
|
| 23 | + * @return callable |
|
| 24 | + * A callable that MUST be type-compatible with $message. |
|
| 25 | + * |
|
| 26 | + * @throws NotFound |
|
| 27 | + * The handler for the message cannot the found. |
|
| 28 | + */ |
|
| 29 | + public function getHandlerForMessage(object $message): callable; |
|
| 30 | 30 | } |
@@ -15,8 +15,8 @@ |
||
| 15 | 15 | |
| 16 | 16 | class QueryDispatcher extends SimpleDispatcher |
| 17 | 17 | { |
| 18 | - public function __construct(QueryHandlerProvider $handlerProvider) |
|
| 19 | - { |
|
| 20 | - parent::__construct($handlerProvider); |
|
| 21 | - } |
|
| 18 | + public function __construct(QueryHandlerProvider $handlerProvider) |
|
| 19 | + { |
|
| 20 | + parent::__construct($handlerProvider); |
|
| 21 | + } |
|
| 22 | 22 | } |
@@ -15,8 +15,8 @@ |
||
| 15 | 15 | |
| 16 | 16 | class CommandDispatcher extends SimpleDispatcher |
| 17 | 17 | { |
| 18 | - public function __construct(CommandHandlerProvider $handlerProvider) |
|
| 19 | - { |
|
| 20 | - parent::__construct($handlerProvider); |
|
| 21 | - } |
|
| 18 | + public function __construct(CommandHandlerProvider $handlerProvider) |
|
| 19 | + { |
|
| 20 | + parent::__construct($handlerProvider); |
|
| 21 | + } |
|
| 22 | 22 | } |
@@ -19,8 +19,8 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | final class NotFound extends LogicException implements Exception |
| 21 | 21 | { |
| 22 | - public function __construct(string $message, Throwable $previous = null) |
|
| 23 | - { |
|
| 24 | - parent::__construct($message, 0, $previous); |
|
| 25 | - } |
|
| 22 | + public function __construct(string $message, Throwable $previous = null) |
|
| 23 | + { |
|
| 24 | + parent::__construct($message, 0, $previous); |
|
| 25 | + } |
|
| 26 | 26 | } |
@@ -16,15 +16,15 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | interface Dispatcher |
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * @param object $message |
|
| 21 | - * The message to dispatch. |
|
| 22 | - * |
|
| 23 | - * @return mixed |
|
| 24 | - * Result type depends on the handler. |
|
| 25 | - * |
|
| 26 | - * @throws NotFound |
|
| 27 | - * The handler for the message cannot the found. |
|
| 28 | - */ |
|
| 29 | - public function dispatch(object $message); |
|
| 19 | + /** |
|
| 20 | + * @param object $message |
|
| 21 | + * The message to dispatch. |
|
| 22 | + * |
|
| 23 | + * @return mixed |
|
| 24 | + * Result type depends on the handler. |
|
| 25 | + * |
|
| 26 | + * @throws NotFound |
|
| 27 | + * The handler for the message cannot the found. |
|
| 28 | + */ |
|
| 29 | + public function dispatch(object $message); |
|
| 30 | 30 | } |
@@ -26,17 +26,17 @@ |
||
| 26 | 26 | */ |
| 27 | 27 | final class QueryHandlerProviderPass extends HandlerProviderPass |
| 28 | 28 | { |
| 29 | - public const DEFAULT_SERVICE_ID = QueryHandlerProvider::class; |
|
| 30 | - public const DEFAULT_HANDLER_TAG = 'query_dispatcher.handler'; |
|
| 31 | - public const DEFAULT_MESSAGE_PROPERTY = 'query'; |
|
| 32 | - public const DEFAULT_PROVIDER_CLASS = QueryHandlerProvider::class; |
|
| 29 | + public const DEFAULT_SERVICE_ID = QueryHandlerProvider::class; |
|
| 30 | + public const DEFAULT_HANDLER_TAG = 'query_dispatcher.handler'; |
|
| 31 | + public const DEFAULT_MESSAGE_PROPERTY = 'query'; |
|
| 32 | + public const DEFAULT_PROVIDER_CLASS = QueryHandlerProvider::class; |
|
| 33 | 33 | |
| 34 | - public function __construct( |
|
| 35 | - string $serviceId = self::DEFAULT_SERVICE_ID, |
|
| 36 | - string $handlerTag = self::DEFAULT_HANDLER_TAG, |
|
| 37 | - string $messageProperty = self::DEFAULT_MESSAGE_PROPERTY, |
|
| 38 | - string $providerClass = self::DEFAULT_PROVIDER_CLASS |
|
| 39 | - ) { |
|
| 40 | - parent::__construct($serviceId, $handlerTag, $messageProperty, $providerClass); |
|
| 41 | - } |
|
| 34 | + public function __construct( |
|
| 35 | + string $serviceId = self::DEFAULT_SERVICE_ID, |
|
| 36 | + string $handlerTag = self::DEFAULT_HANDLER_TAG, |
|
| 37 | + string $messageProperty = self::DEFAULT_MESSAGE_PROPERTY, |
|
| 38 | + string $providerClass = self::DEFAULT_PROVIDER_CLASS |
|
| 39 | + ) { |
|
| 40 | + parent::__construct($serviceId, $handlerTag, $messageProperty, $providerClass); |
|
| 41 | + } |
|
| 42 | 42 | } |
@@ -26,17 +26,17 @@ |
||
| 26 | 26 | */ |
| 27 | 27 | final class CommandHandlerProviderPass extends HandlerProviderPass |
| 28 | 28 | { |
| 29 | - public const DEFAULT_SERVICE_ID = CommandHandlerProvider::class; |
|
| 30 | - public const DEFAULT_HANDLER_TAG = 'command_dispatcher.handler'; |
|
| 31 | - public const DEFAULT_MESSAGE_PROPERTY = 'command'; |
|
| 32 | - public const DEFAULT_PROVIDER_CLASS = CommandHandlerProvider::class; |
|
| 29 | + public const DEFAULT_SERVICE_ID = CommandHandlerProvider::class; |
|
| 30 | + public const DEFAULT_HANDLER_TAG = 'command_dispatcher.handler'; |
|
| 31 | + public const DEFAULT_MESSAGE_PROPERTY = 'command'; |
|
| 32 | + public const DEFAULT_PROVIDER_CLASS = CommandHandlerProvider::class; |
|
| 33 | 33 | |
| 34 | - public function __construct( |
|
| 35 | - string $serviceId = self::DEFAULT_SERVICE_ID, |
|
| 36 | - string $handlerTag = self::DEFAULT_HANDLER_TAG, |
|
| 37 | - string $messageProperty = self::DEFAULT_MESSAGE_PROPERTY, |
|
| 38 | - string $providerClass = self::DEFAULT_PROVIDER_CLASS |
|
| 39 | - ) { |
|
| 40 | - parent::__construct($serviceId, $handlerTag, $messageProperty, $providerClass); |
|
| 41 | - } |
|
| 34 | + public function __construct( |
|
| 35 | + string $serviceId = self::DEFAULT_SERVICE_ID, |
|
| 36 | + string $handlerTag = self::DEFAULT_HANDLER_TAG, |
|
| 37 | + string $messageProperty = self::DEFAULT_MESSAGE_PROPERTY, |
|
| 38 | + string $providerClass = self::DEFAULT_PROVIDER_CLASS |
|
| 39 | + ) { |
|
| 40 | + parent::__construct($serviceId, $handlerTag, $messageProperty, $providerClass); |
|
| 41 | + } |
|
| 42 | 42 | } |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | */ |
| 70 | 70 | public function process(ContainerBuilder $container): void |
| 71 | 71 | { |
| 72 | - [ $mapping, $refMap ] = $this->collectHandlers($container); |
|
| 72 | + [$mapping, $refMap] = $this->collectHandlers($container); |
|
| 73 | 73 | |
| 74 | 74 | $container |
| 75 | 75 | ->register($this->serviceId, $this->providerClass) |
@@ -118,6 +118,6 @@ discard block |
||
| 118 | 118 | $refMap[$id] = new TypedReference($id, $class); |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | - return [ $mapping, $refMap ]; |
|
| 121 | + return [$mapping, $refMap]; |
|
| 122 | 122 | } |
| 123 | 123 | } |
@@ -27,70 +27,70 @@ |
||
| 27 | 27 | */ |
| 28 | 28 | class HandlerProviderPass implements CompilerPassInterface |
| 29 | 29 | { |
| 30 | - public const DEFAULT_SERVICE_ID = HandlerProvider::class; |
|
| 31 | - public const DEFAULT_HANDLER_TAG = 'message_dispatcher.handler'; |
|
| 32 | - public const DEFAULT_MESSAGE_PROPERTY = 'message'; |
|
| 33 | - public const DEFAULT_PROVIDER_CLASS = ContainerHandlerProvider::class; |
|
| 34 | - |
|
| 35 | - public function __construct( |
|
| 36 | - private string $serviceId = self::DEFAULT_SERVICE_ID, |
|
| 37 | - private string $handlerTag = self::DEFAULT_HANDLER_TAG, |
|
| 38 | - private string $messageProperty = self::DEFAULT_MESSAGE_PROPERTY, |
|
| 39 | - private string $providerClass = self::DEFAULT_PROVIDER_CLASS |
|
| 40 | - ) { |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @inheritdoc |
|
| 45 | - */ |
|
| 46 | - public function process(ContainerBuilder $container): void |
|
| 47 | - { |
|
| 48 | - [ $mapping, $refMap ] = $this->collectHandlers($container); |
|
| 49 | - |
|
| 50 | - $container |
|
| 51 | - ->register($this->serviceId, $this->providerClass) |
|
| 52 | - ->setArguments([ |
|
| 53 | - ServiceLocatorTagPass::register($container, $refMap), |
|
| 54 | - $mapping |
|
| 55 | - ]); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * @return array{0: array<string, string>, 1: array<string, TypedReference>} |
|
| 60 | - */ |
|
| 61 | - private function collectHandlers(ContainerBuilder $container): array |
|
| 62 | - { |
|
| 63 | - $handlers = $container->findTaggedServiceIds($this->handlerTag, true); |
|
| 64 | - $messageProperty = $this->messageProperty; |
|
| 65 | - $mapping = []; |
|
| 66 | - $refMap = []; |
|
| 67 | - |
|
| 68 | - foreach ($handlers as $id => $tags) { |
|
| 69 | - assert(is_string($id)); |
|
| 70 | - |
|
| 71 | - $command = $tags[0][$messageProperty] |
|
| 72 | - ?? throw new InvalidArgumentException( |
|
| 73 | - "The `$messageProperty` property is required for service `$id`." |
|
| 74 | - ); |
|
| 75 | - |
|
| 76 | - assert(is_string($command)); |
|
| 77 | - |
|
| 78 | - if (isset($mapping[$command])) { |
|
| 79 | - throw new LogicException( |
|
| 80 | - "The command `$command` already has an handler: `{$mapping[$command]}`." |
|
| 81 | - ); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - $class = $container->getDefinition($id)->getClass(); |
|
| 85 | - |
|
| 86 | - if (!$class) { |
|
| 87 | - throw new LogicException("Unable to get class of service `$id`."); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - $mapping[$command] = $id; |
|
| 91 | - $refMap[$id] = new TypedReference($id, $class); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - return [ $mapping, $refMap ]; |
|
| 95 | - } |
|
| 30 | + public const DEFAULT_SERVICE_ID = HandlerProvider::class; |
|
| 31 | + public const DEFAULT_HANDLER_TAG = 'message_dispatcher.handler'; |
|
| 32 | + public const DEFAULT_MESSAGE_PROPERTY = 'message'; |
|
| 33 | + public const DEFAULT_PROVIDER_CLASS = ContainerHandlerProvider::class; |
|
| 34 | + |
|
| 35 | + public function __construct( |
|
| 36 | + private string $serviceId = self::DEFAULT_SERVICE_ID, |
|
| 37 | + private string $handlerTag = self::DEFAULT_HANDLER_TAG, |
|
| 38 | + private string $messageProperty = self::DEFAULT_MESSAGE_PROPERTY, |
|
| 39 | + private string $providerClass = self::DEFAULT_PROVIDER_CLASS |
|
| 40 | + ) { |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @inheritdoc |
|
| 45 | + */ |
|
| 46 | + public function process(ContainerBuilder $container): void |
|
| 47 | + { |
|
| 48 | + [ $mapping, $refMap ] = $this->collectHandlers($container); |
|
| 49 | + |
|
| 50 | + $container |
|
| 51 | + ->register($this->serviceId, $this->providerClass) |
|
| 52 | + ->setArguments([ |
|
| 53 | + ServiceLocatorTagPass::register($container, $refMap), |
|
| 54 | + $mapping |
|
| 55 | + ]); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * @return array{0: array<string, string>, 1: array<string, TypedReference>} |
|
| 60 | + */ |
|
| 61 | + private function collectHandlers(ContainerBuilder $container): array |
|
| 62 | + { |
|
| 63 | + $handlers = $container->findTaggedServiceIds($this->handlerTag, true); |
|
| 64 | + $messageProperty = $this->messageProperty; |
|
| 65 | + $mapping = []; |
|
| 66 | + $refMap = []; |
|
| 67 | + |
|
| 68 | + foreach ($handlers as $id => $tags) { |
|
| 69 | + assert(is_string($id)); |
|
| 70 | + |
|
| 71 | + $command = $tags[0][$messageProperty] |
|
| 72 | + ?? throw new InvalidArgumentException( |
|
| 73 | + "The `$messageProperty` property is required for service `$id`." |
|
| 74 | + ); |
|
| 75 | + |
|
| 76 | + assert(is_string($command)); |
|
| 77 | + |
|
| 78 | + if (isset($mapping[$command])) { |
|
| 79 | + throw new LogicException( |
|
| 80 | + "The command `$command` already has an handler: `{$mapping[$command]}`." |
|
| 81 | + ); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + $class = $container->getDefinition($id)->getClass(); |
|
| 85 | + |
|
| 86 | + if (!$class) { |
|
| 87 | + throw new LogicException("Unable to get class of service `$id`."); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + $mapping[$command] = $id; |
|
| 91 | + $refMap[$id] = new TypedReference($id, $class); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + return [ $mapping, $refMap ]; |
|
| 95 | + } |
|
| 96 | 96 | } |
@@ -16,13 +16,13 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class SimpleDispatcher implements Dispatcher |
| 18 | 18 | { |
| 19 | - public function __construct( |
|
| 20 | - private HandlerProvider $handlerProvider |
|
| 21 | - ) { |
|
| 22 | - } |
|
| 19 | + public function __construct( |
|
| 20 | + private HandlerProvider $handlerProvider |
|
| 21 | + ) { |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - public function dispatch(object $message) |
|
| 25 | - { |
|
| 26 | - return $this->handlerProvider->getHandlerForMessage($message)($message); |
|
| 27 | - } |
|
| 24 | + public function dispatch(object $message) |
|
| 25 | + { |
|
| 26 | + return $this->handlerProvider->getHandlerForMessage($message)($message); |
|
| 27 | + } |
|
| 28 | 28 | } |