Browse Source

[EventDispatcher] added a way to set the priority for event subscribers

Fabien Potencier 14 years ago
parent
commit
c6cc427e4b

+ 7 - 3
src/Symfony/Component/EventDispatcher/EventDispatcher.php

@@ -127,10 +127,14 @@ class EventDispatcher implements EventDispatcherInterface
     /**
      * @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]);
+            }
         }
     }
 

+ 3 - 4
src/Symfony/Component/EventDispatcher/EventDispatcherInterface.php

@@ -52,11 +52,10 @@ interface EventDispatcherInterface
      * 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.

+ 10 - 0
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();

+ 2 - 2
src/Symfony/Component/Form/FormBuilder.php

@@ -290,9 +290,9 @@ class FormBuilder
      *
      * @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;
     }

+ 22 - 0
tests/Symfony/Tests/Component/EventDispatcher/EventDispatcherTest.php

@@ -168,6 +168,20 @@ class EventDispatcherTest extends \PHPUnit_Framework_TestCase
         $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 @@ class TestEventSubscriber implements EventSubscriberInterface
         return array('pre.foo' => 'preFoo', 'post.foo' => 'postFoo');
     }
 }
+
+class TestEventSubscriberWithPriorities implements EventSubscriberInterface
+{
+    public static function getSubscribedEvents()
+    {
+        return array('pre.foo' => array('preFoo', 10));
+    }
+}