/** * @inheritDoc */ public static function loadFromFile($file) { if (false === ($xml = simplexml_load_file($file))) { throw new LogicException(Message::get(Message::DEFINITION_FORMAT_ERR, $file), Message::DEFINITION_FORMAT_ERR); } return static::xmlToArray($xml); }
/** * {@inheritDoc} * * @return ParameterInterface */ public function getResolver() { if (!$this->hasResolver()) { throw new NotFoundException(Message::get(Message::PARAM_RESOLVER_MISS), Message::PARAM_RESOLVER_MISS); } return $this->resolver; }
/** * Provides a static interface for container dynamic methods * * @param string $name method name * @param array $arguments arguments * @return mixed * @access public * @static * @internal */ public static function __callStatic($name, array $arguments) { $container = static::getContainer(); if (method_exists($container, $name)) { return call_user_func_array([$container, $name], $arguments); } throw new BadMethodCallException(Message::get(Message::METHOD_NOT_FOUND, get_called_class(), $name), Message::METHOD_NOT_FOUND); }
/** * @inheritDoc */ public function getContainer() { // found if ($this->hasContainer()) { return $this->container; } // not found throw new NotFoundException(Message::get(Message::CONTAINER_NOT_FOUND, get_class($this)), Message::CONTAINER_NOT_FOUND); }
/** * @inheritDoc */ public function get($id) { // has() will insert definitions into $container if ($this->has($id)) { // get service from container then return $this->getContainer()->get($id); } else { throw new NotFoundException(Message::get(Message::SERVICE_ID_NOT_FOUND, $id), Message::SERVICE_ID_NOT_FOUND); } }
/** * Get from the delegator * * {@inheritDoc} */ public function get($id) { /* @var $container InteropContainerInterface */ foreach ($this->getContainers() as $container) { if ($container->has($id)) { return $container->get($id); } } // not found throw new NotFoundException(Message::get(Message::SERVICE_ID_NOT_FOUND, $id), Message::SERVICE_ID_NOT_FOUND); }
/** * @inheritDoc * * services.s.json, * <code> * { * 'streamhandler': { * 'class' : [ * '\Phossa\Logger\Handler\StreamHandler', * [ '%logger.file%', 'debug' ] * ], * 'scope' : '__SINGLE__' * }, * ... * } * </code> * * parameters.p.json, * <code> * { * 'logger': { * 'file': '/var/local/app.log', * 'mail': { * 'to_address' : '*****@*****.**', * 'from_address' : '*****@*****.**', * 'subject' : 'App Logs' * } * } * } * </code> */ public static function loadFromFile($file) { // readin json content $json = file_get_contents($file); if (false === $json) { throw new LogicException(Message::get(Message::DEFINITION_NOT_FOUND, $file), Message::DEFINITION_NOT_FOUND); } if (!is_array($definitions = json_decode($json, true))) { throw new LogicException(Message::get(Message::DEFINITION_FORMAT_ERR, $file), Message::DEFINITION_FORMAT_ERR); } return $definitions; }
/** * Get the reference value * * @param ReferenceAbstract $reference * @param int $level current recursive level * @return mixed * @access protected */ protected function getReferenceValue(ReferenceAbstract $reference, $level = 0) { $name = $reference->getName(); // loop found if ($level > 2) { throw new NotFoundException(Message::get(Message::PARAMETER_LOOP_FOUND, $name), Message::PARAMETER_LOOP_FOUND); } // get service reference value if ($reference instanceof ServiceReference) { return $this->delegatedGet($name); // get parameter value, if value is another reference, go deeper } else { $val = $this->getParameter($name); if (is_string($val) && ($ref = $this->isReference($val))) { return $this->getReferenceValue($ref, ++$level); } return $val; } }
/** * Get this paramter's value either a string or an associate array * * ```php * $this->set('cache.dir', '/var/tmp'); * * // will return an array ['dir' => '/var/tmp']; * $result = $this->getParameter('cache'); * * // will return a string, 'var/tmp' * $result = $this->getParameter('cache.dir'); * ``` * * @param string $name parameter name * @return string|array * @throws NotFoundException if not found * @access protected */ protected function getParameter($name) { if ($this->hasResolver()) { $found = $this->getResolver()->get($name); } else { // break into parts by '.' $parts = explode('.', $name); $found = $this->parameters; while (null !== ($part = array_shift($parts))) { if (!isset($found[$part])) { $found = null; break; } $found = $found[$part]; } } if (null === $found) { throw new NotFoundException(Message::get(Message::PARAMETER_NOT_FOUND, $name), Message::PARAMETER_NOT_FOUND); } return $found; }
/** * Constructor, check EXTENION_CLASS set or not * * @throws LogicException if something goes wrong * @access public * @internal */ public function __construct() { if (get_called_class() !== static::EXTENSION_CLASS) { throw new LogicException(Message::get(Message::EXTENION_INVALID_CLASS, get_class($this)), Message::EXTENION_INVALID_CLASS); } }
/** * Get the provider object * * @param string|ProviderAbstract $providerOrClass class or object * @return ProviderAbstract * @throws LogicException * @access protected */ public function getProviderInstance($providerOrClass) { if (is_a($providerOrClass, ProviderAbstract::PROVIDER_CLASS, true)) { if (!is_object($providerOrClass)) { $providerOrClass = new $providerOrClass(); } return $providerOrClass; } else { throw new LogicException(Message::get(Message::EXT_PROVIDER_ERROR, $providerOrClass), Message::EXT_PROVIDER_ERROR); } }
/** * @inheritDoc * * services.s.php * <code> * <?php * use Phossa\Logger; * use Psr\Log\LogLevel; * * return [ * 'streamhandler' => [ * 'class' => [ * Logger\Handler\StreamHandler::class, * [ '%logger.file%', LogLevel::WARNING ], * ], * 'methods' => [ * [ 'setFormatter', ['@ansiFormatter@'] ] * ] * ], * * 'logger' => [ * 'class' => [ * Logger\Logger::class, // class * [ '%logger.channel%' ] // constructor arguments * ], * 'methods' => [ * [ 'addHandler', [ '@streamhandler@' ] ], * [ 'addDecorator', [ '@interpolate@' ] ], * [ 'addDecorator', [ '@profiler@' ] ] * ], * ], * * 'ansiFormatter' => [ * 'class' => function() { * return new Logger\Formatter\AnsiFormatter(); * }, * 'scope' => Container::SCOPE_SINGLE * ], * * // use closure directly * 'interpolate' => function() { * return new Logger\Decorator\InterpolateDecorator(); * }, * * // use array directly * 'profiler' => [ * Logger\Decorator\ProfileHandler::class * ], * ... * ]; * </code> * * parameters.p.php, * <code> * <?php * return [ * 'logger' => [ * 'channel' => 'myLogger', * 'file' => __DIR__.'/../app.log', * 'mail' => [ * 'to_address' => '*****@*****.**', * 'from_address' => '*****@*****.**', * 'subject' => 'App Logs' * ] * ] * ]; * </code> */ public static function loadFromFile($file) { if (!file_exists($file) || !is_array($definitions = (include $file))) { throw new LogicException(Message::get(Message::DEFINITION_NOT_FOUND, $file), Message::DEFINITION_NOT_FOUND); } return $definitions; }
/** * Check circular for get() * * @param string $id service id * @return void * @throws LogicException if circular found * @access protected */ protected function checkCircularMark($id) { // reference id "@$id@" of current service object $refId = $this->getServiceReferenceId($id); // circular detected if (isset($this->circular[$refId])) { throw new LogicException(Message::get(Message::SERVICE_CIRCULAR, $id), Message::SERVICE_CIRCULAR); // mark it } else { $this->circular[$refId] = ++$this->counter; } }
/** * Initialize service object by runing its defined methods * * @param string $id service id * @param object $service service object * @return void * @throws LogicException * @access protected */ protected function runDefinedMethods($id, $service) { try { if (isset($this->services[$id]['methods'])) { $methods = $this->services[$id]['methods']; foreach ($methods as $method) { if (!is_array($method) || !isset($method[0]) || !is_string($method[0])) { throw new LogicException(Message::get(Message::SERVICE_METHOD_ERROR, $id, isset($method[0]) ? $method[0] : ''), Message::SERVICE_METHOD_ERROR); } // execute with arguments $this->executeCallable([$service, $method[0]], isset($method[1]) ? (array) $method[1] : []); } } } catch (\Exception $e) { throw new LogicException($e->getMessage(), $e->getCode(), $e); } }
/** * Get loader class name base on suffix * * @param string $suffix * @param string $filename * @return string classname * @throws LogicException if not class not found * @access protected */ protected function getLoaderClass($suffix, $filename) { $class = __NAMESPACE__ . '\\Loader' . ucfirst($suffix); if (!class_exists($class)) { throw new LogicException(Message::get(Message::FILE_SUFFIX_UNKNOWN, $filename), Message::FILE_SUFFIX_UNKNOWN); } return $class; }