From eba0ba48016fbccc7c629cb4750bca767b5c8f39 Mon Sep 17 00:00:00 2001 From: Prokyonn Date: Tue, 19 Oct 2021 11:24:44 +0200 Subject: [PATCH 1/2] Dispatch before / after events --- src/Command/ExecuteCommand.php | 14 +++++++++++++- src/Resources/config/command.xml | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Command/ExecuteCommand.php b/src/Command/ExecuteCommand.php index e45fea3..7268e36 100644 --- a/src/Command/ExecuteCommand.php +++ b/src/Command/ExecuteCommand.php @@ -16,6 +16,9 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\ConsoleOutputInterface; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Task\Event\Events; +use Task\Event\TaskEvent; use Task\Executor\FailedException; use Task\Handler\TaskHandlerFactoryInterface; use Task\Storage\TaskExecutionRepositoryInterface; @@ -35,6 +38,11 @@ class ExecuteCommand extends Command */ private $executionRepository; + /** + * @var EventDispatcherInterface + */ + private $eventDispatcher; + /** * @param string $name * @param TaskHandlerFactoryInterface $handlerFactory @@ -43,12 +51,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 +80,9 @@ protected function execute(InputInterface $input, OutputInterface $output) $handler = $this->handlerFactory->create($execution->getHandlerClass()); try { + $this->eventDispatcher->dispatch(new TaskEvent($execution->getTask()),Events::TASK_BEFORE); $result = $handler->handle($execution->getWorkload()); + $this->eventDispatcher->dispatch(new TaskEvent($execution),Events::TASK_AFTER); } catch (\Exception $exception) { if ($exception instanceof FailedException) { $errorOutput->writeln(FailedException::class); 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 + From 0df93bc50a5d44bf2060cddee66ad048972131ea Mon Sep 17 00:00:00 2001 From: Prokyonn Date: Tue, 19 Oct 2021 13:06:09 +0200 Subject: [PATCH 2/2] Add dispatcher bridge --- src/Command/ExecuteCommand.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Command/ExecuteCommand.php b/src/Command/ExecuteCommand.php index 7268e36..e3b0286 100644 --- a/src/Command/ExecuteCommand.php +++ b/src/Command/ExecuteCommand.php @@ -17,8 +17,10 @@ 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; @@ -80,9 +82,9 @@ protected function execute(InputInterface $input, OutputInterface $output) $handler = $this->handlerFactory->create($execution->getHandlerClass()); try { - $this->eventDispatcher->dispatch(new TaskEvent($execution->getTask()),Events::TASK_BEFORE); + $this->dispatch(Events::TASK_BEFORE, new TaskEvent($execution->getTask())); $result = $handler->handle($execution->getWorkload()); - $this->eventDispatcher->dispatch(new TaskEvent($execution),Events::TASK_AFTER); + $this->dispatch(Events::TASK_AFTER, new TaskExecutionEvent($execution->getTask(), $execution)); } catch (\Exception $exception) { if ($exception instanceof FailedException) { $errorOutput->writeln(FailedException::class); @@ -107,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); + } + } }