X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=Civi%2FCore%2FCiviEventDispatcher.php;h=f2ddb6431ef63e8eb784b2bf7d059c3f04410e2f;hb=c491114218f41869937e8e50b8d4b735c86e2653;hp=dd3bc3f7e506a2921dcec01be10f4d4f0667a69d;hpb=b522e008471688f89d108b622b08f77621836d5f;p=civicrm-core.git diff --git a/Civi/Core/CiviEventDispatcher.php b/Civi/Core/CiviEventDispatcher.php index dd3bc3f7e5..f2ddb6431e 100644 --- a/Civi/Core/CiviEventDispatcher.php +++ b/Civi/Core/CiviEventDispatcher.php @@ -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. *