Skip to content

Commit

Permalink
added a priority to the event dispatcher listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Aug 26, 2010
1 parent 1d7f43e commit 82ff790
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 27 deletions.
Expand Up @@ -39,10 +39,11 @@ public function __construct(ContainerInterface $container, $controller, LoggerIn
* 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)
Expand Down
5 changes: 3 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/RequestListener.php
Expand Up @@ -37,10 +37,11 @@ public function __construct(RouterInterface $router, LoggerInterface $logger = n
* 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)
Expand Down
Expand Up @@ -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" />
Expand Down
32 changes: 19 additions & 13 deletions src/Symfony/Component/EventDispatcher/EventDispatcher.php
Expand Up @@ -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;
}

/**
Expand All @@ -50,9 +51,11 @@ public function disconnect($name, $listener)
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]);
}
}
}
}
Expand Down Expand Up @@ -120,11 +123,7 @@ public function filter(Event $event, $value)
*/
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));
}

/**
Expand All @@ -140,6 +139,13 @@ public function getListeners($name)
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;
}
}
5 changes: 3 additions & 2 deletions src/Symfony/Component/HttpKernel/Cache/EsiListener.php
Expand Up @@ -39,12 +39,13 @@ public function __construct(Esi $esi = null)
* 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);
}
}

Expand Down
Expand Up @@ -34,10 +34,11 @@ public function __construct(Profiler $profiler)
* 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)
Expand Down
Expand Up @@ -35,10 +35,11 @@ public function __construct(Profiler $profiler)
* 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)
Expand Down
5 changes: 3 additions & 2 deletions src/Symfony/Component/HttpKernel/ResponseListener.php
Expand Up @@ -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);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Framework/EventDispatcher.php
Expand Up @@ -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);
}
}
}

0 comments on commit 82ff790

Please sign in to comment.