3
3
namespace Laravel \Lumen \Console ;
4
4
5
5
use Illuminate \Console \Application as Artisan ;
6
+ use Illuminate \Console \Events \CommandFinished ;
7
+ use Illuminate \Console \Events \CommandStarting ;
6
8
use Illuminate \Console \Scheduling \Schedule ;
7
9
use Illuminate \Console \Scheduling \ScheduleRunCommand ;
8
10
use Illuminate \Contracts \Console \Kernel as KernelContract ;
9
11
use Illuminate \Contracts \Debug \ExceptionHandler ;
12
+ use Illuminate \Contracts \Events \Dispatcher ;
10
13
use Illuminate \Http \Request ;
11
14
use Laravel \Lumen \Application ;
12
15
use Laravel \Lumen \Exceptions \Handler ;
13
16
use RuntimeException ;
17
+ use Symfony \Component \Console \ConsoleEvents ;
18
+ use Symfony \Component \Console \Event \ConsoleCommandEvent ;
19
+ use Symfony \Component \Console \Event \ConsoleTerminateEvent ;
20
+ use Symfony \Component \EventDispatcher \EventDispatcher ;
14
21
use Throwable ;
15
22
16
23
class Kernel implements KernelContract
@@ -22,6 +29,13 @@ class Kernel implements KernelContract
22
29
*/
23
30
protected $ app ;
24
31
32
+ /**
33
+ * The Symfony event dispatcher implementation.
34
+ *
35
+ * @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface|null
36
+ */
37
+ protected $ symfonyDispatcher ;
38
+
25
39
/**
26
40
* The Artisan application instance.
27
41
*
@@ -55,6 +69,8 @@ public function __construct(Application $app)
55
69
56
70
if ($ this ->app ->runningInConsole ()) {
57
71
$ this ->setRequestForConsole ($ this ->app );
72
+ } else {
73
+ $ this ->rerouteSymfonyCommandEvents ();
58
74
}
59
75
60
76
$ this ->app ->prepareForConsoleCommand ($ this ->aliases );
@@ -87,6 +103,34 @@ protected function setRequestForConsole(Application $app)
87
103
));
88
104
}
89
105
106
+ /**
107
+ * Re-route the Symfony command events to their Laravel counterparts.
108
+ *
109
+ * @internal
110
+ *
111
+ * @return $this
112
+ */
113
+ public function rerouteSymfonyCommandEvents ()
114
+ {
115
+ if (is_null ($ this ->symfonyDispatcher )) {
116
+ $ this ->symfonyDispatcher = new EventDispatcher ;
117
+
118
+ $ this ->symfonyDispatcher ->addListener (ConsoleEvents::COMMAND , function (ConsoleCommandEvent $ event ) {
119
+ $ this ->app [Dispatcher::class]->dispatch (
120
+ new CommandStarting ($ event ->getCommand ()->getName (), $ event ->getInput (), $ event ->getOutput ())
121
+ );
122
+ });
123
+
124
+ $ this ->symfonyDispatcher ->addListener (ConsoleEvents::TERMINATE , function (ConsoleTerminateEvent $ event ) {
125
+ $ this ->app [Dispatcher::class]->dispatch (
126
+ new CommandFinished ($ event ->getCommand ()->getName (), $ event ->getInput (), $ event ->getOutput (), $ event ->getExitCode ())
127
+ );
128
+ });
129
+ }
130
+
131
+ return $ this ;
132
+ }
133
+
90
134
/**
91
135
* Define the application's command schedule.
92
136
*
@@ -212,9 +256,14 @@ public function output()
212
256
protected function getArtisan ()
213
257
{
214
258
if (is_null ($ this ->artisan )) {
215
- return $ this ->artisan = (new Artisan ($ this ->app , $ this ->app ->make ('events ' ), $ this ->app ->version ()))
259
+ $ this ->artisan = (new Artisan ($ this ->app , $ this ->app ->make ('events ' ), $ this ->app ->version ()))
216
260
->resolveCommands ($ this ->getCommands ())
217
261
->setContainerCommandLoader ();
262
+
263
+ if ($ this ->symfonyDispatcher instanceof EventDispatcher) {
264
+ $ this ->artisan ->setDispatcher ($ this ->symfonyDispatcher );
265
+ $ this ->artisan ->setSignalsToDispatchEvent ();
266
+ }
218
267
}
219
268
220
269
return $ this ->artisan ;
0 commit comments