Merge pull request #17753 from demeritcowboy/unused-gencode
[civicrm-core.git] / Civi / Core / CiviEventDispatcher.php
index f6f6479b9600bd2b47c734dd30836506fae84a2f..9f3da3215e4fcacc0659bf4836632f05e5313e05 100644 (file)
@@ -61,6 +61,34 @@ class CiviEventDispatcher extends EventDispatcher {
     return (substr($eventName, 0, 5) === 'hook_') && (strpos($eventName, '::') === FALSE);
   }
 
+  /**
+   * Adds a service as event listener.
+   *
+   * This provides partial backwards compatibility with ContainerAwareEventDispatcher.
+   *
+   * @param string $eventName Event for which the listener is added
+   * @param array $callback The service ID of the listener service & the method
+   *                        name that has to be called
+   * @param int $priority The higher this value, the earlier an event listener
+   *                      will be triggered in the chain.
+   *                      Defaults to 0.
+   *
+   * @throws \InvalidArgumentException
+   */
+  public function addListenerService($eventName, $callback, $priority = 0) {
+    if (!\is_array($callback) || 2 !== \count($callback)) {
+      throw new \InvalidArgumentException('Expected an array("service", "method") argument');
+    }
+
+    $this->addListener($eventName, function($event) use ($callback) {
+      static $svc;
+      if ($svc === NULL) {
+        $svc = \Civi::container()->get($callback[0]);
+      }
+      return call_user_func([$svc, $callback[1]], $event);
+    }, $priority);
+  }
+
   /**
    * @inheritDoc
    */
@@ -172,7 +200,6 @@ class CiviEventDispatcher extends EventDispatcher {
    */
   protected function bindPatterns($eventName) {
     if ($eventName !== NULL && !isset($this->autoListeners[$eventName])) {
-      $this->lazyLoad($eventName);
       $this->autoListeners[$eventName] = 1;
       if ($this->isHookEvent($eventName)) {
         // WISHLIST: For native extensions (and possibly D6/D7/D8/BD), enumerate
@@ -184,11 +211,6 @@ class CiviEventDispatcher extends EventDispatcher {
         ], self::DEFAULT_HOOK_PRIORITY);
       }
     }
-    elseif (NULL === $eventName) {
-      foreach ($this->listenerIds as $serviceEventName => $args) {
-        $this->lazyLoad($serviceEventName);
-      }
-    }
   }
 
   /**
@@ -256,69 +278,4 @@ class CiviEventDispatcher extends EventDispatcher {
     return 'fail';
   }
 
-  public function addListenerService($eventName, $callback, $priority = 0) {
-    if (!\is_array($callback) || 2 !== \count($callback)) {
-      throw new \InvalidArgumentException('Expected an array("service", "method") argument');
-    }
-    $this->listenerIds[$eventName][] = array(
-      $callback[0],
-      $callback[1],
-      $priority,
-    );
-  }
-
-  public function addSubscriberService($serviceId, $class) {
-    foreach ($class::getSubscribedEvents() as $eventName => $params) {
-      if (\is_string($params)) {
-        $this->listenerIds[$eventName][] = array(
-          $serviceId,
-          $params,
-          0,
-        );
-      }
-      elseif (\is_string($params[0])) {
-        $this->listenerIds[$eventName][] = array(
-          $serviceId,
-          $params[0],
-          isset($params[1]) ? $params[1] : 0,
-        );
-      }
-      else {
-        foreach ($params as $listener) {
-          $this->listenerIds[$eventName][] = array(
-            $serviceId,
-            $listener[0],
-            isset($listener[1]) ? $listener[1] : 0,
-          );
-        }
-      }
-    }
-  }
-
-  /**
-   * Lazily loads listeners for this event from the dependency injection
-   * container.
-   *
-   * @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.
-   */
-  protected function lazyLoad($eventName) {
-    if (isset($this->listenerIds[$eventName])) {
-      foreach ($this->listenerIds[$eventName] as list($serviceId, $method, $priority)) {
-        $listener = \Civi\Core\Container::singleton()->get($serviceId);
-        $key = $serviceId . '.' . $method;
-        if (!isset($this->listeners[$eventName][$key])) {
-          $this->addListener($eventName, array($listener, $method), $priority);
-        }
-        elseif ($this->listeners[$eventName][$key] !== $listener) {
-          parent::removeListener($eventName, array($this->listeners[$eventName][$key], $method));
-          $this->addListener($eventName, array($listener, $method), $priority);
-        }
-
-        $this->listeners[$eventName][$key] = $listener;
-      }
-    }
-  }
-
 }