Commit 86c02016 authored by Beau Simensen's avatar Beau Simensen Committed by Dave Marshall

Add view.

parent fde7db80
...@@ -18,6 +18,7 @@ use Symfony\Component\HttpKernel\HttpKernelInterface; ...@@ -18,6 +18,7 @@ use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\TerminableInterface; use Symfony\Component\HttpKernel\TerminableInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
use Symfony\Component\HttpKernel\Event\PostResponseEvent; use Symfony\Component\HttpKernel\Event\PostResponseEvent;
use Symfony\Component\HttpKernel\EventListener\ResponseListener; use Symfony\Component\HttpKernel\EventListener\ResponseListener;
use Symfony\Component\HttpKernel\EventListener\RouterListener; use Symfony\Component\HttpKernel\EventListener\RouterListener;
...@@ -404,6 +405,28 @@ class Application extends \Pimple implements HttpKernelInterface, TerminableInte ...@@ -404,6 +405,28 @@ class Application extends \Pimple implements HttpKernelInterface, TerminableInte
$this->on(KernelEvents::EXCEPTION, new ExceptionListenerWrapper($this, $callback), $priority); $this->on(KernelEvents::EXCEPTION, new ExceptionListenerWrapper($this, $callback), $priority);
} }
/**
* Registers a view handler.
*
* View handlers are simple callables which take a single
* GetResponseForControllerResultEvent as an argument. They care called
* when a controller returns a value that is not an instance of Response.
*
* Call setResponse() to set the response that will be returned for the
* current request. The propagation of this event is stopped as soon as a
* response is set.
*
* @param mixed $callback View handelr callback
* @param integer $priority The higher this value, the earlier an event
* listener will be triggered in the chain (defaults to 0)
*/
public function view($callback, $priority = 0)
{
$this->on(KernelEvents::VIEW, function (GetResponseForControllerResultEvent $event) use ($callback) {
call_user_func($callback, $event);
}, $priority);
}
/** /**
* Flushes the controller collection. * Flushes the controller collection.
* *
......
...@@ -559,6 +559,72 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase ...@@ -559,6 +559,72 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
$response = $app->handle(Request::create('/foo')); $response = $app->handle(Request::create('/foo'));
$this->assertEquals(301, $response->getStatusCode()); $this->assertEquals(301, $response->getStatusCode());
} }
public function testBeforeFilterOnMountedControllerGroupIsolatedToGroup()
{
$app = new Application();
$app->match('/', function() { return new Response('ok'); });
$mounted = $app['controllers_factory'];
$mounted->before(function() { return new Response('not ok'); });
$app->mount('/group', $mounted);
$response = $app->handle(Request::create('/'));
$this->assertEquals('ok', $response->getContent());
}
public function testViewListener()
{
$app = new Application();
$app->get('/foo', function() { return array('ok'); });
$app->view(function ($event) {
$view = $event->getControllerResult();
$event->setResponse(new Response($view[0]));
});
$response = $app->handle(Request::create('/foo'));
$this->assertEquals('ok', $response->getContent());
}
public function testDefaultPriorityStringResponseTriggerViewListener()
{
$app = new Application();
$app->get('/foo', function() { return 'not ok'; });
$app->view(function ($event) {
$event->setResponse(new Response('ok'));
});
$response = $app->handle(Request::create('/foo'));
$this->assertEquals('ok', $response->getContent());
}
public function testLowPriorityStringResponseDoesNotTriggerViewListener()
{
$app = new Application();
$app->get('/foo', function() { return 'ok'; });
$app->view(function ($event) {
$event->setResponse(new Response('not ok'));
}, -100);
$response = $app->handle(Request::create('/foo'));
$this->assertEquals('ok', $response->getContent());
}
public function testResponseInstanceResponseDoesNotTriggerViewListener()
{
$app = new Application();
$app->get('/foo', function() { return new Response('ok'); });
$app->view(function ($event) {
$event->setResponse(new Response('not ok'));
});
$response = $app->handle(Request::create('/foo'));
$this->assertEquals('ok', $response->getContent());
}
} }
class FooController class FooController
......
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