Skip to content

Commit

Permalink
[10.x] Wrap response preparation in events (#47229)
Browse files Browse the repository at this point in the history
* wip

* Update ResponsePrepared.php

* Update ResponsePrepared.php

* Update PreparingResponse.php

---------

Co-authored-by: Nuno Maduro <enunomaduro@gmail.com>
Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
3 people committed May 30, 2023
1 parent 8e71667 commit 59b822c
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 3 deletions.
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

0 comments on commit 59b822c

Please sign in to comment.