Commit e40002f0 authored by Fabien Potencier's avatar Fabien Potencier

added Request and Response as arguments to the before and after filters (a bit...

added Request and Response as arguments to the before and after filters (a bit like error() has access to the Exception)
parent d02fd0be
...@@ -354,6 +354,22 @@ through before and after filters. All you need to do is pass a closure:: ...@@ -354,6 +354,22 @@ through before and after filters. All you need to do is pass a closure::
// tear down // tear down
}); });
The before filter has access to the current Request, and can short-circuit the
whole rendering by returning a Response:
$app->before(function (Request $request) {
// redirect the user to the login screen if access to the Resource is protected
if (...) {
return new RedirectResponse('/login');
}
});
The after filter has access to the Request and the Response:
$app->before(function (Request $request, Response $response) {
// tweak the Response
});
.. note:: .. note::
The filters are only run for the "master" Request. The filters are only run for the "master" Request.
......
...@@ -17,12 +17,15 @@ use Symfony\Component\HttpKernel\Event\KernelEvent; ...@@ -17,12 +17,15 @@ use Symfony\Component\HttpKernel\Event\KernelEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent; use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent; use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\EventDispatcher\Event; use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcher;
...@@ -192,7 +195,13 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe ...@@ -192,7 +195,13 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
*/ */
public function before($callback, $priority = 0) public function before($callback, $priority = 0)
{ {
$this['dispatcher']->addListener(SilexEvents::BEFORE, $callback, $priority); $this['dispatcher']->addListener(SilexEvents::BEFORE, function (GetResponseEvent $event) use ($callback) {
$ret = $callback($event->getRequest());
if ($ret instanceof Response) {
$event->setResponse($ret);
}
}, $priority);
} }
/** /**
...@@ -206,7 +215,9 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe ...@@ -206,7 +215,9 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
*/ */
public function after($callback, $priority = 0) public function after($callback, $priority = 0)
{ {
$this['dispatcher']->addListener(SilexEvents::AFTER, $callback, $priority); $this['dispatcher']->addListener(SilexEvents::AFTER, function (FilterResponseEvent $event) use ($callback) {
$callback($event->getRequest(), $event->getResponse());
}, $priority);
} }
/** /**
...@@ -358,7 +369,7 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe ...@@ -358,7 +369,7 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
// make sure onSilexBefore event is dispatched // make sure onSilexBefore event is dispatched
if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) { if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
$this['dispatcher']->dispatch(SilexEvents::BEFORE); $this['dispatcher']->dispatch(SilexEvents::BEFORE, $event);
} }
if ($e instanceof ResourceNotFoundException) { if ($e instanceof ResourceNotFoundException) {
...@@ -373,7 +384,7 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe ...@@ -373,7 +384,7 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
} }
if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) { if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
$this['dispatcher']->dispatch(SilexEvents::BEFORE); $this['dispatcher']->dispatch(SilexEvents::BEFORE, $event);
} }
} }
...@@ -413,7 +424,7 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe ...@@ -413,7 +424,7 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
public function onKernelResponse(Event $event) public function onKernelResponse(Event $event)
{ {
if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) { if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
$this['dispatcher']->dispatch(SilexEvents::AFTER); $this['dispatcher']->dispatch(SilexEvents::AFTER, $event);
} }
} }
......
...@@ -180,4 +180,32 @@ class BeforeAfterFilterTest extends \PHPUnit_Framework_TestCase ...@@ -180,4 +180,32 @@ class BeforeAfterFilterTest extends \PHPUnit_Framework_TestCase
$request = Request::create('/foo/de'); $request = Request::create('/foo/de');
$this->assertEquals('de', $app->handle($request)->getContent()); $this->assertEquals('de', $app->handle($request)->getContent());
} }
public function testBeforeFilterAccessesRequestAndCanReturnResponse()
{
$app = new Application();
$app->before(function (Request $request) {
return new Response($request->get('name'));
});
$app->match('/', function () use ($app) { throw new \Exception('Should never be executed'); });
$request = Request::create('/?name=Fabien');
$this->assertEquals('Fabien', $app->handle($request)->getContent());
}
public function testAfterFilterAccessRequestResponse()
{
$app = new Application();
$app->after(function (Request $request, Response $response) {
$response->setContent($response->getContent().'---');
});
$app->match('/', function () { return new Response('foo'); });
$request = Request::create('/');
$this->assertEquals('foo---', $app->handle($request)->getContent());
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment