5 use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher
;
6 use Symfony\Component\EventDispatcher\Event
;
9 * Class CiviEventDispatcher
12 * The CiviEventDispatcher is a Symfony dispatcher. Additionally, if an event
13 * follows the naming convention of "hook_*", then it will also be dispatched
14 * through CRM_Utils_Hook::invoke().
16 * @see \CRM_Utils_Hook
18 class CiviEventDispatcher
extends ContainerAwareEventDispatcher
{
20 const DEFAULT_HOOK_PRIORITY
= -100;
23 * Track the list of hook-events for which we have autoregistered
27 * Array(string $eventName => trueish).
29 private $autoListeners = [];
32 * Determine whether $eventName should delegate to the CMS hook system.
34 * @param string $eventName
35 * Ex: 'civi.token.eval', 'hook_civicrm_post`.
38 protected function isHookEvent($eventName) {
39 return (substr($eventName, 0, 5) === 'hook_') && (strpos($eventName, '::') === FALSE);
45 public function dispatch($eventName, Event
$event = NULL) {
46 $this->bindPatterns($eventName);
47 return parent
::dispatch($eventName, $event);
53 public function getListeners($eventName = NULL) {
54 $this->bindPatterns($eventName);
55 return parent
::getListeners($eventName);
61 public function hasListeners($eventName = NULL) {
62 // All hook_* events have default listeners, so hasListeners(NULL) is a truism.
63 return ($eventName === NULL ||
$this->isHookEvent($eventName))
64 ?
TRUE : parent
::hasListeners($eventName);
68 * Invoke hooks using an event object.
70 * @param \Civi\Core\Event\GenericHookEvent $event
71 * @param string $eventName
72 * Ex: 'hook_civicrm_dashboard'.
74 public static function delegateToUF($event, $eventName) {
75 $hookName = substr($eventName, 5);
76 $hooks = \CRM_Utils_Hook
::singleton();
77 $params = $event->getHookValues();
78 $count = count($params);
82 $fResult = $hooks->invokeViaUF($count, \CRM_Utils_Hook
::$_nullObject, \CRM_Utils_Hook
::$_nullObject, \CRM_Utils_Hook
::$_nullObject, \CRM_Utils_Hook
::$_nullObject, \CRM_Utils_Hook
::$_nullObject, \CRM_Utils_Hook
::$_nullObject, $hookName);
86 $fResult = $hooks->invokeViaUF($count, $params[0], \CRM_Utils_Hook
::$_nullObject, \CRM_Utils_Hook
::$_nullObject, \CRM_Utils_Hook
::$_nullObject, \CRM_Utils_Hook
::$_nullObject, \CRM_Utils_Hook
::$_nullObject, $hookName);
90 $fResult = $hooks->invokeViaUF($count, $params[0], $params[1], \CRM_Utils_Hook
::$_nullObject, \CRM_Utils_Hook
::$_nullObject, \CRM_Utils_Hook
::$_nullObject, \CRM_Utils_Hook
::$_nullObject, $hookName);
94 $fResult = $hooks->invokeViaUF($count, $params[0], $params[1], $params[2], \CRM_Utils_Hook
::$_nullObject, \CRM_Utils_Hook
::$_nullObject, \CRM_Utils_Hook
::$_nullObject, $hookName);
98 $fResult = $hooks->invokeViaUF($count, $params[0], $params[1], $params[2], $params[3], \CRM_Utils_Hook
::$_nullObject, \CRM_Utils_Hook
::$_nullObject, $hookName);
102 $fResult = $hooks->invokeViaUF($count, $params[0], $params[1], $params[2], $params[3], $params[4], \CRM_Utils_Hook
::$_nullObject, $hookName);
106 $fResult = $hooks->invokeViaUF($count, $params[0], $params[1], $params[2], $params[3], $params[4], $params[5], $hookName);
110 throw new \
RuntimeException("hook_{$hookName} cannot support more than 6 parameters");
113 $event->addReturnValues($fResult);
117 * @param string $eventName
118 * Ex: 'civi.api.resolve' or 'hook_civicrm_dashboard'.
120 protected function bindPatterns($eventName) {
121 if ($eventName !== NULL && !isset($this->autoListeners
[$eventName])) {
122 $this->autoListeners
[$eventName] = 1;
123 if ($this->isHookEvent($eventName)) {
124 // WISHLIST: For native extensions (and possibly D6/D7/D8/BD), enumerate
125 // the listeners and list them one-by-one. This would make it easier to
126 // inspect via "cv debug:event-dispatcher".
127 $this->addListener($eventName, [
128 '\Civi\Core\CiviEventDispatcher',
130 ], self
::DEFAULT_HOOK_PRIORITY
);