This commit is contained in:
2020-09-03 15:33:55 +08:00
parent cef63a50fc
commit bec1a70c5b
+20 -6
View File
@@ -22,7 +22,7 @@ class Config extends Component
const ERROR_MESSAGE = 'The not find %s in app configs.'; const ERROR_MESSAGE = 'The not find %s in app configs.';
public $data; protected $data;
/** /**
@@ -33,6 +33,17 @@ class Config extends Component
return $this->data; return $this->data;
} }
/**
* @param $key
* @param $value
* @return mixed
*/
public function setData($key, $value)
{
return $this->data[$key] = $value;
}
/** /**
* @param $key * @param $key
* @param bool $try * @param bool $try
@@ -43,17 +54,20 @@ class Config extends Component
public static function get($key, $try = FALSE, $default = null) public static function get($key, $try = FALSE, $default = null)
{ {
$explode = explode('.', $key); $explode = explode('.', $key);
$instance = Snowflake::app()->getConfig()->getData(); $instance = Snowflake::app()->config->getData();
foreach ($explode as $index => $value) { foreach ($explode as $index => $value) {
if (empty($value)) { if (empty($value)) {
continue; continue;
} }
if (!isset($instance[$value]) && $try) { if (!isset($instance[$value])) {
throw new ConfigException(sprintf(self::ERROR_MESSAGE, $value)); if ($try) {
throw new ConfigException(sprintf(self::ERROR_MESSAGE, $key));
}
return $default;
} }
$instance = $instance[$value]; $instance = $instance[$value];
if (!is_array($instance) && $index + 1 < count($explode)) { if (!is_array($instance) && $index + 1 < count($explode)) {
throw new ConfigException(sprintf(self::ERROR_MESSAGE, $value)); throw new ConfigException(sprintf(self::ERROR_MESSAGE, $key));
} }
} }
return empty($instance) ? $default : $instance; return empty($instance) ? $default : $instance;
@@ -68,7 +82,7 @@ class Config extends Component
public static function set($key, $value) public static function set($key, $value)
{ {
$config = Snowflake::app()->config; $config = Snowflake::app()->config;
return $config->data[$key] = $value; return $config->setData($key, $value);
} }
/** /**