Skip to content

Commit

Permalink
Adds terminating event (#52259)
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald authored Jul 26, 2024
1 parent 810a834 commit c71cdf7
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Illuminate/Foundation/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions src/Illuminate/Foundation/Events/Terminating.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Illuminate\Foundation\Events;

class Terminating
{
//
}
3 changes: 3 additions & 0 deletions src/Illuminate/Foundation/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Http\Kernel as KernelContract;
use Illuminate\Foundation\Events\Terminating;
use Illuminate\Foundation\Http\Events\RequestHandled;
use Illuminate\Routing\Pipeline;
use Illuminate\Routing\Router;
Expand Down Expand Up @@ -210,6 +211,8 @@ protected function dispatchToRouter()
*/
public function terminate($request, $response)
{
$this->app['events']->dispatch(new Terminating);

$this->terminateMiddleware($request, $response);

$this->app->terminate();
Expand Down
35 changes: 35 additions & 0 deletions tests/Foundation/Console/KernelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Illuminate\Tests\Foundation\Console;

use Illuminate\Events\Dispatcher;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Console\Kernel;
use Illuminate\Foundation\Events\Terminating;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\StringInput;

class KernelTest extends TestCase
{
public function testItDispatchesTerminatingEvent()
{
$called = [];
$app = new Application;
$events = new Dispatcher($app);
$app->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);
}
}
46 changes: 46 additions & 0 deletions tests/Foundation/Http/KernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit c71cdf7

Please sign in to comment.