From c6cc427e4b566f0899e785347710ee19f256ef04 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 14 Jun 2011 14:40:27 +0200 Subject: [PATCH] [EventDispatcher] added a way to set the priority for event subscribers --- .../EventDispatcher/EventDispatcher.php | 10 ++++++--- .../EventDispatcherInterface.php | 7 +++--- .../EventSubscriberInterface.php | 10 +++++++++ src/Symfony/Component/Form/FormBuilder.php | 4 ++-- .../EventDispatcher/EventDispatcherTest.php | 22 +++++++++++++++++++ 5 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Component/EventDispatcher/EventDispatcher.php b/src/Symfony/Component/EventDispatcher/EventDispatcher.php index 489587385f42..4f4fd030ce29 100644 --- a/src/Symfony/Component/EventDispatcher/EventDispatcher.php +++ b/src/Symfony/Component/EventDispatcher/EventDispatcher.php @@ -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]); + } } } diff --git a/src/Symfony/Component/EventDispatcher/EventDispatcherInterface.php b/src/Symfony/Component/EventDispatcher/EventDispatcherInterface.php index 397bfed0f2d3..012bc293927a 100644 --- a/src/Symfony/Component/EventDispatcher/EventDispatcherInterface.php +++ b/src/Symfony/Component/EventDispatcher/EventDispatcherInterface.php @@ -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. diff --git a/src/Symfony/Component/EventDispatcher/EventSubscriberInterface.php b/src/Symfony/Component/EventDispatcher/EventSubscriberInterface.php index edd9081c55d7..b50181378b6b 100644 --- a/src/Symfony/Component/EventDispatcher/EventSubscriberInterface.php +++ b/src/Symfony/Component/EventDispatcher/EventSubscriberInterface.php @@ -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(); diff --git a/src/Symfony/Component/Form/FormBuilder.php b/src/Symfony/Component/Form/FormBuilder.php index aebbbd3c4557..d600ac754b93 100644 --- a/src/Symfony/Component/Form/FormBuilder.php +++ b/src/Symfony/Component/Form/FormBuilder.php @@ -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; } diff --git a/tests/Symfony/Tests/Component/EventDispatcher/EventDispatcherTest.php b/tests/Symfony/Tests/Component/EventDispatcher/EventDispatcherTest.php index 40e005aebab0..c77b87dff7f6 100644 --- a/tests/Symfony/Tests/Component/EventDispatcher/EventDispatcherTest.php +++ b/tests/Symfony/Tests/Component/EventDispatcher/EventDispatcherTest.php @@ -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(); @@ -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)); + } +}