/** * Attach an event handler to the stack. * * @param string $name Name of handler. * @param mixed $handler Callable handler or instance of ServiceHandler. * @param integer $priority Priotity to control notification order, (default = 10). * * @throws InvalidArgumentException If Handler is not callable or an instance of ServiceHandler. * * @return void */ public function attach($name, $handler, $priority=10) { if ($handler instanceof Zikula_ServiceHandler && $this->serviceManager && !$this->serviceManager->hasService($handler->getId())) { throw new InvalidArgumentException(sprintf('ServiceHandler (id:"%s") is not registered with the ServiceManager', $handler->getId())); } $priority = (int)$priority; if (!$handler instanceof Zikula_ServiceHandler && !is_callable($handler)) { // an error has occurred, so we'll generate a meaningful message. if (is_array($handler)) { $callableText = is_object($handler[0]) ? get_class($handler[0]) . "->$handler[1]()" : "$handler[0]::$handler[1]()"; } else { $callableText = "$handler()"; } throw new InvalidArgumentException(sprintf('Handler %s given is not a valid PHP callback or Zikula_ServiceHandler instance.', $callableText)); } if (!isset($this->handlers[$name][$priority])) { $this->handlers[$name][$priority] = array(); } $this->handlers[$name][$priority][] = $handler; // Reorder according to priority. ksort($this->handlers[$name]); $this->handlers[$name] = $this->handlers[$name]; }
/** * Build service. * * Builds event servicehandlers. If the service does not exist, it creates it * and adds it to the DI container. * * @param string $id * @param string $className * @param string $method * * @return Zikula_ServiceHandler */ public function buildService($id, $className, $method) { if (!$this->serviceManager->hasService($id)) { $definition = new Zikula_ServiceManager_Definition($className, array(new Zikula_ServiceManager_Reference($this->serviceId))); $this->serviceManager->registerService($id, $definition); } return new Zikula_ServiceHandler($id, $method); }
/** * Convenience hasService shortcut. * * @param string $id Service name. * * @return boolean */ protected function hasService($id) { return $this->serviceManager->hasService($id); }