<?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);
}
$object = Loader::factory($type, '\\think\\config\\driver\\', $config);
return $this->set($object->parse(), $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;
}
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);
return isset($this->config[$name]) ? $this->config[$name] : [];
}
public function get($name = null, $default = 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;
foreach ($name as $val) {
if (isset($config[$val])) {
$config = $config[$val];
} else {
return $default;
}
}
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);
}
}