added a priority to the event dispatcher listeners

This commit is contained in:
Fabien Potencier 2010-08-26 14:08:42 +02:00
parent 1d7f43eed4
commit 82ff79064a
9 changed files with 41 additions and 27 deletions

View File

@ -39,10 +39,11 @@ class ExceptionListener
* Registers a core.exception listener. * Registers a core.exception listener.
* *
* @param EventDispatcher $dispatcher An EventDispatcher instance * @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) public function handle(Event $event)

View File

@ -37,10 +37,11 @@ class RequestListener
* Registers a core.request listener. * Registers a core.request listener.
* *
* @param EventDispatcher $dispatcher An EventDispatcher instance * @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) public function resolve(Event $event)

View File

@ -46,7 +46,7 @@
</service> </service>
<service id="exception_listener" class="%exception_listener.class%"> <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 type="service" id="service_container" />
<argument>%exception_listener.controller%</argument> <argument>%exception_listener.controller%</argument>
<argument type="service" id="logger" on-invalid="null" /> <argument type="service" id="logger" on-invalid="null" />

View File

@ -26,14 +26,15 @@ class EventDispatcher
* *
* @param string $name An event name * @param string $name An event name
* @param mixed $listener A PHP callable * @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])) { if (!isset($this->listeners[$name][$priority])) {
$this->listeners[$name] = array(); $this->listeners[$name][$priority] = array();
} }
$this->listeners[$name][] = $listener; $this->listeners[$name][$priority][] = $listener;
} }
/** /**
@ -50,9 +51,11 @@ class EventDispatcher
return false; return false;
} }
foreach ($this->listeners[$name] as $i => $callable) { foreach ($this->listeners[$name] as $priority => $callables) {
if ($listener === $callable) { foreach ($callables as $i => $callable) {
unset($this->listeners[$name][$i]); if ($listener === $callable) {
unset($this->listeners[$name][$priority][$i]);
}
} }
} }
} }
@ -120,11 +123,7 @@ class EventDispatcher
*/ */
public function hasListeners($name) public function hasListeners($name)
{ {
if (!isset($this->listeners[$name])) { return (Boolean) count($this->getListeners($name));
$this->listeners[$name] = array();
}
return (boolean) count($this->listeners[$name]);
} }
/** /**
@ -140,6 +139,13 @@ class EventDispatcher
return array(); 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;
} }
} }

View File

@ -39,12 +39,13 @@ class EsiListener
* Registers a core.response listener to add the Surrogate-Control header to a Response when needed. * Registers a core.response listener to add the Surrogate-Control header to a Response when needed.
* *
* @param EventDispatcher $dispatcher An EventDispatcher instance * @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) if (null !== $this->esi)
{ {
$dispatcher->connect('core.response', array($this, 'filter')); $dispatcher->connect('core.response', array($this, 'filter'), $priority);
} }
} }

View File

@ -34,10 +34,11 @@ class ProfilerListener
* Registers a core.response listener. * Registers a core.response listener.
* *
* @param EventDispatcher $dispatcher An EventDispatcher instance * @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) public function handle(Event $event, Response $response)

View File

@ -35,10 +35,11 @@ class WebDebugToolbarListener
* Registers a core.response listener. * Registers a core.response listener.
* *
* @param EventDispatcher $dispatcher An EventDispatcher instance * @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) public function handle(Event $event, Response $response)

View File

@ -26,10 +26,11 @@ class ResponseListener
* Registers a core.response listener to change the Content-Type header based on the Request format. * Registers a core.response listener to change the Content-Type header based on the Request format.
* *
* @param EventDispatcher $dispatcher An EventDispatcher instance * @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);
} }
/** /**

View File

@ -29,7 +29,9 @@ class EventDispatcher extends BaseEventDispatcher
public function __construct(ContainerInterface $container) public function __construct(ContainerInterface $container)
{ {
foreach ($container->findTaggedServiceIds('kernel.listener') as $id => $attributes) { 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);
} }
} }
} }