This commit is contained in:
2023-04-04 15:21:20 +08:00
parent bc98a865af
commit 77dd313b12
+21 -5
View File
@@ -12,6 +12,20 @@ namespace Kiri\Abstracts;
use Kiri\Exception\ConfigException;
/**
* @param $key
* @param $try
* @return void
* @throws ConfigException
*/
function ConfigTry($key, $try): void
{
if ($try) {
throw new ConfigException(sprintf(Config::ERROR_MESSAGE, $key));
}
}
/**
* Class Config
* @package Kiri\Base
@@ -58,7 +72,7 @@ class Config extends Component
/**
* @param $key
* @param bool $try
* @param mixed|null $default
* @param mixed $default
* @return mixed
* @throws
*/
@@ -69,15 +83,17 @@ class Config extends Component
}
$array = explode('.', $key);
$data = static::$data[array_shift($array)] ?? null;
if (($data == null || !is_array($data)) && count($array) > 0) {
if ($data === null) {
return $default;
} else if (count($array) === 0) {
return $data;
} else if (!is_array($data)) {
return $default;
}
foreach ($array as $value) {
$data = $data[$value] ?? null;
if ($data === null) {
if ($try) {
throw new ConfigException(sprintf(self::ERROR_MESSAGE, $key));
}
ConfigTry($key, $try);
return $default;
}
}