/** * {@inheritDoc} */ public function getContainer() { if ($this->hasContainer()) { return $this->container; } throw new NotFoundException(Message::get(Message::DI_CONTAINER_NOTFOUND), Message::DI_CONTAINER_NOTFOUND); }
/** * {@inheritDoc} */ public function set($id, $value) { if ($this->isWritable()) { return $this->writable->set($id, $value); } else { throw new RuntimeException(Message::get(Message::DI_CONTAINER_READONLY, $id), Message::DI_CONTAINER_READONLY); } }
/** * Locate a service from the container * * ```php * // the global config object * $config = Service::config(); * * // the container * $container = Service::container(); * ``` * * @param string $method object id actually * @param array $params * @return object * @throws NotFoundException if container not set or object not found * @throws RuntimeException if object instantiation error * @access public * @api */ public static function __callstatic($method, array $params) { if (static::$container) { // append scope if provided if (!isset($params[0])) { $method .= '@' . $params[0]; } return static::$container->get($method); } // container not set yet throw new NotFoundException(Message::get(Message::DI_CONTAINER_NOTFOUND), Message::DI_CONTAINER_NOTFOUND); }
/** * {@inheritDoc} */ public function set($id, $value) { if ($this->isWritable()) { list($rawId, $scope) = $this->splitId($id); return $this->getResolver()->setService($rawId, '' === $scope ? $value : $this->scopedData($value, $scope)); } else { throw new RuntimeException(Message::get(Message::DI_CONTAINER_READONLY, $id), Message::DI_CONTAINER_READONLY); } }
/** * Create the instance with loop detection * * Loop: an instance depends on itself in the creation chain. * * @param string $rawId * @param array $args arguments for the constructor if any * @return object * @throws LogicException if instantiation goes wrong or loop detected * @access protected */ protected function createInstance($rawId, array $args) { // conver 'service_id' to '#service_id' $serviceId = ObjectResolver::getServiceId($rawId); if (isset($this->loop[$serviceId])) { throw new LogicException(Message::get(Message::DI_LOOP_DETECTED, $rawId), Message::DI_LOOP_DETECTED); } else { $this->loop[$serviceId] = ++$this->counter; $obj = $this->getFactory()->createInstance($rawId, $args); unset($this->loop[$serviceId]); return $obj; } }
/** * Returns [$object, $method] if it is a callable, otherwise returns $method * * @param mixed $object * @param mixed $method * @return bool * @access protected */ protected function getObjectMethod($object, $method) { if (is_string($method) && method_exists($object, $method)) { return [$object, $method]; } elseif (is_callable($method)) { return $method; } else { throw new LogicException(Message::get(Message::DI_CALLABLE_BAD, $method), Message::DI_CALLABLE_BAD); } }