/** * Set cache directory * * @access public */ public function __construct($dir) { if (!is_dir($dir)) { trigger_error(Message::get(Message::CACHE_DIR_INVALID, $dir), \E_WARNING); } if (!is_writable($dir)) { trigger_error(Message::get(Message::CACHE_DIR_NONWRITABLE, $dir), \E_WARNING); } $this->cache_dir = rtrim($dir, '/\\') . \DIRECTORY_SEPARATOR; }
/** * {@inheritDoc} */ public static function load($path) { if (!is_readable($path)) { throw new LogicException(Message::get(Message::CONFIG_LOAD_ERROR, $path), Message::CONFIG_LOAD_ERROR); } try { $data = @parse_ini_file($path, true); } catch (\Exception $exception) { throw new LogicException(Message::get(Message::CONFIG_FORMAT_ERROR), Message::CONFIG_FORMAT_ERROR); } if (!$data) { $error = error_get_last(); throw new LogicException(Message::get($error['message']), Message::CONFIG_FORMAT_ERROR); } return $data; }
/** * {@inheritDoc} */ public static function load($path) { if (!is_readable($path)) { throw new LogicException(Message::get(Message::CONFIG_LOAD_ERROR, $path), Message::CONFIG_LOAD_ERROR); } $data = @json_decode(file_get_contents($path), true); if (json_last_error() !== \JSON_ERROR_NONE) { if (function_exists('json_last_error_msg')) { $message = json_last_error_msg(); } else { $message = Message::get(Message::CONFIG_FORMAT_ERROR); } throw new LogicException($message, Message::CONFIG_FORMAT_ERROR); } return $data; }
/** * {@inheritDoc} */ public static function load($path) { try { $data = (include $path); } catch (\Exception $exception) { throw new LogicException(Message::get(Message::CONFIG_LOAD_ERROR, $path), Message::CONFIG_LOAD_ERROR, $exception); } // callable if (is_callable($data)) { $data = $data(); } if (!is_array($data)) { throw new LogicException(Message::get(Message::CONFIG_FORMAT_ERROR), Message::CONFIG_FORMAT_ERROR); } return $data; }
/** * {@inheritDoc} */ public static function load($path) { if (!is_readable($path)) { throw new LogicException(Message::get(Message::CONFIG_LOAD_ERROR, $path), Message::CONFIG_LOAD_ERROR); } libxml_use_internal_errors(true); $data = simplexml_load_file($path, null, \LIBXML_NOERROR); if (false === $data) { $errors = libxml_get_errors(); $error = array_pop($errors); $message = $error->message; throw new LogicException($message, Message::CONFIG_FORMAT_ERROR); } $data = json_decode(json_encode($data), true); return $data; }
/** * dereferencing string like '${USER}' into 'realUser' * * @param string $value * @return string * @throws LogicException if error happens * @access protected */ protected function deReference($value) { $regex = sprintf('/\\${(%s)}/', $this->name_pattern); while (false !== strpos($value, '${')) { $value = preg_replace_callback($regex, function ($matched) { $env = $this->matchEnv($matched[1]); if (false === $env) { throw new LogicException(Message::get(Message::CONFIG_ENV_UNKNOWN, $matched[1]), Message::CONFIG_ENV_UNKNOWN); } else { return $env; } }, $value); } return $value; }
/** * Throw exception if not a string * * @param string $key * @throws InvalidArgumentException if $key not a string * @access public */ protected function exceptionIfNotString($key) { if (!is_string($key)) { throw new InvalidArgumentException(Message::get(Message::CONFIG_KEY_INVALID, $key), Message::CONFIG_KEY_INVALID); } }
/** * Set config file type * * @param string $fileType * @return self * @throws InvalidArgumentException if unsupported file type * @access protected */ protected function setFileType($fileType) { // validate file type $class = __NAMESPACE__ . '\\' . ucfirst($fileType) . 'Loader'; if (!class_exists($class, true)) { throw new InvalidArgumentException(Message::get(Message::CONFIG_SUFFIX_UNKNOWN, $fileType), Message::CONFIG_SUFFIX_UNKNOWN); } $this->file_type = $fileType; $this->loader_class = $class; return $this; }
/** * {@inheritDoc} */ public function deReference($str) { // for loop detection in recursive dereference $level = 0; // unresolved(unknown) reference $unresolved = []; // find reference in the string RECURSIVELY while ($this->hasReference($str)) { // extract references $res = $this->extractReference($str); if (empty($res)) { break; } // loop found if ($level++ > 10) { throw new LogicException(Message::get(Message::CONFIG_REF_LOOP, $str), Message::CONFIG_REF_LOOP); } // all unresolved if ($res == $unresolved) { break; } foreach ($res as $ref => $name) { if (!isset($unresolved[$ref])) { $value = $this->getValue($name); } else { continue; } // try resolve if (is_null($value)) { $value = $this->resolveUnResolved($name); } // unresolved if (is_null($value)) { $unresolved[$ref] = $name; // value is string } elseif (is_string($value)) { $str = str_replace($ref, $value, $str); // value is array or object } elseif ($str === $ref) { return $value; // malformed, array|object + string found } else { throw new LogicException(Message::get(Message::CONFIG_REF_MALFORM, $str), Message::CONFIG_REF_MALFORM); } } } return $str; }