16
16
use Symfony \Component \Console \Input \InputInterface ;
17
17
use Symfony \Component \Console \Output \ConsoleOutputInterface ;
18
18
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 ;
19
24
use Task \Executor \FailedException ;
20
25
use Task \Handler \TaskHandlerFactoryInterface ;
21
26
use Task \Storage \TaskExecutionRepositoryInterface ;
@@ -35,6 +40,11 @@ class ExecuteCommand extends Command
35
40
*/
36
41
private $ executionRepository ;
37
42
43
+ /**
44
+ * @var EventDispatcherInterface
45
+ */
46
+ private $ eventDispatcher ;
47
+
38
48
/**
39
49
* @param string $name
40
50
* @param TaskHandlerFactoryInterface $handlerFactory
@@ -43,12 +53,14 @@ class ExecuteCommand extends Command
43
53
public function __construct (
44
54
$ name ,
45
55
TaskHandlerFactoryInterface $ handlerFactory ,
46
- TaskExecutionRepositoryInterface $ executionRepository
56
+ TaskExecutionRepositoryInterface $ executionRepository ,
57
+ EventDispatcherInterface $ dispatcher
47
58
) {
48
59
parent ::__construct ($ name );
49
60
50
61
$ this ->handlerFactory = $ handlerFactory ;
51
62
$ this ->executionRepository = $ executionRepository ;
63
+ $ this ->eventDispatcher = $ dispatcher ;
52
64
}
53
65
54
66
/**
@@ -70,7 +82,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
70
82
$ handler = $ this ->handlerFactory ->create ($ execution ->getHandlerClass ());
71
83
72
84
try {
85
+ $ this ->dispatch (Events::TASK_BEFORE , new TaskEvent ($ execution ->getTask ()));
73
86
$ result = $ handler ->handle ($ execution ->getWorkload ());
87
+ $ this ->dispatch (Events::TASK_AFTER , new TaskExecutionEvent ($ execution ->getTask (), $ execution ));
74
88
} catch (\Exception $ exception ) {
75
89
if ($ exception instanceof FailedException) {
76
90
$ errorOutput ->writeln (FailedException::class);
@@ -95,4 +109,13 @@ public function isHidden()
95
109
{
96
110
return true ;
97
111
}
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
+ }
98
121
}
0 commit comments