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