X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=Civi%2FCore%2FCiviEventDispatcher.php;h=ac104537d81bfe8968015ea37004a6447634aeb1;hb=95d9cb655144abe69ca3daef970f446d59cc85a2;hp=18d8aa30438ddebef9d8b6bd078d078f54fa1c82;hpb=542ab281db6717f35f2a11a90e968af1cd82655d;p=civicrm-core.git diff --git a/Civi/Core/CiviEventDispatcher.php b/Civi/Core/CiviEventDispatcher.php index 18d8aa3043..ac104537d8 100644 --- a/Civi/Core/CiviEventDispatcher.php +++ b/Civi/Core/CiviEventDispatcher.php @@ -2,7 +2,7 @@ namespace Civi\Core; -use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher; +use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\Event; /** @@ -15,7 +15,7 @@ use Symfony\Component\EventDispatcher\Event; * * @see \CRM_Utils_Hook */ -class CiviEventDispatcher extends ContainerAwareEventDispatcher { +class CiviEventDispatcher extends EventDispatcher { const DEFAULT_HOOK_PRIORITY = -100; @@ -61,6 +61,34 @@ class CiviEventDispatcher extends ContainerAwareEventDispatcher { 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 */ @@ -89,6 +117,9 @@ class CiviEventDispatcher extends ContainerAwareEventDispatcher { case 'fail': throw new \RuntimeException("The dispatch policy prohibits event \"$eventName\"."); + case 'not-ready': + throw new \RuntimeException("CiviCRM has not bootstrapped sufficiently to fire event \"$eventName\"."); + default: throw new \RuntimeException("The dispatch policy for \"$eventName\" is unrecognized ($mode).");