Skip to content

Commit 59b822c

Browse files
timacdonaldnunomadurotaylorotwell
authored
[10.x] Wrap response preparation in events (#47229)
* 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>
1 parent 8e71667 commit 59b822c

File tree

4 files changed

+111
-3
lines changed

4 files changed

+111
-3
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Illuminate\Routing\Events;
4+
5+
class PreparingResponse
6+
{
7+
/**
8+
* The request instance.
9+
*
10+
* @var \Symfony\Component\HttpFoundation\Request
11+
*/
12+
public $request;
13+
14+
/**
15+
* The response instance.
16+
*
17+
* @var mixed
18+
*/
19+
public $response;
20+
21+
/**
22+
* Create a new event instance.
23+
*
24+
* @param \Symfony\Component\HttpFoundation\Request $request
25+
* @param mixed $response
26+
* @return void
27+
*/
28+
public function __construct($request, $response)
29+
{
30+
$this->request = $request;
31+
$this->response = $response;
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Illuminate\Routing\Events;
4+
5+
class ResponsePrepared
6+
{
7+
/**
8+
* The request instance.
9+
*
10+
* @var \Symfony\Component\HttpFoundation\Request
11+
*/
12+
public $request;
13+
14+
/**
15+
* The response instance.
16+
*
17+
* @var \Symfony\Component\HttpFoundation\Response
18+
*/
19+
public $response;
20+
21+
/**
22+
* Create a new event instance.
23+
*
24+
* @param \Symfony\Component\HttpFoundation\Request $request
25+
* @param \Symfony\Component\HttpFoundation\Response $response
26+
* @return void
27+
*/
28+
public function __construct($request, $response)
29+
{
30+
$this->request = $request;
31+
$this->response = $response;
32+
}
33+
}

src/Illuminate/Routing/Router.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Illuminate\Http\JsonResponse;
1616
use Illuminate\Http\Request;
1717
use Illuminate\Http\Response;
18+
use Illuminate\Routing\Events\PreparingResponse;
19+
use Illuminate\Routing\Events\ResponsePrepared;
1820
use Illuminate\Routing\Events\RouteMatched;
1921
use Illuminate\Routing\Events\Routing;
2022
use Illuminate\Support\Arr;
@@ -871,7 +873,11 @@ protected function sortMiddleware(Collection $middlewares)
871873
*/
872874
public function prepareResponse($request, $response)
873875
{
874-
return static::toResponse($request, $response);
876+
$this->events->dispatch(new PreparingResponse($request, $response));
877+
878+
return tap(static::toResponse($request, $response), function ($response) use ($request) {
879+
$this->events->dispatch(new ResponsePrepared($request, $response));
880+
});
875881
}
876882

877883
/**

tests/Routing/RoutingRouteTest.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
use Illuminate\Routing\Contracts\ControllerDispatcher as ControllerDispatcherContract;
2424
use Illuminate\Routing\Controller;
2525
use Illuminate\Routing\ControllerDispatcher;
26+
use Illuminate\Routing\Events\PreparingResponse;
27+
use Illuminate\Routing\Events\ResponsePrepared;
2628
use Illuminate\Routing\Events\Routing;
2729
use Illuminate\Routing\Exceptions\UrlGenerationException;
2830
use Illuminate\Routing\Middleware\SubstituteBindings;
@@ -2169,11 +2171,45 @@ public function testRouteCanMiddlewareCanBeAssigned()
21692171
], $route->middleware());
21702172
}
21712173

2172-
protected function getRouter()
2174+
public function testItDispatchesEventsWhilePreparingRequest()
21732175
{
2176+
$events = new Dispatcher;
2177+
$preparing = [];
2178+
$prepared = [];
2179+
$events->listen(PreparingResponse::class, function ($event) use (&$preparing) {
2180+
$preparing[] = $event;
2181+
});
2182+
$events->listen(ResponsePrepared::class, function ($event) use (&$prepared) {
2183+
$prepared[] = $event;
2184+
});
21742185
$container = new Container;
2186+
$container->instance(Dispatcher::class, $events);
2187+
$router = $this->getRouter($container);
2188+
$router->get('foo/bar', function () {
2189+
return 'hello';
2190+
});
2191+
$request = Request::create('foo/bar', 'GET');
21752192

2176-
$router = new Router(new Dispatcher, $container);
2193+
$response = $router->dispatch($request);
2194+
2195+
$this->assertSame('hello', $response->getContent());
2196+
$this->assertCount(2, $preparing);
2197+
$this->assertSame($request, $preparing[0]->request);
2198+
$this->assertSame('hello', $preparing[0]->response);
2199+
$this->assertSame($request, $preparing[1]->request);
2200+
$this->assertSame($response, $preparing[1]->response);
2201+
$this->assertCount(2, $prepared);
2202+
$this->assertSame($request, $prepared[0]->request);
2203+
$this->assertSame($response, $prepared[0]->response);
2204+
$this->assertSame($request, $prepared[1]->request);
2205+
$this->assertSame($response, $prepared[1]->response);
2206+
}
2207+
2208+
protected function getRouter($container = null)
2209+
{
2210+
$container ??= new Container;
2211+
2212+
$router = new Router($container->make(Dispatcher::class), $container);
21772213

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

0 commit comments

Comments
 (0)