diff --git a/src/Command/ExecuteCommand.php b/src/Command/ExecuteCommand.php index e45fea3..e3b0286 100644 --- a/src/Command/ExecuteCommand.php +++ b/src/Command/ExecuteCommand.php @@ -16,6 +16,11 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\ConsoleOutputInterface; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy; +use Task\Event\Events; +use Task\Event\TaskEvent; +use Task\Event\TaskExecutionEvent; use Task\Executor\FailedException; use Task\Handler\TaskHandlerFactoryInterface; use Task\Storage\TaskExecutionRepositoryInterface; @@ -35,6 +40,11 @@ class ExecuteCommand extends Command */ private $executionRepository; + /** + * @var EventDispatcherInterface + */ + private $eventDispatcher; + /** * @param string $name * @param TaskHandlerFactoryInterface $handlerFactory @@ -43,12 +53,14 @@ class ExecuteCommand extends Command public function __construct( $name, TaskHandlerFactoryInterface $handlerFactory, - TaskExecutionRepositoryInterface $executionRepository + TaskExecutionRepositoryInterface $executionRepository, + EventDispatcherInterface $dispatcher ) { parent::__construct($name); $this->handlerFactory = $handlerFactory; $this->executionRepository = $executionRepository; + $this->eventDispatcher = $dispatcher; } /** @@ -70,7 +82,9 @@ protected function execute(InputInterface $input, OutputInterface $output) $handler = $this->handlerFactory->create($execution->getHandlerClass()); try { + $this->dispatch(Events::TASK_BEFORE, new TaskEvent($execution->getTask())); $result = $handler->handle($execution->getWorkload()); + $this->dispatch(Events::TASK_AFTER, new TaskExecutionEvent($execution->getTask(), $execution)); } catch (\Exception $exception) { if ($exception instanceof FailedException) { $errorOutput->writeln(FailedException::class); @@ -95,4 +109,13 @@ public function isHidden() { return true; } + + private function dispatch($eventName, $event) + { + if (class_exists(LegacyEventDispatcherProxy::class)) { + return $this->eventDispatcher->dispatch($event, $eventName); + } else { + return $this->eventDispatcher->dispatch($eventName, $event); + } + } } diff --git a/src/Resources/config/command.xml b/src/Resources/config/command.xml index 05c71e3..ca2a325 100644 --- a/src/Resources/config/command.xml +++ b/src/Resources/config/command.xml @@ -23,6 +23,7 @@ task:execute +