Commit ca79d330 authored by Fabien Potencier's avatar Fabien Potencier

moved all subscribers registration into the dispatcher closure creation

This removes yet another special case that should not exist. Now, all
listeners/subscribers registration are done in one place.

If you want to disable the exception_handler, you now should call the
disable() method on it. You can still unset it but be careful to do it
as early as possible (which is anyway always the best idea).
parent 056fc32f
Changelog
=========
* **2012-11-04**: Removing the default exception handler should now be done
via its ``disable()`` method:
Before:
unset($app['exception_handler']);
After:
$app['exception_handler']->disable();
* **2012-07-15**: removed the ``monolog.configure`` service. Use the
``extend`` method instead:
......
......@@ -213,7 +213,7 @@ don't want to mess with most of them.
* **exception_handler**: The Exception handler is the default handler that is
used when you don't register one via the ``error()`` method or if your handler
does not return a Response. Disable it with
``unset($app['exception_handler'])``.
``$app['exception_handler']->disable()``.
* **logger**: A
`LoggerInterface
......
......@@ -99,7 +99,7 @@ executed before every test.
{
$app = require __DIR__.'/path/to/app.php';
$app['debug'] = true;
unset($app['exception_handler']);
$app['exception_handler']->disable();
return $app;
}
......
......@@ -97,6 +97,10 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
});
$dispatcher->addSubscriber(new RouterListener($urlMatcher, $app['request_context'], $app['logger']));
$dispatcher->addSubscriber(new LocaleListener($app['locale'], $urlMatcher));
if (isset($app['exception_handler'])) {
$dispatcher->addSubscriber($app['exception_handler']);
}
$dispatcher->addSubscriber(new ResponseListener($app['charset']));
return $dispatcher;
});
......@@ -528,21 +532,6 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
$this['kernel']->terminate($request, $response);
}
/**
* Handles KernelEvents::REQUEST events registered early.
*
* @param GetResponseEvent $event The event to handle
*/
public function onEarlyKernelRequest(GetResponseEvent $event)
{
if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
if (isset($this['exception_handler'])) {
$this['dispatcher']->addSubscriber($this['exception_handler']);
}
$this['dispatcher']->addSubscriber(new ResponseListener($this['charset']));
}
}
/**
* Runs before filters.
*
......@@ -600,13 +589,10 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
public static function getSubscribedEvents()
{
return array(
KernelEvents::REQUEST => array(
array('onEarlyKernelRequest', 256),
array('onKernelRequest', -128)
),
KernelEvents::CONTROLLER => 'onKernelController',
KernelEvents::RESPONSE => array('onKernelResponse', 128),
KernelEvents::VIEW => array('onKernelView', -10),
KernelEvents::REQUEST => array('onKernelRequest', -128),
KernelEvents::CONTROLLER => array('onKernelController', 0),
KernelEvents::RESPONSE => array('onKernelResponse', 128),
KernelEvents::VIEW => array('onKernelView', -10),
);
}
}
......@@ -24,14 +24,25 @@ use Symfony\Component\HttpKernel\KernelEvents;
class ExceptionHandler implements EventSubscriberInterface
{
protected $debug;
protected $enabled;
public function __construct($debug)
{
$this->debug = $debug;
$this->enabled = true;
}
public function disable()
{
$this->enabled = false;
}
public function onSilexError(GetResponseForExceptionEvent $event)
{
if (!$this->enabled) {
return;
}
$handler = new DebugExceptionHandler($this->debug);
$event->setResponse($handler->createResponse($event->getException()));
......
......@@ -108,7 +108,7 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
public function testNoErrorHandler()
{
$app = new Application();
unset($app['exception_handler']);
$app['exception_handler']->disable();
$app->match('/foo', function () {
throw new \RuntimeException('foo exception');
......@@ -190,7 +190,7 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
public function testNoResponseErrorHandler()
{
$app = new Application();
unset($app['exception_handler']);
$app['exception_handler']->disable();
$app->match('/foo', function () {
throw new \RuntimeException('foo exception');
......@@ -262,7 +262,7 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
// just making sure the dispatcher gets created
});
unset($app['exception_handler']);
$app['exception_handler']->disable();
try {
$request = Request::create('/foo');
......
......@@ -44,11 +44,11 @@ class HttpCacheServiceProviderTest extends \PHPUnit_Framework_TestCase
{
$finished = false;
$app->get('/', function () use ($app, &$finished) {
$app->finish(function () use (&$finished) {
$finished = true;
});
$app->finish(function () use (&$finished) {
$finished = true;
});
$app->get('/', function () use ($app) {
return new UnsendableResponse('will do something after finish');
});
......
......@@ -102,8 +102,7 @@ class RouterTest extends \PHPUnit_Framework_TestCase
public function testMissingRoute()
{
$app = new Application();
unset($app['exception_handler']);
$app['exception_handler']->disable();
$request = Request::create('/baz');
$app->handle($request);
......
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