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
*/
*/
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
], self::DEFAULT_HOOK_PRIORITY);
}
}
- elseif (NULL === $eventName) {
- foreach ($this->listenerIds as $serviceEventName => $args) {
- $this->lazyLoad($serviceEventName);
- }
- }
}
/**
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;
- }
- }
- }
-
}