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;