Commit cc34489e authored by Henrik Bjornskov's avatar Henrik Bjornskov

Introduce KernelServiceProvider in order to "hacks"

This introduces a new KernelServiceProvider that is auto registered
by Silex\Application.

The main reason for including this is to remove the ugly
"dispatcher_class" hack we used to have. But since we introduced a
seperate interface for listeners to use for subscribing we can just have
a nice normal service.
parent 82f7e13d
......@@ -20,7 +20,6 @@ use Symfony\Component\HttpKernel\TerminableInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
use Symfony\Component\HttpKernel\EventListener\ResponseListener;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpFoundation\Request;
......@@ -33,10 +32,8 @@ use Symfony\Component\Routing\RouteCollection;
use Silex\Api\BootableProviderInterface;
use Silex\Api\EventListenerProviderInterface;
use Silex\Api\ControllerProviderInterface;
use Silex\EventListener\MiddlewareListener;
use Silex\EventListener\ConverterListener;
use Silex\EventListener\StringToResponseListener;
use Silex\Provider\RoutingServiceProvider;
use Silex\Provider\KernelServiceProvider;
/**
* The Silex framework class.
......@@ -87,20 +84,6 @@ class Application extends Container implements HttpKernelInterface, TerminableIn
return new ExceptionHandler($app['debug']);
};
$this['dispatcher_class'] = 'Symfony\\Component\\EventDispatcher\\EventDispatcher';
$this['dispatcher'] = function () use ($app) {
$dispatcher = new $app['dispatcher_class']();
if (isset($app['exception_handler'])) {
$dispatcher->addSubscriber($app['exception_handler']);
}
$dispatcher->addSubscriber(new ResponseListener($app['charset']));
$dispatcher->addSubscriber(new MiddlewareListener($app));
$dispatcher->addSubscriber(new ConverterListener($app['routes'], $app['callback_resolver']));
$dispatcher->addSubscriber(new StringToResponseListener());
return $dispatcher;
};
$this['callback_resolver'] = function () use ($app) {
return new CallbackResolver($app);
......@@ -124,6 +107,7 @@ class Application extends Container implements HttpKernelInterface, TerminableIn
$this['charset'] = 'UTF-8';
$this['logger'] = null;
$this->register(new KernelServiceProvider());
$this->register(new RoutingServiceProvider());
foreach ($values as $key => $value) {
......
<?php
namespace Silex\Provider;
use Pimple\ServiceProviderInterface;
use Pimple\Container;
use Silex\Api\EventListenerProviderInterface;
use Silex\EventListener\ConverterListener;
use Silex\EventListener\MiddlewareListener;
use Silex\EventListener\StringToResponseListener;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpKernel\EventListener\ResponseListener;
class KernelServiceProvider implements ServiceProviderInterface, EventListenerProviderInterface
{
/**
* {@inheritDoc}
*/
public function register(Container $pimple)
{
$pimple['dispatcher'] = function () {
return new EventDispatcher();
};
}
/**
* {@inheritDoc}
*/
public function subscribe(Container $app, EventDispatcherInterface $dispatcher)
{
if (isset($app['exception_handler'])) {
$dispatcher->addSubscriber($app['exception_handler']);
}
$dispatcher->addSubscriber(new ResponseListener($app['charset']));
$dispatcher->addSubscriber(new MiddlewareListener($app));
$dispatcher->addSubscriber(new ConverterListener($app['routes'], $app['callback_resolver']));
$dispatcher->addSubscriber(new StringToResponseListener());
}
}
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