Skip to content

Commit

Permalink
[EventDispatcher] added a way to set the priority for event subscribers
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Jun 14, 2011
1 parent b76a1c3 commit c6cc427
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
10 changes: 7 additions & 3 deletions src/Symfony/Component/EventDispatcher/EventDispatcher.php
Expand Up @@ -127,10 +127,14 @@ public function removeListener($eventName, $listener)
/**
* @see EventDispatcherInterface::addSubscriber
*/
public function addSubscriber(EventSubscriberInterface $subscriber, $priority = 0)
public function addSubscriber(EventSubscriberInterface $subscriber)
{
foreach ($subscriber->getSubscribedEvents() as $eventName => $method) {
$this->addListener($eventName, array($subscriber, $method), $priority);
foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
if (is_string($params)) {
$this->addListener($eventName, array($subscriber, $params));
} else {
$this->addListener($eventName, array($subscriber, $params[0]), $params[1]);
}
}
}

Expand Down
Expand Up @@ -52,11 +52,10 @@ function addListener($eventName, $listener, $priority = 0);
* interested in and added as a listener for these events.
*
* @param EventSubscriberInterface $subscriber The subscriber.
* @param integer $priority The higher this value, the earlier an event
* listener will be triggered in the chain.
* Defaults to 0.
*
* @api
*/
function addSubscriber(EventSubscriberInterface $subscriber, $priority = 0);
function addSubscriber(EventSubscriberInterface $subscriber);

/**
* Removes an event listener from the specified events.
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/EventDispatcher/EventSubscriberInterface.php
Expand Up @@ -39,6 +39,16 @@ interface EventSubscriberInterface
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
*
* For instance:
*
* * array('eventName' => 'methodName')
* * array('eventName' => array('methodName', $priority))
*
* @return array The event names to listen to
*/
static function getSubscribedEvents();
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Form/FormBuilder.php
Expand Up @@ -290,9 +290,9 @@ public function addEventListener($eventName, $listener, $priority = 0)
*
* @return FormBuilder The current builder
*/
public function addEventSubscriber(EventSubscriberInterface $subscriber, $priority = 0)
public function addEventSubscriber(EventSubscriberInterface $subscriber)
{
$this->dispatcher->addSubscriber($subscriber, $priority);
$this->dispatcher->addSubscriber($subscriber);

return $this;
}
Expand Down
Expand Up @@ -168,6 +168,20 @@ public function testAddSubscriber()
$this->assertTrue($this->dispatcher->hasListeners(self::postFoo));
}

public function testAddSubscriberWithPriorities()
{
$eventSubscriber = new TestEventSubscriber();
$this->dispatcher->addSubscriber($eventSubscriber);

$eventSubscriber = new TestEventSubscriberWithPriorities();
$this->dispatcher->addSubscriber($eventSubscriber);

$listeners = $this->dispatcher->getListeners('pre.foo');
$this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
$this->assertEquals(2, count($listeners));
$this->assertInstanceOf('Symfony\Tests\Component\EventDispatcher\TestEventSubscriberWithPriorities', $listeners[0][0]);
}

public function testRemoveSubscriber()
{
$eventSubscriber = new TestEventSubscriber();
Expand Down Expand Up @@ -207,3 +221,11 @@ public static function getSubscribedEvents()
return array('pre.foo' => 'preFoo', 'post.foo' => 'postFoo');
}
}

class TestEventSubscriberWithPriorities implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return array('pre.foo' => array('preFoo', 10));
}
}

0 comments on commit c6cc427

Please sign in to comment.