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