ソースを参照

[FrameworkBundle] Small optimization, remove some function calls

Jordi Boggiano 14 年 前
コミット
48d6a95803

+ 26 - 16
src/Symfony/Bundle/FrameworkBundle/Debug/TraceableEventDispatcher.php

@@ -68,30 +68,40 @@ class TraceableEventDispatcher extends ContainerAwareEventDispatcher implements
     /**
      * {@inheritDoc}
      */
-    protected function triggerListener($listener, $eventName, Event $event)
+    protected function doDispatch($listeners, $eventName, Event $event)
     {
-        parent::triggerListener($listener, $eventName, $event);
+        foreach ($listeners as $listener) {
+            if ($listener instanceof \Closure) {
+                $listener->__invoke($event);
+            } else {
+                $listener->$eventName($event);
+            }
 
-        if (null !== $this->logger) {
-            $this->logger->debug(sprintf('Notified event "%s" to listener "%s".', $eventName, get_class($listener)));
-        }
+            if (null !== $this->logger) {
+                $this->logger->debug(sprintf('Notified event "%s" to listener "%s".', $eventName, get_class($listener)));
+            }
 
-        $this->called[$eventName.'.'.get_class($listener)] = $this->getListenerInfo($listener, $eventName);
+            $this->called[$eventName.'.'.get_class($listener)] = $this->getListenerInfo($listener, $eventName);
 
-        if ($event->isPropagationStopped() && null !== $this->logger) {
-            $this->logger->debug(sprintf('Listener "%s" stopped propagation of the event "%s".', get_class($listener), $eventName));
+            if ($event->isPropagationStopped()) {
+                if (null !== $this->logger) {
+                    $this->logger->debug(sprintf('Listener "%s" stopped propagation of the event "%s".', get_class($listener), $eventName));
 
-            $skippedListeners = $this->getListeners($eventName);
-            $skipped = false;
+                    $skippedListeners = $this->getListeners($eventName);
+                    $skipped = false;
 
-            foreach ($skippedListeners as $skippedListener) {
-                if ($skipped) {
-                    $this->logger->debug(sprintf('Listener "%s" was not called for event "%s".', get_class($skippedListener), $eventName));
-                }
+                    foreach ($skippedListeners as $skippedListener) {
+                        if ($skipped) {
+                            $this->logger->debug(sprintf('Listener "%s" was not called for event "%s".', get_class($skippedListener), $eventName));
+                        }
 
-                if ($skippedListener === $listener) {
-                    $skipped = true;
+                        if ($skippedListener === $listener) {
+                            $skipped = true;
+                        }
+                    }
                 }
+
+                break;
             }
         }
     }

+ 17 - 20
src/Symfony/Component/EventDispatcher/EventDispatcher.php

@@ -27,14 +27,12 @@ namespace Symfony\Component\EventDispatcher;
  * manager.
  *
  * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
- * @link    www.doctrine-project.org
- * @since   2.0
- * @version $Revision: 3938 $
  * @author  Guilherme Blanco <guilhermeblanco@hotmail.com>
  * @author  Jonathan Wage <jonwage@gmail.com>
  * @author  Roman Borschel <roman@code-factory.org>
  * @author  Bernhard Schussek <bschussek@gmail.com>
  * @author  Fabien Potencier <fabien@symfony.com>
+ * @author  Jordi Boggiano <j.boggiano@seld.be>
  *
  * @api
  */
@@ -58,13 +56,7 @@ class EventDispatcher implements EventDispatcherInterface
             $event = new Event();
         }
 
-        foreach ($this->getListeners($eventName) as $listener) {
-            $this->triggerListener($listener, $eventName, $event);
-
-            if ($event->isPropagationStopped()) {
-                break;
-            }
-        }
+        $this->doDispatch($this->getListeners($eventName), $eventName, $event);
     }
 
     /**
@@ -161,22 +153,27 @@ class EventDispatcher implements EventDispatcherInterface
     }
 
     /**
-     * Triggers the listener method for an event.
+     * Triggers the listeners of an event.
      *
      * This method can be overridden to add functionality that is executed
      * for each listener.
      *
-     * @param object $listener The event listener on which to invoke the listener method.
-     * @param string $eventName The name of the event to dispatch. The name of the event is
-     *                          the name of the method that is invoked on listeners.
-     * @param Event $event The event arguments to pass to the event handlers/listeners.
+     * @param array[callback] $listeners The event listeners.
+     * @param string $eventName The name of the event to dispatch.
+     * @param Event $event The event object to pass to the event handlers/listeners.
      */
-    protected function triggerListener($listener, $eventName, Event $event)
+    protected function doDispatch($listeners, $eventName, Event $event)
     {
-        if ($listener instanceof \Closure) {
-            $listener->__invoke($event);
-        } else {
-            $listener->$eventName($event);
+        foreach ($listeners as $listener) {
+            if ($listener instanceof \Closure) {
+                $listener->__invoke($event);
+            } else {
+                $listener->$eventName($event);
+            }
+
+            if ($event->isPropagationStopped()) {
+                break;
+            }
         }
     }