refactored the listeners management
This commit is contained in:
parent
3fe83cd726
commit
8c4dd5cca9
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Framework\WebBundle\Listener;
|
||||
namespace Symfony\Components\HttpKernel\Listener;
|
||||
|
||||
use Symfony\Components\EventDispatcher\EventDispatcher;
|
||||
use Symfony\Components\EventDispatcher\Event;
|
||||
@ -26,18 +26,22 @@ use Symfony\Components\HttpKernel\HttpKernelInterface;
|
||||
*/
|
||||
class ResponseFilter
|
||||
{
|
||||
protected $dispatcher;
|
||||
|
||||
public function __construct(EventDispatcher $dispatcher)
|
||||
/**
|
||||
* Registers a core.response listener to change the Content-Type header based on the Request format.
|
||||
*
|
||||
* @param Symfony\Components\EventDispatcher\EventDispatcher $dispatcher An EventDispatcher instance
|
||||
*/
|
||||
public function register(EventDispatcher $dispatcher)
|
||||
{
|
||||
$this->dispatcher = $dispatcher;
|
||||
}
|
||||
|
||||
public function register()
|
||||
{
|
||||
$this->dispatcher->connect('core.response', array($this, 'filter'));
|
||||
$dispatcher->connect('core.response', array($this, 'filter'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the Response.
|
||||
*
|
||||
* @param Symfony\Components\EventDispatcher\Event $event An Event instance
|
||||
* @param Symfony\Components\HttpKernel\Response $response A Response instance
|
||||
*/
|
||||
public function filter(Event $event, Response $response)
|
||||
{
|
||||
if (HttpKernelInterface::MASTER_REQUEST !== $event->getParameter('request_type') || $response->headers->has('Content-Type')) {
|
@ -16,8 +16,7 @@ use Symfony\Components\DependencyInjection\ContainerInterface;
|
||||
*/
|
||||
|
||||
/**
|
||||
* This EventDispatcher implementation uses a DependencyInjection container to
|
||||
* lazy load listeners.
|
||||
* This EventDispatcher implementation uses a DependencyInjection container to load listeners.
|
||||
*
|
||||
* @package Symfony
|
||||
* @subpackage Foundation
|
||||
@ -25,44 +24,14 @@ use Symfony\Components\DependencyInjection\ContainerInterface;
|
||||
*/
|
||||
class EventDispatcher extends BaseEventDispatcher
|
||||
{
|
||||
protected $container;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
*/
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
|
||||
foreach ($container->findAnnotatedServiceIds('kernel.listener') as $id => $attributes) {
|
||||
foreach ($attributes as $attribute) {
|
||||
if (isset($attribute['event'])) {
|
||||
$this->connect($attribute['event'], array($id, isset($attribute['method']) ? $attribute['method'] : 'handle'));
|
||||
}
|
||||
}
|
||||
$container->getService($id)->register($this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all listeners associated with a given event name.
|
||||
*
|
||||
* @param string $name The event name
|
||||
*
|
||||
* @return array An array of listeners
|
||||
*/
|
||||
public function getListeners($name)
|
||||
{
|
||||
if (!isset($this->listeners[$name])) {
|
||||
return array();
|
||||
}
|
||||
|
||||
foreach ($this->listeners[$name] as $i => $listener) {
|
||||
if (is_array($listener) && is_string($listener[0])) {
|
||||
$this->listeners[$name][$i] = array($this->container->getService($listener[0]), $listener[1]);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->listeners[$name];
|
||||
}
|
||||
}
|
||||
|
@ -338,35 +338,11 @@ use Symfony\Components\DependencyInjection\ContainerInterface;
|
||||
|
||||
class EventDispatcher extends BaseEventDispatcher
|
||||
{
|
||||
protected $container;
|
||||
|
||||
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
|
||||
foreach ($container->findAnnotatedServiceIds('kernel.listener') as $id => $attributes) {
|
||||
foreach ($attributes as $attribute) {
|
||||
if (isset($attribute['event'])) {
|
||||
$this->connect($attribute['event'], array($id, isset($attribute['method']) ? $attribute['method'] : 'handle'));
|
||||
}
|
||||
}
|
||||
$container->getService($id)->register($this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function getListeners($name)
|
||||
{
|
||||
if (!isset($this->listeners[$name])) {
|
||||
return array();
|
||||
}
|
||||
|
||||
foreach ($this->listeners[$name] as $i => $listener) {
|
||||
if (is_array($listener) && is_string($listener[0])) {
|
||||
$this->listeners[$name][$i] = array($this->container->getService($listener[0]), $listener[1]);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->listeners[$name];
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace Symfony\Framework\ProfilerBundle\DataCollector;
|
||||
|
||||
use Symfony\Components\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Components\EventDispatcher\EventDispatcher;
|
||||
use Symfony\Components\EventDispatcher\Event;
|
||||
use Symfony\Components\HttpKernel\Response;
|
||||
use Symfony\Components\HttpKernel\HttpKernelInterface;
|
||||
@ -43,9 +44,14 @@ class DataCollectorManager
|
||||
$this->collectors = $this->initCollectors();
|
||||
}
|
||||
|
||||
public function register()
|
||||
/**
|
||||
* Registers a core.response listener.
|
||||
*
|
||||
* @param Symfony\Components\EventDispatcher\EventDispatcher $dispatcher An EventDispatcher instance
|
||||
*/
|
||||
public function register(EventDispatcher $dispatcher)
|
||||
{
|
||||
$this->container->getEventDispatcherService()->connect('core.response', array($this, 'handle'));
|
||||
$dispatcher->connect('core.response', array($this, 'handle'));
|
||||
}
|
||||
|
||||
public function handle(Event $event, Response $response)
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace Symfony\Framework\ProfilerBundle\Listener;
|
||||
|
||||
use Symfony\Components\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Components\EventDispatcher\EventDispatcher;
|
||||
use Symfony\Components\EventDispatcher\Event;
|
||||
use Symfony\Components\HttpKernel\Response;
|
||||
use Symfony\Components\HttpKernel\HttpKernelInterface;
|
||||
@ -35,9 +36,14 @@ class WebDebugToolbar
|
||||
$this->collectorManager = $collectorManager;
|
||||
}
|
||||
|
||||
public function register()
|
||||
/**
|
||||
* Registers a core.response listener.
|
||||
*
|
||||
* @param Symfony\Components\EventDispatcher\EventDispatcher $dispatcher An EventDispatcher instance
|
||||
*/
|
||||
public function register(EventDispatcher $dispatcher)
|
||||
{
|
||||
$this->container->getEventDispatcherService()->connect('core.response', array($this, 'handle'));
|
||||
$dispatcher->connect('core.response', array($this, 'handle'));
|
||||
}
|
||||
|
||||
public function handle(Event $event, Response $response)
|
||||
|
@ -17,7 +17,7 @@
|
||||
|
||||
<services>
|
||||
<service id="data_collector_manager" class="%data_collector_manager.class%">
|
||||
<annotation name="kernel.listener" event="core.response" method="handle" />
|
||||
<annotation name="kernel.listener" />
|
||||
<argument type="service" id="service_container" />
|
||||
<argument type="service" id="logger" />
|
||||
<argument type="service" id="data_collector_manager.storage" />
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
<services>
|
||||
<service id="debug.toolbar" class="%debug.toolbar.class%">
|
||||
<annotation name="kernel.listener" event="core.response" method="handle" />
|
||||
<annotation name="kernel.listener" />
|
||||
<argument type="service" id="service_container" />
|
||||
<argument type="service" id="data_collector_manager" />
|
||||
</service>
|
||||
|
@ -27,19 +27,22 @@ use Symfony\Components\EventDispatcher\Event;
|
||||
class ControllerLoader
|
||||
{
|
||||
protected $manager;
|
||||
protected $dispatcher;
|
||||
protected $logger;
|
||||
|
||||
public function __construct(EventDispatcher $dispatcher, ControllerManager $manager, LoggerInterface $logger = null)
|
||||
public function __construct(ControllerManager $manager, LoggerInterface $logger = null)
|
||||
{
|
||||
$this->dispatcher = $dispatcher;
|
||||
$this->manager = $manager;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
public function register()
|
||||
/**
|
||||
* Registers a core.load_controller listener.
|
||||
*
|
||||
* @param Symfony\Components\EventDispatcher\EventDispatcher $dispatcher An EventDispatcher instance
|
||||
*/
|
||||
public function register(EventDispatcher $dispatcher)
|
||||
{
|
||||
$this->dispatcher->connect('core.load_controller', array($this, 'resolve'));
|
||||
$dispatcher->connect('core.load_controller', array($this, 'resolve'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace Symfony\Framework\WebBundle\Listener;
|
||||
|
||||
use Symfony\Components\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Components\EventDispatcher\EventDispatcher;
|
||||
use Symfony\Components\EventDispatcher\Event;
|
||||
use Symfony\Foundation\LoggerInterface;
|
||||
use Symfony\Components\HttpKernel\HttpKernelInterface;
|
||||
@ -37,9 +38,14 @@ class ExceptionHandler
|
||||
$this->controller = $controller;
|
||||
}
|
||||
|
||||
public function register()
|
||||
/**
|
||||
* Registers a core.exception listener.
|
||||
*
|
||||
* @param Symfony\Components\EventDispatcher\EventDispatcher $dispatcher An EventDispatcher instance
|
||||
*/
|
||||
public function register(EventDispatcher $dispatcher)
|
||||
{
|
||||
$this->container->getEventDispatcherService()->connect('core.exception', array($this, 'handle'));
|
||||
$dispatcher->connect('core.exception', array($this, 'handle'));
|
||||
}
|
||||
|
||||
public function handle(Event $event)
|
||||
|
@ -4,6 +4,7 @@ namespace Symfony\Framework\WebBundle\Listener;
|
||||
|
||||
use Symfony\Foundation\LoggerInterface;
|
||||
use Symfony\Components\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Components\EventDispatcher\EventDispatcher;
|
||||
use Symfony\Components\EventDispatcher\Event;
|
||||
use Symfony\Components\Routing\RouterInterface;
|
||||
use Symfony\Components\HttpKernel\HttpKernelInterface;
|
||||
@ -37,9 +38,14 @@ class RequestParser
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
public function register()
|
||||
/**
|
||||
* Registers a core.request listener.
|
||||
*
|
||||
* @param Symfony\Components\EventDispatcher\EventDispatcher $dispatcher An EventDispatcher instance
|
||||
*/
|
||||
public function register(EventDispatcher $dispatcher)
|
||||
{
|
||||
$this->container->getEventDispatcherService()->connect('core.request', array($this, 'resolve'));
|
||||
$dispatcher->connect('core.request', array($this, 'resolve'));
|
||||
}
|
||||
|
||||
public function resolve(Event $event)
|
||||
|
@ -9,7 +9,7 @@
|
||||
<parameter key="controller_manager.class">Symfony\Framework\WebBundle\Controller\ControllerManager</parameter>
|
||||
<parameter key="controller_loader.class">Symfony\Framework\WebBundle\Listener\ControllerLoader</parameter>
|
||||
<parameter key="router.class">Symfony\Components\Routing\Router</parameter>
|
||||
<parameter key="response_filter.class">Symfony\Framework\WebBundle\Listener\ResponseFilter</parameter>
|
||||
<parameter key="response_filter.class">Symfony\Components\HttpKernel\Listener\ResponseFilter</parameter>
|
||||
|
||||
<parameter key="exception_handler.class">Symfony\Framework\WebBundle\Listener\ExceptionHandler</parameter>
|
||||
<parameter key="exception_handler.bundle">WebBundle</parameter>
|
||||
@ -24,14 +24,13 @@
|
||||
</service>
|
||||
|
||||
<service id="controller_loader" class="%controller_loader.class%">
|
||||
<annotation name="kernel.listener" event="core.load_controller" method="resolve" />
|
||||
<argument type="service" id="event_dispatcher" />
|
||||
<annotation name="kernel.listener" />
|
||||
<argument type="service" id="controller_manager" />
|
||||
<argument type="service" id="logger" on-invalid="ignore" />
|
||||
</service>
|
||||
|
||||
<service id="request_parser" class="%request_parser.class%">
|
||||
<annotation name="kernel.listener" event="core.request" method="resolve" />
|
||||
<annotation name="kernel.listener" />
|
||||
<argument type="service" id="service_container" />
|
||||
<argument type="service" id="router" />
|
||||
<argument type="service" id="logger" on-invalid="ignore" />
|
||||
@ -51,12 +50,11 @@
|
||||
</service>
|
||||
|
||||
<service id="response_filter" class="%response_filter.class%">
|
||||
<annotation name="kernel.listener" event="core.response" method="filter" />
|
||||
<argument type="service" id="event_dispatcher" />
|
||||
<annotation name="kernel.listener" />
|
||||
</service>
|
||||
|
||||
<service id="exception_handler" class="%exception_handler.class%">
|
||||
<annotation name="kernel.listener" event="core.exception" method="handle" />
|
||||
<annotation name="kernel.listener" />
|
||||
<argument type="service" id="service_container" />
|
||||
<argument type="service" id="logger" on-invalid="null" />
|
||||
<argument>%exception_handler.bundle%</argument>
|
||||
|
Reference in New Issue
Block a user