diff --git a/src/Illuminate/Foundation/Console/Kernel.php b/src/Illuminate/Foundation/Console/Kernel.php index 16fc49d87141..eb9405ba873b 100644 --- a/src/Illuminate/Foundation/Console/Kernel.php +++ b/src/Illuminate/Foundation/Console/Kernel.php @@ -14,6 +14,7 @@ use Illuminate\Contracts\Debug\ExceptionHandler; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Foundation\Application; +use Illuminate\Foundation\Events\Terminating; use Illuminate\Support\Arr; use Illuminate\Support\Carbon; use Illuminate\Support\Env; @@ -212,6 +213,8 @@ public function handle($input, $output = null) */ public function terminate($input, $status) { + $this->events->dispatch(new Terminating); + $this->app->terminate(); if ($this->commandStartedAt === null) { diff --git a/src/Illuminate/Foundation/Events/Terminating.php b/src/Illuminate/Foundation/Events/Terminating.php new file mode 100644 index 000000000000..a74a21e01220 --- /dev/null +++ b/src/Illuminate/Foundation/Events/Terminating.php @@ -0,0 +1,8 @@ +app['events']->dispatch(new Terminating); + $this->terminateMiddleware($request, $response); $this->app->terminate(); diff --git a/tests/Foundation/Console/KernelTest.php b/tests/Foundation/Console/KernelTest.php new file mode 100644 index 000000000000..7ef493b010f8 --- /dev/null +++ b/tests/Foundation/Console/KernelTest.php @@ -0,0 +1,35 @@ +instance('events', $events); + $kernel = new Kernel($app, $events); + $events->listen(function (Terminating $terminating) use (&$called) { + $called[] = 'terminating event'; + }); + $app->terminating(function () use (&$called) { + $called[] = 'terminating callback'; + }); + + $kernel->terminate(new StringInput('tinker'), 0); + + $this->assertSame([ + 'terminating event', + 'terminating callback', + ], $called); + } +} diff --git a/tests/Foundation/Http/KernelTest.php b/tests/Foundation/Http/KernelTest.php index 4b411b24a642..0b1c117ea075 100644 --- a/tests/Foundation/Http/KernelTest.php +++ b/tests/Foundation/Http/KernelTest.php @@ -4,7 +4,10 @@ use Illuminate\Events\Dispatcher; use Illuminate\Foundation\Application; +use Illuminate\Foundation\Events\Terminating; use Illuminate\Foundation\Http\Kernel; +use Illuminate\Http\Request; +use Illuminate\Http\Response; use Illuminate\Routing\Router; use PHPUnit\Framework\TestCase; @@ -43,6 +46,49 @@ public function testGetMiddlewarePriority() ], $kernel->getMiddlewarePriority()); } + public function testItTriggersTerminatingEvent() + { + $called = []; + $app = $this->getApplication(); + $events = new Dispatcher($app); + $app->instance('events', $events); + $kernel = new Kernel($app, $this->getRouter()); + $app->instance('terminating-middleware', new class($called) + { + public function __construct(private &$called) + { + // + } + + public function handle($request, $next) + { + return $next($response); + } + + public function terminate($request, $response) + { + $this->called[] = 'terminating middleware'; + } + }); + $kernel->setGlobalMiddleware([ + 'terminating-middleware', + ]); + $events->listen(function (Terminating $terminating) use (&$called) { + $called[] = 'terminating event'; + }); + $app->terminating(function () use (&$called) { + $called[] = 'terminating callback'; + }); + + $kernel->terminate(new Request(), new Response()); + + $this->assertSame([ + 'terminating event', + 'terminating middleware', + 'terminating callback', + ], $called); + } + /** * @return \Illuminate\Contracts\Foundation\Application */