From a3d8eb9b822fa7d8bd56761cb652e4b64e3076fa Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Thu, 25 Jul 2024 15:58:13 +1000 Subject: [PATCH] Adds terminating event --- src/Illuminate/Foundation/Console/Kernel.php | 2 + .../Foundation/Events/Terminating.php | 8 ++++ src/Illuminate/Foundation/Http/Kernel.php | 3 ++ tests/Foundation/Console/KernelTest.php | 35 ++++++++++++++ tests/Foundation/Http/KernelTest.php | 46 +++++++++++++++++++ 5 files changed, 94 insertions(+) create mode 100644 src/Illuminate/Foundation/Events/Terminating.php create mode 100644 tests/Foundation/Console/KernelTest.php diff --git a/src/Illuminate/Foundation/Console/Kernel.php b/src/Illuminate/Foundation/Console/Kernel.php index 16fc49d87141..6c49e519759f 100644 --- a/src/Illuminate/Foundation/Console/Kernel.php +++ b/src/Illuminate/Foundation/Console/Kernel.php @@ -212,6 +212,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 */