added a priority to the event dispatcher listeners
This commit is contained in:
parent
1d7f43eed4
commit
82ff79064a
@ -39,10 +39,11 @@ class ExceptionListener
|
||||
* Registers a core.exception listener.
|
||||
*
|
||||
* @param EventDispatcher $dispatcher An EventDispatcher instance
|
||||
* @param integer $priority The priority
|
||||
*/
|
||||
public function register(EventDispatcher $dispatcher)
|
||||
public function register(EventDispatcher $dispatcher, $priority = 0)
|
||||
{
|
||||
$dispatcher->connect('core.exception', array($this, 'handle'));
|
||||
$dispatcher->connect('core.exception', array($this, 'handle'), $priority);
|
||||
}
|
||||
|
||||
public function handle(Event $event)
|
||||
|
@ -37,10 +37,11 @@ class RequestListener
|
||||
* Registers a core.request listener.
|
||||
*
|
||||
* @param EventDispatcher $dispatcher An EventDispatcher instance
|
||||
* @param integer $priority The priority
|
||||
*/
|
||||
public function register(EventDispatcher $dispatcher)
|
||||
public function register(EventDispatcher $dispatcher, $priority = 0)
|
||||
{
|
||||
$dispatcher->connect('core.request', array($this, 'resolve'));
|
||||
$dispatcher->connect('core.request', array($this, 'resolve'), $priority);
|
||||
}
|
||||
|
||||
public function resolve(Event $event)
|
||||
|
@ -46,7 +46,7 @@
|
||||
</service>
|
||||
|
||||
<service id="exception_listener" class="%exception_listener.class%">
|
||||
<tag name="kernel.listener" />
|
||||
<tag name="kernel.listener" priority="128" />
|
||||
<argument type="service" id="service_container" />
|
||||
<argument>%exception_listener.controller%</argument>
|
||||
<argument type="service" id="logger" on-invalid="null" />
|
||||
|
@ -26,14 +26,15 @@ class EventDispatcher
|
||||
*
|
||||
* @param string $name An event name
|
||||
* @param mixed $listener A PHP callable
|
||||
* @param integer $priority The priority (between -10 and 10 -- defaults to 0)
|
||||
*/
|
||||
public function connect($name, $listener)
|
||||
public function connect($name, $listener, $priority = 0)
|
||||
{
|
||||
if (!isset($this->listeners[$name])) {
|
||||
$this->listeners[$name] = array();
|
||||
if (!isset($this->listeners[$name][$priority])) {
|
||||
$this->listeners[$name][$priority] = array();
|
||||
}
|
||||
|
||||
$this->listeners[$name][] = $listener;
|
||||
$this->listeners[$name][$priority][] = $listener;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -50,9 +51,11 @@ class EventDispatcher
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($this->listeners[$name] as $i => $callable) {
|
||||
if ($listener === $callable) {
|
||||
unset($this->listeners[$name][$i]);
|
||||
foreach ($this->listeners[$name] as $priority => $callables) {
|
||||
foreach ($callables as $i => $callable) {
|
||||
if ($listener === $callable) {
|
||||
unset($this->listeners[$name][$priority][$i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -120,11 +123,7 @@ class EventDispatcher
|
||||
*/
|
||||
public function hasListeners($name)
|
||||
{
|
||||
if (!isset($this->listeners[$name])) {
|
||||
$this->listeners[$name] = array();
|
||||
}
|
||||
|
||||
return (boolean) count($this->listeners[$name]);
|
||||
return (Boolean) count($this->getListeners($name));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -140,6 +139,13 @@ class EventDispatcher
|
||||
return array();
|
||||
}
|
||||
|
||||
return $this->listeners[$name];
|
||||
$listeners = array();
|
||||
$all = $this->listeners[$name];
|
||||
ksort($all);
|
||||
foreach ($all as $l) {
|
||||
$listeners = array_merge($listeners, $l);
|
||||
}
|
||||
|
||||
return $listeners;
|
||||
}
|
||||
}
|
||||
|
@ -39,12 +39,13 @@ class EsiListener
|
||||
* Registers a core.response listener to add the Surrogate-Control header to a Response when needed.
|
||||
*
|
||||
* @param EventDispatcher $dispatcher An EventDispatcher instance
|
||||
* @param integer $priority The priority
|
||||
*/
|
||||
public function register(EventDispatcher $dispatcher)
|
||||
public function register(EventDispatcher $dispatcher, $priority = 0)
|
||||
{
|
||||
if (null !== $this->esi)
|
||||
{
|
||||
$dispatcher->connect('core.response', array($this, 'filter'));
|
||||
$dispatcher->connect('core.response', array($this, 'filter'), $priority);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -34,10 +34,11 @@ class ProfilerListener
|
||||
* Registers a core.response listener.
|
||||
*
|
||||
* @param EventDispatcher $dispatcher An EventDispatcher instance
|
||||
* @param integer $priority The priority
|
||||
*/
|
||||
public function register(EventDispatcher $dispatcher)
|
||||
public function register(EventDispatcher $dispatcher, $priority = 0)
|
||||
{
|
||||
$dispatcher->connect('core.response', array($this, 'handle'));
|
||||
$dispatcher->connect('core.response', array($this, 'handle'), $priority);
|
||||
}
|
||||
|
||||
public function handle(Event $event, Response $response)
|
||||
|
@ -35,10 +35,11 @@ class WebDebugToolbarListener
|
||||
* Registers a core.response listener.
|
||||
*
|
||||
* @param EventDispatcher $dispatcher An EventDispatcher instance
|
||||
* @param integer $priority The priority
|
||||
*/
|
||||
public function register(EventDispatcher $dispatcher)
|
||||
public function register(EventDispatcher $dispatcher, $priority = 0)
|
||||
{
|
||||
$dispatcher->connect('core.response', array($this, 'handle'));
|
||||
$dispatcher->connect('core.response', array($this, 'handle'), $priority);
|
||||
}
|
||||
|
||||
public function handle(Event $event, Response $response)
|
||||
|
@ -26,10 +26,11 @@ class ResponseListener
|
||||
* Registers a core.response listener to change the Content-Type header based on the Request format.
|
||||
*
|
||||
* @param EventDispatcher $dispatcher An EventDispatcher instance
|
||||
* @param integer $priority The priority
|
||||
*/
|
||||
public function register(EventDispatcher $dispatcher)
|
||||
public function register(EventDispatcher $dispatcher, $priority = 0)
|
||||
{
|
||||
$dispatcher->connect('core.response', array($this, 'filter'));
|
||||
$dispatcher->connect('core.response', array($this, 'filter'), $priority);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -29,7 +29,9 @@ class EventDispatcher extends BaseEventDispatcher
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
foreach ($container->findTaggedServiceIds('kernel.listener') as $id => $attributes) {
|
||||
$container->get($id)->register($this);
|
||||
$priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
|
||||
|
||||
$container->get($id)->register($this, $priority);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user