Commit 09a61047 authored by Fabien Potencier's avatar Fabien Potencier

moved the converter and string to response listeners to proper classes

parent f51a1f24
...@@ -14,9 +14,7 @@ namespace Silex; ...@@ -14,9 +14,7 @@ namespace Silex;
use Symfony\Component\HttpKernel\HttpKernel; use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\TerminableInterface; use Symfony\Component\HttpKernel\TerminableInterface;
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\FilterResponseEvent; use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\PostResponseEvent; use Symfony\Component\HttpKernel\Event\PostResponseEvent;
...@@ -31,20 +29,21 @@ use Symfony\Component\HttpFoundation\RedirectResponse; ...@@ -31,20 +29,21 @@ use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\StreamedResponse; use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\EventDispatcher\Event; use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RequestContext; use Symfony\Component\Routing\RequestContext;
use Silex\RedirectableUrlMatcher; use Silex\RedirectableUrlMatcher;
use Silex\ControllerResolver; use Silex\ControllerResolver;
use Silex\EventListener\LocaleListener; use Silex\EventListener\LocaleListener;
use Silex\EventListener\MiddlewareListener; use Silex\EventListener\MiddlewareListener;
use Silex\EventListener\ConverterListener;
use Silex\EventListener\StringToResponseListener;
/** /**
* The Silex framework class. * The Silex framework class.
* *
* @author Fabien Potencier <fabien@symfony.com> * @author Fabien Potencier <fabien@symfony.com>
*/ */
class Application extends \Pimple implements HttpKernelInterface, EventSubscriberInterface, TerminableInterface class Application extends \Pimple implements HttpKernelInterface, TerminableInterface
{ {
const VERSION = '1.0-DEV'; const VERSION = '1.0-DEV';
...@@ -91,7 +90,6 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe ...@@ -91,7 +90,6 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
$this['dispatcher_class'] = 'Symfony\\Component\\EventDispatcher\\EventDispatcher'; $this['dispatcher_class'] = 'Symfony\\Component\\EventDispatcher\\EventDispatcher';
$this['dispatcher'] = $this->share(function () use ($app) { $this['dispatcher'] = $this->share(function () use ($app) {
$dispatcher = new $app['dispatcher_class'](); $dispatcher = new $app['dispatcher_class']();
$dispatcher->addSubscriber($app);
$urlMatcher = new LazyUrlMatcher(function () use ($app) { $urlMatcher = new LazyUrlMatcher(function () use ($app) {
return $app['url_matcher']; return $app['url_matcher'];
...@@ -103,6 +101,8 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe ...@@ -103,6 +101,8 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
} }
$dispatcher->addSubscriber(new ResponseListener($app['charset'])); $dispatcher->addSubscriber(new ResponseListener($app['charset']));
$dispatcher->addSubscriber(new MiddlewareListener($app)); $dispatcher->addSubscriber(new MiddlewareListener($app));
$dispatcher->addSubscriber(new ConverterListener($app['routes']));
$dispatcher->addSubscriber(new StringToResponseListener());
return $dispatcher; return $dispatcher;
}); });
...@@ -497,44 +497,4 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe ...@@ -497,44 +497,4 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
{ {
$this['kernel']->terminate($request, $response); $this['kernel']->terminate($request, $response);
} }
/**
* Handles converters.
*
* @param FilterControllerEvent $event The event to handle
*/
public function onKernelController(FilterControllerEvent $event)
{
$request = $event->getRequest();
$route = $this['routes']->get($request->attributes->get('_route'));
if ($route && $converters = $route->getOption('_converters')) {
foreach ($converters as $name => $callback) {
$request->attributes->set($name, call_user_func($callback, $request->attributes->get($name, null), $request));
}
}
}
/**
* Handles string responses.
*
* @param GetResponseForControllerResultEvent $event The event to handle
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$response = $event->getControllerResult();
$response = $response instanceof Response ? $response : new Response((string) $response);
$event->setResponse($response);
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return array(
KernelEvents::CONTROLLER => array('onKernelController', 0),
KernelEvents::VIEW => array('onKernelView', -10),
);
}
} }
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silex\EventListener;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\RouteCollection;
/**
* Handles converters.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class ConverterListener implements EventSubscriberInterface
{
protected $routes;
/**
* Constructor.
*
* @param RouteCollection $routes A RouteCollection instance
*/
public function __construct(RouteCollection $routes)
{
$this->routes = $routes;
}
/**
* Handles converters.
*
* @param FilterControllerEvent $event The event to handle
*/
public function onKernelController(FilterControllerEvent $event)
{
$request = $event->getRequest();
$route = $this->routes->get($request->attributes->get('_route'));
if ($route && $converters = $route->getOption('_converters')) {
foreach ($converters as $name => $callback) {
$request->attributes->set($name, call_user_func($callback, $request->attributes->get($name, null), $request));
}
}
}
public static function getSubscribedEvents()
{
return array(
KernelEvents::CONTROLLER => 'onKernelController',
);
}
}
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silex\EventListener;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
/**
* Handles converters.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class StringToResponseListener implements EventSubscriberInterface
{
/**
* Handles string responses.
*
* @param GetResponseForControllerResultEvent $event The event to handle
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$response = $event->getControllerResult();
$response = $response instanceof Response ? $response : new Response((string) $response);
$event->setResponse($response);
}
public static function getSubscribedEvents()
{
return array(
KernelEvents::VIEW => array('onKernelView', -10),
);
}
}
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