Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / lib / Drupal / Component / EventDispatcher / ContainerAwareEventDispatcher.php
index 6c1e200825306709f2311b950bf903012a1d65b3..2e790eea0417e35045526f91028ca82869c230e7 100644 (file)
@@ -2,7 +2,7 @@
 
 namespace Drupal\Component\EventDispatcher;
 
-use Symfony\Component\DependencyInjection\IntrospectableContainerInterface;
+use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Component\EventDispatcher\Event;
 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@@ -36,7 +36,7 @@ class ContainerAwareEventDispatcher implements EventDispatcherInterface {
   /**
    * The service container.
    *
-   * @var \Symfony\Component\DependencyInjection\IntrospectableContainerInterface;
+   * @var \Symfony\Component\DependencyInjection\ContainerInterface
    */
   protected $container;
 
@@ -66,7 +66,7 @@ class ContainerAwareEventDispatcher implements EventDispatcherInterface {
   /**
    * Constructs a container aware event dispatcher.
    *
-   * @param \Symfony\Component\DependencyInjection\IntrospectableContainerInterface $container
+   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
    *   The service container.
    * @param array $listeners
    *   A nested array of listener definitions keyed by event name and priority.
@@ -77,7 +77,7 @@ class ContainerAwareEventDispatcher implements EventDispatcherInterface {
    *   A service entry will be resolved to a callable only just before its
    *   invocation.
    */
-  public function __construct(IntrospectableContainerInterface $container, array $listeners = []) {
+  public function __construct(ContainerInterface $container, array $listeners = []) {
     $this->container = $container;
     $this->listeners = $listeners;
     $this->unsorted = [];
@@ -91,9 +91,6 @@ class ContainerAwareEventDispatcher implements EventDispatcherInterface {
       $event = new Event();
     }
 
-    $event->setDispatcher($this);
-    $event->setName($event_name);
-
     if (isset($this->listeners[$event_name])) {
       // Sort listeners if necessary.
       if (isset($this->unsorted[$event_name])) {
@@ -107,8 +104,11 @@ class ContainerAwareEventDispatcher implements EventDispatcherInterface {
           if (!isset($definition['callable'])) {
             $definition['callable'] = [$this->container->get($definition['service'][0]), $definition['service'][1]];
           }
+          if (is_array($definition['callable']) && isset($definition['callable'][0]) && $definition['callable'][0] instanceof \Closure) {
+            $definition['callable'][0] = $definition['callable'][0]();
+          }
 
-          $definition['callable']($event, $event_name, $this);
+          call_user_func($definition['callable'], $event, $event_name, $this);
           if ($event->isPropagationStopped()) {
             return $event;
           }
@@ -147,6 +147,9 @@ class ContainerAwareEventDispatcher implements EventDispatcherInterface {
           if (!isset($definition['callable'])) {
             $definition['callable'] = [$this->container->get($definition['service'][0]), $definition['service'][1]];
           }
+          if (is_array($definition['callable']) && isset($definition['callable'][0]) && $definition['callable'][0] instanceof \Closure) {
+            $definition['callable'][0] = $definition['callable'][0]();
+          }
 
           $result[] = $definition['callable'];
         }
@@ -159,27 +162,29 @@ class ContainerAwareEventDispatcher implements EventDispatcherInterface {
   /**
    * {@inheritdoc}
    */
-  public function getListenerPriority($eventName, $listener) {
-    // Parts copied from \Symfony\Component\EventDispatcher, that's why you see
-    // a yoda condition here.
-    if (!isset($this->listeners[$eventName])) {
+  public function getListenerPriority($event_name, $listener) {
+    if (!isset($this->listeners[$event_name])) {
       return;
     }
-    foreach ($this->listeners[$eventName] as $priority => $listeners) {
-      if (FALSE !== ($key = array_search(['callable' => $listener], $listeners, TRUE))) {
-        return $priority;
-      }
+    if (is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure) {
+      $listener[0] = $listener[0]();
     }
     // Resolve service definitions if the listener has not been found so far.
-    foreach ($this->listeners[$eventName] as $priority => &$definitions) {
+    foreach ($this->listeners[$event_name] as $priority => &$definitions) {
       foreach ($definitions as $key => &$definition) {
         if (!isset($definition['callable'])) {
           // Once the callable is retrieved we keep it for subsequent method
           // invocations on this class.
-          $definition['callable'] = [$this->container->get($definition['service'][0]), $definition['service'][1]];
-          if ($definition['callable'] === $listener) {
-            return $priority;
-          }
+          $definition['callable'] = [
+            $this->container->get($definition['service'][0]),
+            $definition['service'][1],
+          ];
+        }
+        if (is_array($definition['callable']) && isset($definition['callable'][0]) && $definition['callable'][0] instanceof \Closure) {
+          $definition['callable'][0] = $definition['callable'][0]();
+        }
+        if ($definition['callable'] === $listener) {
+          return $priority;
         }
       }
     }
@@ -189,7 +194,17 @@ class ContainerAwareEventDispatcher implements EventDispatcherInterface {
    * {@inheritdoc}
    */
   public function hasListeners($event_name = NULL) {
-    return (bool) count($this->getListeners($event_name));
+    if ($event_name !== NULL) {
+      return !empty($this->listeners[$event_name]);
+    }
+
+    foreach ($this->listeners as $event_listeners) {
+      if ($event_listeners) {
+        return TRUE;
+      }
+    }
+
+    return FALSE;
   }
 
   /**
@@ -217,10 +232,23 @@ class ContainerAwareEventDispatcher implements EventDispatcherInterface {
           $definition['callable'] = [$this->container->get($definition['service'][0]), $definition['service'][1]];
         }
 
+        if (is_array($definition['callable']) && isset($definition['callable'][0]) && $definition['callable'][0] instanceof \Closure && !$listener instanceof \Closure) {
+          $definition['callable'][0] = $definition['callable'][0]();
+        }
+
+        if (is_array($definition['callable']) && isset($definition['callable'][0]) && !$definition['callable'][0] instanceof \Closure && is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure) {
+          $listener[0] = $listener[0]();
+        }
         if ($definition['callable'] === $listener) {
-          unset($this->listeners[$event_name][$priority][$key]);
+          unset($definitions[$key]);
         }
       }
+      if ($definitions) {
+        $this->listeners[$event_name][$priority] = $definitions;
+      }
+      else {
+        unset($this->listeners[$event_name][$priority]);
+      }
     }
   }