diff --git a/src/Silex/Application.php b/src/Silex/Application.php index 19a8170ea..65fdbb882 100644 --- a/src/Silex/Application.php +++ b/src/Silex/Application.php @@ -339,7 +339,12 @@ public function after($callback, $priority = 0) return; } - call_user_func($app['callback_resolver']->resolveCallback($callback), $event->getRequest(), $event->getResponse(), $app); + $response = call_user_func($app['callback_resolver']->resolveCallback($callback), $event->getRequest(), $event->getResponse(), $app); + if ($response instanceof Response) { + $event->setResponse($response); + } elseif (null !== $response) { + throw new \RuntimeException('An after middleware returned an invalid response value. Must return null or an instance of Response.'); + } }, $priority); } diff --git a/tests/Silex/Tests/MiddlewareTest.php b/tests/Silex/Tests/MiddlewareTest.php index e50a37205..b2c073b92 100644 --- a/tests/Silex/Tests/MiddlewareTest.php +++ b/tests/Silex/Tests/MiddlewareTest.php @@ -234,6 +234,20 @@ public function testAfterFilterAccessRequestResponse() $this->assertEquals('foo---', $app->handle($request)->getContent()); } + public function testAfterFilterCanReturnResponse() + { + $app = new Application(); + + $app->after(function (Request $request, Response $response) { + return new Response('bar'); + }); + + $app->match('/', function () { return new Response('foo'); }); + + $request = Request::create('/'); + $this->assertEquals('bar', $app->handle($request)->getContent()); + } + public function testRouteAndApplicationMiddlewareParameterInjection() { $app = new Application();