From ca6e483f72f950e0f95ad4ac72027b89a82f7cd9 Mon Sep 17 00:00:00 2001 From: Joey Smith Date: Wed, 4 Mar 2026 02:32:49 -0600 Subject: [PATCH] Signed-off-by: Joey Smith --- src/EventAwareInterface.php | 12 ++++++++++++ src/EventAwareTrait.php | 24 ++++++++++++++++++++++++ src/Middleware/PostHandleMiddleware.php | 10 +++++++++- 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 src/EventAwareInterface.php create mode 100644 src/EventAwareTrait.php diff --git a/src/EventAwareInterface.php b/src/EventAwareInterface.php new file mode 100644 index 0000000..6d76b04 --- /dev/null +++ b/src/EventAwareInterface.php @@ -0,0 +1,12 @@ +event; + } + + #[Override] + public function setEvent(EventInterface $event): void + { + $this->event = $event; + } +} diff --git a/src/Middleware/PostHandleMiddleware.php b/src/Middleware/PostHandleMiddleware.php index a54c38d..609b111 100644 --- a/src/Middleware/PostHandleMiddleware.php +++ b/src/Middleware/PostHandleMiddleware.php @@ -18,6 +18,7 @@ use Webware\CommandBus\Command\CommandResultInterface; use Webware\CommandBus\CommandHandlerInterface; use Webware\CommandBus\CommandInterface; +use Webware\CommandBus\Event\EventAwareInterface; use Webware\CommandBus\Event\EventDispatcherAwareInterface; use Webware\CommandBus\Event\EventDispatcherAwareTrait; use Webware\CommandBus\Event\PostHandleEvent; @@ -33,8 +34,15 @@ public function process( CommandHandlerInterface $handler, ): CommandResultInterface { // Custom processing logic for this middleware + $dispatcher = $this->getEventDispatcher(); + + if ($command instanceof EventAwareInterface) { + if ($event = $command->getEvent()) { + $dispatcher->dispatch($event); + } + } if ($command instanceof CommandResultInterface) { - $this->getEventDispatcher()->dispatch(new PostHandleEvent($command)); + $dispatcher->dispatch(new PostHandleEvent($command)); // Return the CommandResult return $command;