Merge pull request #23215 from eileenmcnaughton/test_amount
[civicrm-core.git] / Civi / Core / CiviEventDispatcher.php
index dd3bc3f7e506a2921dcec01be10f4d4f0667a69d..f2ddb6431ef63e8eb784b2bf7d059c3f04410e2f 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace Civi\Core;
 
+use Civi\Core\Event\HookStyleListener;
 use Symfony\Component\EventDispatcher\EventDispatcher;
 use Symfony\Component\EventDispatcher\Event;
 
@@ -89,6 +90,52 @@ class CiviEventDispatcher extends EventDispatcher {
     }
   }
 
+  /**
+   * Add a test listener.
+   *
+   * @param string $eventName
+   *   Ex: 'civi.internal.event'
+   *   Ex: 'hook_civicrm_publicEvent'
+   *   Ex: '&hook_civicrm_publicEvent' (an alias for 'hook_civicrm_publicEvent' in which the listener abides hook-style ordered parameters).
+   *        This notation is handy when attaching via listener-maps (e.g. `getSubscribedEvents()`).
+   * @param callable $listener
+   * @param int $priority
+   */
+  public function addListener($eventName, $listener, $priority = 0) {
+    if ($eventName[0] === '&') {
+      $eventName = substr($eventName, 1);
+      $listener = new HookStyleListener($listener);
+    }
+    parent::addListener($eventName, $listener, $priority);
+  }
+
+  /**
+   * Adds a series of event listeners from methods in a class.
+   *
+   * @param string|object $target
+   *   The object/class which will receive the notifications.
+   *   Use a string (class-name) if the listeners are static methods.
+   *   Use an object-instance if the listeners are regular methods.
+   * @param array $events
+   *   List of events/methods/priorities.
+   * @see \Symfony\Component\EventDispatcher\EventSubscriberInterface::getSubscribedEvents()
+   */
+  public function addListenerMap($target, array $events) {
+    foreach ($events as $eventName => $params) {
+      if (\is_string($params)) {
+        $this->addListener($eventName, [$target, $params]);
+      }
+      elseif (\is_string($params[0])) {
+        $this->addListener($eventName, [$target, $params[0]], isset($params[1]) ? $params[1] : 0);
+      }
+      else {
+        foreach ($params as $listener) {
+          $this->addListener($eventName, [$target, $listener[0]], isset($listener[1]) ? $listener[1] : 0);
+        }
+      }
+    }
+  }
+
   /**
    * Adds a service as event listener.
    *