Skip to content
This repository has been archived by the owner on Jul 4, 2018. It is now read-only.

Commit

Permalink
Allow global after middlewares to return responses like route specifi…
Browse files Browse the repository at this point in the history
…c ones
  • Loading branch information
naderman committed Jan 7, 2015
1 parent f64ac7b commit d2b5906
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Silex/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
14 changes: 14 additions & 0 deletions tests/Silex/Tests/MiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit d2b5906

Please sign in to comment.