Skip to content

Commit 3dc0df7

Browse files
authored
Dispatch before / after events in ExecuteCommand (#53)
* Dispatch before / after events * Add dispatcher bridge
1 parent dec507f commit 3dc0df7

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/Command/ExecuteCommand.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
use Symfony\Component\Console\Input\InputInterface;
1717
use Symfony\Component\Console\Output\ConsoleOutputInterface;
1818
use Symfony\Component\Console\Output\OutputInterface;
19+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
20+
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
21+
use Task\Event\Events;
22+
use Task\Event\TaskEvent;
23+
use Task\Event\TaskExecutionEvent;
1924
use Task\Executor\FailedException;
2025
use Task\Handler\TaskHandlerFactoryInterface;
2126
use Task\Storage\TaskExecutionRepositoryInterface;
@@ -35,6 +40,11 @@ class ExecuteCommand extends Command
3540
*/
3641
private $executionRepository;
3742

43+
/**
44+
* @var EventDispatcherInterface
45+
*/
46+
private $eventDispatcher;
47+
3848
/**
3949
* @param string $name
4050
* @param TaskHandlerFactoryInterface $handlerFactory
@@ -43,12 +53,14 @@ class ExecuteCommand extends Command
4353
public function __construct(
4454
$name,
4555
TaskHandlerFactoryInterface $handlerFactory,
46-
TaskExecutionRepositoryInterface $executionRepository
56+
TaskExecutionRepositoryInterface $executionRepository,
57+
EventDispatcherInterface $dispatcher
4758
) {
4859
parent::__construct($name);
4960

5061
$this->handlerFactory = $handlerFactory;
5162
$this->executionRepository = $executionRepository;
63+
$this->eventDispatcher = $dispatcher;
5264
}
5365

5466
/**
@@ -70,7 +82,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
7082
$handler = $this->handlerFactory->create($execution->getHandlerClass());
7183

7284
try {
85+
$this->dispatch(Events::TASK_BEFORE, new TaskEvent($execution->getTask()));
7386
$result = $handler->handle($execution->getWorkload());
87+
$this->dispatch(Events::TASK_AFTER, new TaskExecutionEvent($execution->getTask(), $execution));
7488
} catch (\Exception $exception) {
7589
if ($exception instanceof FailedException) {
7690
$errorOutput->writeln(FailedException::class);
@@ -95,4 +109,13 @@ public function isHidden()
95109
{
96110
return true;
97111
}
112+
113+
private function dispatch($eventName, $event)
114+
{
115+
if (class_exists(LegacyEventDispatcherProxy::class)) {
116+
return $this->eventDispatcher->dispatch($event, $eventName);
117+
} else {
118+
return $this->eventDispatcher->dispatch($eventName, $event);
119+
}
120+
}
98121
}

src/Resources/config/command.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<argument type="string">task:execute</argument>
2424
<argument type="service" id="task.handler.factory"/>
2525
<argument type="service" id="task.storage.task_execution"/>
26+
<argument type="service" id="event_dispatcher"/>
2627

2728
<tag name="console.command"/>
2829
</service>

0 commit comments

Comments
 (0)