From ad7be9cd02025ab8c9b550cdfddd09321085141f Mon Sep 17 00:00:00 2001 From: Joey Smith Date: Sat, 21 Feb 2026 02:22:23 -0600 Subject: [PATCH 1/2] cs-fix changes Signed-off-by: Joey Smith --- src/.gitkeep | 0 src/ConfigProvider.php | 13 +++++++-- .../EventDispatcherAwareDelegator.php | 17 +++++++++--- .../EventDispatcherInterfaceFactory.php | 15 ++++++++++- .../ListenerProviderAggregateFactory.php | 18 +++++++++++-- src/EventDispatcherAwareInterface.php | 10 +++++++ src/EventDispatcherAwareTrait.php | 10 +++++++ src/EventInterface.php | 10 +++++++ src/ImmutableEvent.php | 27 ++++++++++++------- src/ImmutableEventInterface.php | 10 +++++++ src/ListenerInterface.php | 10 +++++++ src/Middleware/PostHandleMiddleware.php | 19 ++++++++++--- src/Middleware/PreHandleMiddleware.php | 19 ++++++++++--- src/MutableEvent.php | 25 +++++++++++------ src/MutableEventInterface.php | 10 +++++++ src/PostHandleEvent.php | 10 +++++++ src/PreHandleEvent.php | 10 +++++++ 17 files changed, 200 insertions(+), 33 deletions(-) delete mode 100644 src/.gitkeep diff --git a/src/.gitkeep b/src/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index 9dd75e0..a8b4305 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -2,9 +2,18 @@ declare(strict_types=1); +/** + * This file is part of the Webware Commandbus Event package. + * + * Copyright (c) 2026 Joey (aka Tyrsson) Smith + * and contributors. + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Webware\CommandBus\Event; -use Laminas\ServiceManager\Factory\InvokableFactory; use Phly\EventDispatcher\EventDispatcher; use Phly\EventDispatcher\ListenerProvider\ListenerProviderAggregate; use Psr\EventDispatcher\EventDispatcherInterface; @@ -33,7 +42,7 @@ final class ConfigProvider public function __invoke(): array { return [ - 'dependencies' => $this->getDependencies(), + 'dependencies' => $this->getDependencies(), CommandBusInterface::class => [ BusProvider::MIDDLEWARE_PIPELINE_KEY => $this->getPipeline(), ], diff --git a/src/Container/EventDispatcherAwareDelegator.php b/src/Container/EventDispatcherAwareDelegator.php index 6487253..1edcba2 100644 --- a/src/Container/EventDispatcherAwareDelegator.php +++ b/src/Container/EventDispatcherAwareDelegator.php @@ -2,6 +2,16 @@ declare(strict_types=1); +/** + * This file is part of the Webware Commandbus Event package. + * + * Copyright (c) 2026 Joey (aka Tyrsson) Smith + * and contributors. + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Webware\CommandBus\Event\Container; use Psr\Container\ContainerInterface; @@ -13,9 +23,9 @@ final class EventDispatcherAwareDelegator { public function __invoke( - ContainerInterface $container, - string $requestedName, - callable $callback + ContainerInterface $container, + string $requestedName, + callable $callback, ): object { $serviceInstance = $callback(); if (! $serviceInstance instanceof EventDispatcherAwareInterface) { @@ -24,6 +34,7 @@ public function __invoke( $eventDispatcher = $container->get(EventDispatcherInterface::class); assert($eventDispatcher instanceof EventDispatcherInterface); $serviceInstance->setEventDispatcher($eventDispatcher); + return $serviceInstance; } } diff --git a/src/Container/EventDispatcherInterfaceFactory.php b/src/Container/EventDispatcherInterfaceFactory.php index 5821603..480ff29 100644 --- a/src/Container/EventDispatcherInterfaceFactory.php +++ b/src/Container/EventDispatcherInterfaceFactory.php @@ -2,6 +2,16 @@ declare(strict_types=1); +/** + * This file is part of the Webware Commandbus Event package. + * + * Copyright (c) 2026 Joey (aka Tyrsson) Smith + * and contributors. + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Webware\CommandBus\Event\Container; use League\Event\EventDispatcher; @@ -12,7 +22,9 @@ use function assert; -/** @internal */ +/** + * @internal + */ final class EventDispatcherFactory { public function __invoke(ContainerInterface $container): EventDispatcherInterface @@ -25,6 +37,7 @@ public function __invoke(ContainerInterface $container): EventDispatcherInterfac /** @var array $config */ $config = $container->get('config'); + /** @var class-string[] $subscribers */ $subscribers = $config[ConfigKey::Subscribers->value] ?? []; diff --git a/src/Container/ListenerProviderAggregateFactory.php b/src/Container/ListenerProviderAggregateFactory.php index 84af3a0..a250aae 100644 --- a/src/Container/ListenerProviderAggregateFactory.php +++ b/src/Container/ListenerProviderAggregateFactory.php @@ -2,6 +2,16 @@ declare(strict_types=1); +/** + * This file is part of the Webware Commandbus Event package. + * + * Copyright (c) 2026 Joey (aka Tyrsson) Smith + * and contributors. + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Webware\CommandBus\Event\Container; use Phly\EventDispatcher\ListenerProvider\AttachableListenerProvider; @@ -9,9 +19,10 @@ use Phly\EventDispatcher\ListenerProvider\PrioritizedListenerProvider; use Psr\Container\ContainerInterface; use Psr\EventDispatcher\ListenerProviderInterface; -use Webware\CommandBus\Event\ListenerInterface; -/** @internal */ +/** + * @internal + */ final class ListenerProviderAggregateFactory { public function __invoke(ContainerInterface $container): ListenerProviderAggregate @@ -28,6 +39,7 @@ public function __invoke(ContainerInterface $container): ListenerProviderAggrega } else { $attachableProvider->listen($listener['event'], $listener['listener']); } + continue; } @@ -35,6 +47,7 @@ public function __invoke(ContainerInterface $container): ListenerProviderAggrega $listener = $container->has($listener) ? $container->get($listener) : null; if ($listener instanceof ListenerProviderInterface) { $aggregate->attach($listener); + continue; } } @@ -42,6 +55,7 @@ public function __invoke(ContainerInterface $container): ListenerProviderAggrega $aggregate->attach($prioritizedProvider); $aggregate->attach($attachableProvider); + return $aggregate; } } diff --git a/src/EventDispatcherAwareInterface.php b/src/EventDispatcherAwareInterface.php index eb7b6f2..5bc1dad 100644 --- a/src/EventDispatcherAwareInterface.php +++ b/src/EventDispatcherAwareInterface.php @@ -2,6 +2,16 @@ declare(strict_types=1); +/** + * This file is part of the Webware Commandbus Event package. + * + * Copyright (c) 2026 Joey (aka Tyrsson) Smith + * and contributors. + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Webware\CommandBus\Event; use Psr\EventDispatcher\EventDispatcherInterface; diff --git a/src/EventDispatcherAwareTrait.php b/src/EventDispatcherAwareTrait.php index e0933a8..276aaac 100644 --- a/src/EventDispatcherAwareTrait.php +++ b/src/EventDispatcherAwareTrait.php @@ -2,6 +2,16 @@ declare(strict_types=1); +/** + * This file is part of the Webware Commandbus Event package. + * + * Copyright (c) 2026 Joey (aka Tyrsson) Smith + * and contributors. + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Webware\CommandBus\Event; use Psr\EventDispatcher\EventDispatcherInterface; diff --git a/src/EventInterface.php b/src/EventInterface.php index 1f34091..a31fdbe 100644 --- a/src/EventInterface.php +++ b/src/EventInterface.php @@ -2,6 +2,16 @@ declare(strict_types=1); +/** + * This file is part of the Webware Commandbus Event package. + * + * Copyright (c) 2026 Joey (aka Tyrsson) Smith + * and contributors. + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Webware\CommandBus\Event; interface EventInterface diff --git a/src/ImmutableEvent.php b/src/ImmutableEvent.php index 59e324e..f2c1742 100644 --- a/src/ImmutableEvent.php +++ b/src/ImmutableEvent.php @@ -2,6 +2,16 @@ declare(strict_types=1); +/** + * This file is part of the Webware Commandbus Event package. + * + * Copyright (c) 2026 Joey (aka Tyrsson) Smith + * and contributors. + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Webware\CommandBus\Event; use Override; @@ -15,34 +25,33 @@ public function __construct( private ?string $name = self::class, private ?object $target = null, private ?array $params = null, - ) { - } + ) {} - #[Override] + #[Override()] public function getName(): string { return $this->eventName(); } - #[Override] + #[Override()] public function eventName(): string { return $this->name ?? self::class; } - #[Override] + #[Override()] public function withName(string $name): self { return new self($name, $this->target, $this->params); } - #[Override] + #[Override()] public function getTarget(): ?object { return $this->target; } - #[Override] + #[Override()] public function withTarget(object $target): self { return new self($this->name, $target, $this->params); @@ -51,7 +60,7 @@ public function withTarget(object $target): self /** * @return array */ - #[Override] + #[Override()] public function getParams(): array { return $this->params ?? []; @@ -60,7 +69,7 @@ public function getParams(): array /** * @param array $params */ - #[Override] + #[Override()] public function withParams(array $params): self { return new self($this->name, $this->target, $params); diff --git a/src/ImmutableEventInterface.php b/src/ImmutableEventInterface.php index c1f85c5..aafc280 100644 --- a/src/ImmutableEventInterface.php +++ b/src/ImmutableEventInterface.php @@ -2,6 +2,16 @@ declare(strict_types=1); +/** + * This file is part of the Webware Commandbus Event package. + * + * Copyright (c) 2026 Joey (aka Tyrsson) Smith + * and contributors. + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Webware\CommandBus\Event; interface ImmutableEventInterface extends EventInterface diff --git a/src/ListenerInterface.php b/src/ListenerInterface.php index 193606a..9df7782 100644 --- a/src/ListenerInterface.php +++ b/src/ListenerInterface.php @@ -2,6 +2,16 @@ declare(strict_types=1); +/** + * This file is part of the Webware Commandbus Event package. + * + * Copyright (c) 2026 Joey (aka Tyrsson) Smith + * and contributors. + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Webware\CommandBus\Event; interface ListenerInterface diff --git a/src/Middleware/PostHandleMiddleware.php b/src/Middleware/PostHandleMiddleware.php index 8e21ae4..7f18aff 100644 --- a/src/Middleware/PostHandleMiddleware.php +++ b/src/Middleware/PostHandleMiddleware.php @@ -2,33 +2,44 @@ declare(strict_types=1); +/** + * This file is part of the Webware Commandbus Event package. + * + * Copyright (c) 2026 Joey (aka Tyrsson) Smith + * and contributors. + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Webware\CommandBus\Event\Middleware; use Override; use Webware\CommandBus\Command\CommandResultInterface; use Webware\CommandBus\CommandHandlerInterface; use Webware\CommandBus\CommandInterface; -use Webware\CommandBus\Event\PostHandleEvent; use Webware\CommandBus\Event\EventDispatcherAwareInterface; use Webware\CommandBus\Event\EventDispatcherAwareTrait; +use Webware\CommandBus\Event\PostHandleEvent; use Webware\CommandBus\MiddlewareInterface; final readonly class PostHandleMiddleware implements MiddlewareInterface, EventDispatcherAwareInterface { use EventDispatcherAwareTrait; - #[Override] + #[Override()] public function process( CommandInterface $command, - CommandHandlerInterface $handler + CommandHandlerInterface $handler, ): CommandResultInterface { // Custom processing logic for this middleware if ($command instanceof CommandResultInterface) { $this->getEventDispatcher()->dispatch(new PostHandleEvent($command)); + // Return the CommandResult return $command; } - + return $handler->handle($command); } } diff --git a/src/Middleware/PreHandleMiddleware.php b/src/Middleware/PreHandleMiddleware.php index d3126fe..6eea359 100644 --- a/src/Middleware/PreHandleMiddleware.php +++ b/src/Middleware/PreHandleMiddleware.php @@ -2,28 +2,39 @@ declare(strict_types=1); +/** + * This file is part of the Webware Commandbus Event package. + * + * Copyright (c) 2026 Joey (aka Tyrsson) Smith + * and contributors. + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Webware\CommandBus\Event\Middleware; use Override; use Webware\CommandBus\Command\CommandResultInterface; use Webware\CommandBus\CommandHandlerInterface; use Webware\CommandBus\CommandInterface; -use Webware\CommandBus\Event\PreHandleEvent; use Webware\CommandBus\Event\EventDispatcherAwareInterface; -use Webware\CommandBus\MiddlewareInterface; use Webware\CommandBus\Event\EventDispatcherAwareTrait; +use Webware\CommandBus\Event\PreHandleEvent; +use Webware\CommandBus\MiddlewareInterface; final readonly class PreHandleMiddleware implements MiddlewareInterface, EventDispatcherAwareInterface { use EventDispatcherAwareTrait; - #[Override] + #[Override()] public function process( CommandInterface $command, - CommandHandlerInterface $handler + CommandHandlerInterface $handler, ): CommandResultInterface { // Let'em know $this->getEventDispatcher()->dispatch(new PreHandleEvent($command)); + return $handler->handle($command); } } diff --git a/src/MutableEvent.php b/src/MutableEvent.php index ab837ce..669040c 100644 --- a/src/MutableEvent.php +++ b/src/MutableEvent.php @@ -2,6 +2,16 @@ declare(strict_types=1); +/** + * This file is part of the Webware Commandbus Event package. + * + * Copyright (c) 2026 Joey (aka Tyrsson) Smith + * and contributors. + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Webware\CommandBus\Event; use Override; @@ -15,40 +25,39 @@ public function __construct( private ?string $name = null, private ?object $target = null, private ?array $params = null, - ) { - } + ) {} - #[Override] + #[Override()] public function getName(): string { return $this->name ?? static::class; } - #[Override] + #[Override()] public function setName(string $name): void { $this->name = $name; } - #[Override] + #[Override()] public function getTarget(): ?object { return $this->target; } - #[Override] + #[Override()] public function setTarget(object $target): void { $this->target = $target; } - #[Override] + #[Override()] public function getParams(): array { return $this->params ?? []; } - #[Override] + #[Override()] public function setParams(array $params): void { $this->params = $params; diff --git a/src/MutableEventInterface.php b/src/MutableEventInterface.php index 51379f9..26072d8 100644 --- a/src/MutableEventInterface.php +++ b/src/MutableEventInterface.php @@ -2,6 +2,16 @@ declare(strict_types=1); +/** + * This file is part of the Webware Commandbus Event package. + * + * Copyright (c) 2026 Joey (aka Tyrsson) Smith + * and contributors. + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Webware\CommandBus\Event; interface MutableEventInterface extends EventInterface diff --git a/src/PostHandleEvent.php b/src/PostHandleEvent.php index 5aa8858..0bbfffc 100644 --- a/src/PostHandleEvent.php +++ b/src/PostHandleEvent.php @@ -2,6 +2,16 @@ declare(strict_types=1); +/** + * This file is part of the Webware Commandbus Event package. + * + * Copyright (c) 2026 Joey (aka Tyrsson) Smith + * and contributors. + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Webware\CommandBus\Event; use Webware\CommandBus\CommandInterface; diff --git a/src/PreHandleEvent.php b/src/PreHandleEvent.php index 4423bdf..d7118ff 100644 --- a/src/PreHandleEvent.php +++ b/src/PreHandleEvent.php @@ -2,6 +2,16 @@ declare(strict_types=1); +/** + * This file is part of the Webware Commandbus Event package. + * + * Copyright (c) 2026 Joey (aka Tyrsson) Smith + * and contributors. + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Webware\CommandBus\Event; use Webware\CommandBus\CommandInterface; From 8a6dab9269fc77e28a4af6b7f0c462e2cfe91eb2 Mon Sep 17 00:00:00 2001 From: Joey Smith Date: Sun, 22 Feb 2026 00:46:05 -0600 Subject: [PATCH 2/2] Adds 10 array issues to phpstan baseline Signed-off-by: Joey Smith --- composer.lock | 12 ++-- phpstan-baseline.neon | 55 ++++++++++++++++++ phpstan.neon.dist | 2 + src/ConfigProvider.php | 14 ++++- .../EventDispatcherInterfaceFactory.php | 55 ------------------ .../ListenerProviderAggregateFactory.php | 58 ++++++++++++++----- src/EventDispatcherAwareTrait.php | 2 +- src/ImmutableEvent.php | 18 ++---- src/Middleware/PostHandleMiddleware.php | 4 +- src/Middleware/PreHandleMiddleware.php | 4 +- src/MutableEvent.php | 12 ++-- 11 files changed, 137 insertions(+), 99 deletions(-) create mode 100644 phpstan-baseline.neon delete mode 100644 src/Container/EventDispatcherInterfaceFactory.php diff --git a/composer.lock b/composer.lock index 9b7252b..de44060 100644 --- a/composer.lock +++ b/composer.lock @@ -5653,16 +5653,16 @@ }, { "name": "webware/coding-standard", - "version": "0.1.0", + "version": "0.1.1", "source": { "type": "git", "url": "https://github.com/tyrsson/coding-standard.git", - "reference": "f11071f4e1208d7ae1057cb1edbaa6fbc9e1cd8f" + "reference": "ce1abd755e33400857e3b98dd6b3cf94fa6b494e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/tyrsson/coding-standard/zipball/f11071f4e1208d7ae1057cb1edbaa6fbc9e1cd8f", - "reference": "f11071f4e1208d7ae1057cb1edbaa6fbc9e1cd8f", + "url": "https://api.github.com/repos/tyrsson/coding-standard/zipball/ce1abd755e33400857e3b98dd6b3cf94fa6b494e", + "reference": "ce1abd755e33400857e3b98dd6b3cf94fa6b494e", "shasum": "" }, "require": { @@ -5698,9 +5698,9 @@ ], "support": { "issues": "https://github.com/tyrsson/coding-standard/issues", - "source": "https://github.com/tyrsson/coding-standard/tree/0.1.0" + "source": "https://github.com/tyrsson/coding-standard/tree/0.1.1" }, - "time": "2026-02-13T11:06:22+00:00" + "time": "2026-02-22T04:13:47+00:00" }, { "name": "webware/command-bus", diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon new file mode 100644 index 0000000..31a32d2 --- /dev/null +++ b/phpstan-baseline.neon @@ -0,0 +1,55 @@ +parameters: + ignoreErrors: + - + message: '#^Method Webware\\CommandBus\\Event\\ConfigProvider\:\:__invoke\(\) should return array\{dependencies\: array\{aliases\: array\, delegators\: array\\>, factories\: array\\}, Webware\\CommandBus\\CommandBusInterface\: non\-empty\-array\\>\} but returns array\{dependencies\: array, Webware\\CommandBus\\CommandBusInterface\: array\{middleware_pipeline\: array\{list\\}\}\}\.$#' + identifier: return.type + count: 1 + path: src/ConfigProvider.php + + - + message: '#^Method Webware\\CommandBus\\Event\\ConfigProvider\:\:getDependencies\(\) return type has no value type specified in iterable type array\.$#' + identifier: missingType.iterableValue + count: 1 + path: src/ConfigProvider.php + + - + message: '#^Method Webware\\CommandBus\\Event\\ConfigProvider\:\:getPipeline\(\) should return array\{list\\} but returns array\{array\{middleware\: ''Webware\\\\CommandBus\\\\Event\\\\Middleware\\\\PreHandleMiddleware'', priority\: 100\}, array\{middleware\: ''Webware\\\\CommandBus\\\\Event\\\\Middleware\\\\PostHandleMiddleware'', priority\: \-100\}\}\.$#' + identifier: return.type + count: 1 + path: src/ConfigProvider.php + + - + message: '#^Method Webware\\CommandBus\\Event\\Container\\EventDispatcherAwareDelegator\:\:__invoke\(\) should return object but returns mixed\.$#' + identifier: return.type + count: 1 + path: src/Container/EventDispatcherAwareDelegator.php + + - + message: '#^Parameter \#1 \$id of method Psr\\Container\\ContainerInterface\:\:get\(\) expects string, mixed given\.$#' + identifier: argument.type + count: 1 + path: src/Container/ListenerProviderAggregateFactory.php + + - + message: '#^Parameter \#1 \$id of method Psr\\Container\\ContainerInterface\:\:has\(\) expects string, mixed given\.$#' + identifier: argument.type + count: 1 + path: src/Container/ListenerProviderAggregateFactory.php + + - + message: '#^Parameter \#2 \$listener of method Phly\\EventDispatcher\\ListenerProvider\\AttachableListenerProvider\:\:listen\(\) expects callable\(\)\: mixed, mixed given\.$#' + identifier: argument.type + count: 2 + path: src/Container/ListenerProviderAggregateFactory.php + + - + message: '#^Parameter \#2 \$listener of method Phly\\EventDispatcher\\ListenerProvider\\PrioritizedListenerProvider\:\:listen\(\) expects callable\(\)\: mixed, mixed given\.$#' + identifier: argument.type + count: 1 + path: src/Container/ListenerProviderAggregateFactory.php + + - + message: '#^Parameter \#3 \$priority of method Phly\\EventDispatcher\\ListenerProvider\\PrioritizedListenerProvider\:\:listen\(\) expects int, mixed given\.$#' + identifier: argument.type + count: 1 + path: src/Container/ListenerProviderAggregateFactory.php diff --git a/phpstan.neon.dist b/phpstan.neon.dist index a355a56..72c79b9 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,3 +1,5 @@ +includes: + - phpstan-baseline.neon parameters: level: 10 paths: diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index a8b4305..6b6c97f 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -23,11 +23,14 @@ final class ConfigProvider { - public const CONFIG_KEY = 'listeners'; + public const LISTENER_KEY = 'listeners'; + + public const LISTENER_PROVIDER_KEY = 'listener_providers'; /** * @phpstan-return array{ * dependencies: array{ + * aliases: array, * delegators: array>, * factories: array * }, @@ -46,6 +49,7 @@ public function __invoke(): array CommandBusInterface::class => [ BusProvider::MIDDLEWARE_PIPELINE_KEY => $this->getPipeline(), ], + // self::LISTENER_KEY => $this->getListeners(), ]; } @@ -62,6 +66,9 @@ private function getDependencies(): array ]; } + /** + * @return array{list} + */ private function getPipeline(): array { return [ @@ -75,4 +82,9 @@ private function getPipeline(): array ], ]; } + + // private function getListeners(): array + // { + // return []; + // } } diff --git a/src/Container/EventDispatcherInterfaceFactory.php b/src/Container/EventDispatcherInterfaceFactory.php deleted file mode 100644 index 480ff29..0000000 --- a/src/Container/EventDispatcherInterfaceFactory.php +++ /dev/null @@ -1,55 +0,0 @@ - - * and contributors. - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Webware\CommandBus\Event\Container; - -use League\Event\EventDispatcher; -use League\Event\ListenerSubscriber as ListenerSubscriberInterface; -use Psr\Container\ContainerInterface; -use Psr\EventDispatcher\EventDispatcherInterface; -use Webware\Event\ConfigKey; - -use function assert; - -/** - * @internal - */ -final class EventDispatcherFactory -{ - public function __invoke(ContainerInterface $container): EventDispatcherInterface - { - $listenerSubscriber = $container->get(ListenerSubscriberInterface::class); - assert($listenerSubscriber instanceof ListenerSubscriberInterface); - - $dispatcher = new EventDispatcher(); - $dispatcher->subscribeListenersFrom($listenerSubscriber); - - /** @var array $config */ - $config = $container->get('config'); - - /** @var class-string[] $subscribers */ - $subscribers = $config[ConfigKey::Subscribers->value] ?? []; - - foreach ($subscribers as $subscriber) { - if ($container->has($subscriber)) { - $instance = $container->get($subscriber); - if ($instance instanceof ListenerSubscriberInterface) { - $dispatcher->subscribeListenersFrom($instance); - } - } - } - - return $dispatcher; - } -} diff --git a/src/Container/ListenerProviderAggregateFactory.php b/src/Container/ListenerProviderAggregateFactory.php index a250aae..0ad9f3c 100644 --- a/src/Container/ListenerProviderAggregateFactory.php +++ b/src/Container/ListenerProviderAggregateFactory.php @@ -19,6 +19,11 @@ use Phly\EventDispatcher\ListenerProvider\PrioritizedListenerProvider; use Psr\Container\ContainerInterface; use Psr\EventDispatcher\ListenerProviderInterface; +use Webware\CommandBus\Event\ConfigProvider; + +use function is_array; +use function is_callable; +use function is_string; /** * @internal @@ -27,32 +32,57 @@ final class ListenerProviderAggregateFactory { public function __invoke(ContainerInterface $container): ListenerProviderAggregate { - // todo: need some defense here - $listeners = $container->get('config')['listeners'] ?? []; + /** @var array{listeners?: array>, listener_providers?: array} $config */ + $config = $container->get('config'); + $listeners = $config[ConfigProvider::LISTENER_KEY] ?? []; + $listenerProviders = $config[ConfigProvider::LISTENER_PROVIDER_KEY] ?? []; $prioritizedProvider = $container->get(PrioritizedListenerProvider::class); $attachableProvider = $container->get(AttachableListenerProvider::class); $aggregate = new ListenerProviderAggregate(); - foreach ($listeners as $listener) { - if (is_array($listener)) { - if (isset($listener['listener'], $listener['priority'])) { - $prioritizedProvider->listen($listener['event'], $listener['listener'], $listener['priority']); - } else { - $attachableProvider->listen($listener['event'], $listener['listener']); + + /** @var string $eventType */ + /** @var array $spec */ + foreach ($listeners as $eventType => $spec) { + foreach ($spec as $listener) { + if (is_string($listener)) { + if ($container->has($listener)) { + $attachableProvider->listen($eventType, $container->get($listener)); + } elseif (is_callable($listener)) { + $attachableProvider->listen($eventType, $listener); + } + + continue; } - continue; - } + if (is_array($listener)) { + $resolvedListener = null; + if ($container->has($listener['listener'])) { + $resolvedListener = $container->get($listener['listener']); + } elseif (is_callable($listener['listener'])) { + $resolvedListener = $listener['listener']; + } else { + continue; + } - if (is_string($listener)) { - $listener = $container->has($listener) ? $container->get($listener) : null; - if ($listener instanceof ListenerProviderInterface) { - $aggregate->attach($listener); + if ($listener['priority']) { + $prioritizedProvider->listen($eventType, $resolvedListener, $listener['priority']); + } else { + $attachableProvider->listen($eventType, $resolvedListener); + } continue; } } } + /** @var string $provider */ + foreach ($listenerProviders as $provider) { + $providerInstance = $container->has($provider) ? $container->get($provider) : null; + if ($providerInstance instanceof ListenerProviderInterface) { + $aggregate->attach($providerInstance); + } + } + $aggregate->attach($prioritizedProvider); $aggregate->attach($attachableProvider); diff --git a/src/EventDispatcherAwareTrait.php b/src/EventDispatcherAwareTrait.php index 276aaac..340a583 100644 --- a/src/EventDispatcherAwareTrait.php +++ b/src/EventDispatcherAwareTrait.php @@ -21,7 +21,7 @@ */ trait EventDispatcherAwareTrait { - private readonly EventDispatcherInterface $eventDispatcher; + protected EventDispatcherInterface $eventDispatcher; public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void { diff --git a/src/ImmutableEvent.php b/src/ImmutableEvent.php index f2c1742..8992c81 100644 --- a/src/ImmutableEvent.php +++ b/src/ImmutableEvent.php @@ -27,31 +27,25 @@ public function __construct( private ?array $params = null, ) {} - #[Override()] + #[Override] public function getName(): string - { - return $this->eventName(); - } - - #[Override()] - public function eventName(): string { return $this->name ?? self::class; } - #[Override()] + #[Override] public function withName(string $name): self { return new self($name, $this->target, $this->params); } - #[Override()] + #[Override] public function getTarget(): ?object { return $this->target; } - #[Override()] + #[Override] public function withTarget(object $target): self { return new self($this->name, $target, $this->params); @@ -60,7 +54,7 @@ public function withTarget(object $target): self /** * @return array */ - #[Override()] + #[Override] public function getParams(): array { return $this->params ?? []; @@ -69,7 +63,7 @@ public function getParams(): array /** * @param array $params */ - #[Override()] + #[Override] public function withParams(array $params): self { return new self($this->name, $this->target, $params); diff --git a/src/Middleware/PostHandleMiddleware.php b/src/Middleware/PostHandleMiddleware.php index 7f18aff..a54c38d 100644 --- a/src/Middleware/PostHandleMiddleware.php +++ b/src/Middleware/PostHandleMiddleware.php @@ -23,11 +23,11 @@ use Webware\CommandBus\Event\PostHandleEvent; use Webware\CommandBus\MiddlewareInterface; -final readonly class PostHandleMiddleware implements MiddlewareInterface, EventDispatcherAwareInterface +final class PostHandleMiddleware implements MiddlewareInterface, EventDispatcherAwareInterface { use EventDispatcherAwareTrait; - #[Override()] + #[Override] public function process( CommandInterface $command, CommandHandlerInterface $handler, diff --git a/src/Middleware/PreHandleMiddleware.php b/src/Middleware/PreHandleMiddleware.php index 6eea359..e1500b7 100644 --- a/src/Middleware/PreHandleMiddleware.php +++ b/src/Middleware/PreHandleMiddleware.php @@ -23,11 +23,11 @@ use Webware\CommandBus\Event\PreHandleEvent; use Webware\CommandBus\MiddlewareInterface; -final readonly class PreHandleMiddleware implements MiddlewareInterface, EventDispatcherAwareInterface +final class PreHandleMiddleware implements MiddlewareInterface, EventDispatcherAwareInterface { use EventDispatcherAwareTrait; - #[Override()] + #[Override] public function process( CommandInterface $command, CommandHandlerInterface $handler, diff --git a/src/MutableEvent.php b/src/MutableEvent.php index 669040c..82ebbba 100644 --- a/src/MutableEvent.php +++ b/src/MutableEvent.php @@ -27,37 +27,37 @@ public function __construct( private ?array $params = null, ) {} - #[Override()] + #[Override] public function getName(): string { return $this->name ?? static::class; } - #[Override()] + #[Override] public function setName(string $name): void { $this->name = $name; } - #[Override()] + #[Override] public function getTarget(): ?object { return $this->target; } - #[Override()] + #[Override] public function setTarget(object $target): void { $this->target = $target; } - #[Override()] + #[Override] public function getParams(): array { return $this->params ?? []; } - #[Override()] + #[Override] public function setParams(array $params): void { $this->params = $params;