public function handle(CommandInterface $command) { $commandName = $this->getCommandName($command); if (!$this->commandHandlers->has($commandName)) { throw new Exception\RuntimeException(sprintf('No handler registered for command "%s"', $commandName)); } $preEventParams = array(CommandDispatcherEvent::PARAM_COMMAND_NAME => $commandName, CommandDispatcherEvent::PARAM_COMMAND => $command); $events = $this->getEventManager(); $preEvent = $this->prepareEvent(CommandDispatcherEvent::EVENT_PRE_HANDLE, $preEventParams); $events->trigger($preEvent, function ($result) { // Don't handle the command when a listener returns false return $result === false; }); $commandHandler = $this->commandHandlers->get($commandName); $commandHandlerResult = $commandHandler->handle($command); $postEventParams = array_merge($preEventParams, array(CommandDispatcherEvent::PARAM_RESULT => $commandHandlerResult)); $postEvent = $this->prepareEvent(CommandDispatcherEvent::EVENT_HANDLE, $postEventParams); $events->trigger($postEvent); return $commandHandlerResult; }
/** * @param CommandInterface $command * @return mixed */ public function dispatch(CommandInterface $command) { $commandName = $this->getCommandName($command); if (!$this->commandHandlers->has($commandName)) { throw new Exception\RuntimeException(sprintf('No handler registered for command "%s"', $commandName)); } $preEventParams = array(CommandDispatcherEvent::PARAM_COMMAND_NAME => $commandName, CommandDispatcherEvent::PARAM_COMMAND => $command); $continueDispatch = $this->triggerPreDispatchEvent(CommandDispatcherEvent::EVENT_PRE_DISPATCH, $preEventParams); /** @todo Remove when we can break backwards compatibility */ if ($continueDispatch) { $continueDispatch = $this->triggerPreDispatchEvent(CommandDispatcherEvent::EVENT_PRE_HANDLE, $preEventParams); } if (!$continueDispatch) { /** @todo Should probably trigger an abort event */ return null; } $commandHandler = $this->commandHandlers->get($commandName); $commandHandlerResult = $commandHandler->handle($command); $postEventParams = array_merge($preEventParams, array(CommandDispatcherEvent::PARAM_RESULT => $commandHandlerResult)); $this->triggerPostDispatchEvent(CommandDispatcherEvent::EVENT_DISPATCH, $postEventParams); /** @todo Remove when we can break backwards compatibility */ $this->triggerPostDispatchEvent(CommandDispatcherEvent::EVENT_HANDLE, $postEventParams); return $commandHandlerResult; }