Skip to content

Commit 6884010

Browse files
committed
Dispatch before / after events
1 parent dec507f commit 6884010

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/Command/ExecuteCommand.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
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 Task\Event\Events;
21+
use Task\Event\TaskEvent;
1922
use Task\Executor\FailedException;
2023
use Task\Handler\TaskHandlerFactoryInterface;
2124
use Task\Storage\TaskExecutionRepositoryInterface;
@@ -35,6 +38,11 @@ class ExecuteCommand extends Command
3538
*/
3639
private $executionRepository;
3740

41+
/**
42+
* @var EventDispatcherInterface
43+
*/
44+
private $eventDispatcher;
45+
3846
/**
3947
* @param string $name
4048
* @param TaskHandlerFactoryInterface $handlerFactory
@@ -43,12 +51,14 @@ class ExecuteCommand extends Command
4351
public function __construct(
4452
$name,
4553
TaskHandlerFactoryInterface $handlerFactory,
46-
TaskExecutionRepositoryInterface $executionRepository
54+
TaskExecutionRepositoryInterface $executionRepository,
55+
EventDispatcherInterface $dispatcher
4756
) {
4857
parent::__construct($name);
4958

5059
$this->handlerFactory = $handlerFactory;
5160
$this->executionRepository = $executionRepository;
61+
$this->eventDispatcher = $dispatcher;
5262
}
5363

5464
/**
@@ -70,7 +80,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
7080
$handler = $this->handlerFactory->create($execution->getHandlerClass());
7181

7282
try {
83+
$this->eventDispatcher->dispatch(new TaskEvent($execution->getTask()),Events::TASK_BEFORE);
7384
$result = $handler->handle($execution->getWorkload());
85+
$this->eventDispatcher->dispatch(new TaskEvent($execution->getTask()),Events::TASK_BEFORE);
7486
} catch (\Exception $exception) {
7587
if ($exception instanceof FailedException) {
7688
$errorOutput->writeln(FailedException::class);

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)