<?php<liu21st@gmail.com>
namespace think;
class Config implements \ArrayAccess
{
private $config = [];
private $prefix = 'app';
public function setDefaultPrefix($prefix)
{
$this->prefix = $prefix;
}
public function parse($config, $type = '', $name = '')
{
if (empty($type)) {
$type = pathinfo($config, PATHINFO_EXTENSION);
}
$class = false !== strpos($type, '\\') ? $type : '\\think\\config\\driver\\' . ucwords($type);
return $this->set((new $class())->parse($config), $name);
}
public function load($file, $name = '')
{
if (is_file($file)) {
$name = strtolower($name);
$type = pathinfo($file, PATHINFO_EXTENSION);
if ('php' == $type) {
return $this->set(include $file, $name);
} elseif ('yaml' == $type && function_exists('yaml_parse_file')) {
return $this->set(yaml_parse_file($file), $name);
}
return $this->parse($file, $type, $name);
}
return $this->config;
}
protected function autoLoad($name)
{
$module = Container::get('request')->module();
$module = $module ? $module . DIRECTORY_SEPARATOR : '';
$app = Container::get('app');
$path = $app->getAppPath() . $module;
if (is_dir($path . 'config')) {
$file = $path . 'config' . DIRECTORY_SEPARATOR . $name . $app->getConfigExt();
} elseif (is_dir($app->getConfigPath() . $module)) {
$file = $app->getConfigPath() . $module . $name . $app->getConfigExt();
}
if (isset($file) && is_file($file)) {
$this->load($file, $name);
}
}
public function has($name)
{
if (!strpos($name, '.')) {
$name = $this->prefix . '.' . $name;
}
return !is_null($this->get($name)) ? true : false;
}
public function pull($name)
{
$name = strtolower($name);
if (!isset($this->config[$name])) {
$this->autoLoad($name);
}
return isset($this->config[$name]) ? $this->config[$name] : [];
}
public function get($name = null)
{
if (empty($name)) {
return $this->config;
}
if (!strpos($name, '.')) {
$name = $this->prefix . '.' . $name;
} elseif ('.' == substr($name, -1)) {
return $this->pull(substr($name, 0, -1));
}
$name = explode('.', $name);
$name[0] = strtolower($name[0]);
$config = $this->config;
if (!isset($config[$name[0]])) {
$this->autoLoad($name[0]);
}
foreach ($name as $val) {
if (isset($config[$val])) {
$config = $config[$val];
} else {
return;
}
}
return $config;
}
public function set($name, $value = null)
{
if (is_string($name)) {
if (!strpos($name, '.')) {
$name = $this->prefix . '.' . $name;
}
$name = explode('.', $name, 3);
if (count($name) == 2) {
$this->config[strtolower($name[0])][$name[1]] = $value;
} else {
$this->config[strtolower($name[0])][$name[1]][$name[2]] = $value;
}
return $value;
} elseif (is_array($name)) {
if (!empty($value)) {
if (isset($this->config[$value])) {
$result = array_merge($this->config[$value], $name);
} else {
$result = $name;
}
$this->config[$value] = $result;
} else {
$result = $this->config = array_merge($this->config, $name);
}
} else {
$result = $this->config;
}
return $result;
}
public function remove($name)
{
if (!strpos($name, '.')) {
$name = $this->prefix . '.' . $name;
}
$name = explode('.', $name, 3);
if (count($name) == 2) {
unset($this->config[strtolower($name[0])][$name[1]]);
} else {
unset($this->config[strtolower($name[0])][$name[1]][$name[2]]);
}
}
public function reset($prefix = '')
{
if ('' === $prefix) {
$this->config = [];
} else {
$this->config[$prefix] = [];
}
}
public function __set($name, $value)
{
return $this->set($name, $value);
}
public function __get($name)
{
return $this->get($name);
}
public function __isset($name)
{
return $this->has($name);
}
public function offsetSet($name, $value)
{
$this->set($name, $value);
}
public function offsetExists($name)
{
return $this->has($name);
}
public function offsetUnset($name)
{
$this->remove($name);
}
public function offsetGet($name)
{
return $this->get($name);
}
}