Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Wrap response preparation in events #47229

Merged
merged 4 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/Illuminate/Routing/Events/PreparingResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Illuminate\Routing\Events;

class PreparingResponse
{
/**
* The request instance.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
public $request;

/**
* The response instance.
*
* @var mixed
*/
public $response;

/**
* Create a new event instance.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @param mixed $response
* @return void
*/
public function __construct($request, $response)
{
$this->request = $request;
$this->response = $response;
}
}
33 changes: 33 additions & 0 deletions src/Illuminate/Routing/Events/ResponsePrepared.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Illuminate\Routing\Events;

class ResponsePrepared
{
/**
* The request instance.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
public $request;

/**
* The response instance.
*
* @var \Symfony\Component\HttpFoundation\Response
*/
public $response;

/**
* Create a new event instance.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @param \Symfony\Component\HttpFoundation\Response $response
* @return void
*/
public function __construct($request, $response)
{
$this->request = $request;
$this->response = $response;
}
}
8 changes: 7 additions & 1 deletion src/Illuminate/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Events\PreparingResponse;
use Illuminate\Routing\Events\ResponsePrepared;
use Illuminate\Routing\Events\RouteMatched;
use Illuminate\Routing\Events\Routing;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -871,7 +873,11 @@ protected function sortMiddleware(Collection $middlewares)
*/
public function prepareResponse($request, $response)
{
return static::toResponse($request, $response);
$this->events->dispatch(new PreparingResponse($request, $response));

return tap(static::toResponse($request, $response), function ($response) use ($request) {
$this->events->dispatch(new ResponsePrepared($request, $response));
});
}

/**
Expand Down
40 changes: 38 additions & 2 deletions tests/Routing/RoutingRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
use Illuminate\Routing\Contracts\ControllerDispatcher as ControllerDispatcherContract;
use Illuminate\Routing\Controller;
use Illuminate\Routing\ControllerDispatcher;
use Illuminate\Routing\Events\PreparingResponse;
use Illuminate\Routing\Events\ResponsePrepared;
use Illuminate\Routing\Events\Routing;
use Illuminate\Routing\Exceptions\UrlGenerationException;
use Illuminate\Routing\Middleware\SubstituteBindings;
Expand Down Expand Up @@ -2169,11 +2171,45 @@ public function testRouteCanMiddlewareCanBeAssigned()
], $route->middleware());
}

protected function getRouter()
public function testItDispatchesEventsWhilePreparingRequest()
{
$events = new Dispatcher;
$preparing = [];
$prepared = [];
$events->listen(PreparingResponse::class, function ($event) use (&$preparing) {
$preparing[] = $event;
});
$events->listen(ResponsePrepared::class, function ($event) use (&$prepared) {
$prepared[] = $event;
});
$container = new Container;
$container->instance(Dispatcher::class, $events);
$router = $this->getRouter($container);
$router->get('foo/bar', function () {
return 'hello';
});
$request = Request::create('foo/bar', 'GET');

$router = new Router(new Dispatcher, $container);
$response = $router->dispatch($request);

$this->assertSame('hello', $response->getContent());
$this->assertCount(2, $preparing);
$this->assertSame($request, $preparing[0]->request);
$this->assertSame('hello', $preparing[0]->response);
$this->assertSame($request, $preparing[1]->request);
$this->assertSame($response, $preparing[1]->response);
$this->assertCount(2, $prepared);
$this->assertSame($request, $prepared[0]->request);
$this->assertSame($response, $prepared[0]->response);
$this->assertSame($request, $prepared[1]->request);
$this->assertSame($response, $prepared[1]->response);
}

protected function getRouter($container = null)
{
$container ??= new Container;

$router = new Router($container->make(Dispatcher::class), $container);

$container->singleton(Registrar::class, function () use ($router) {
return $router;
Expand Down