This commit is contained in:
2023-04-04 15:15:30 +08:00
parent 0c2a419d0b
commit bc98a865af
+9 -11
View File
@@ -64,26 +64,24 @@ class Config extends Component
*/ */
public static function get($key, mixed $default = null, bool $try = FALSE): mixed public static function get($key, mixed $default = null, bool $try = FALSE): mixed
{ {
$instance = static::$data;
if (!str_contains($key, '.')) { if (!str_contains($key, '.')) {
return $instance[$key] ?? $default; return static::$data[$key] ?? $default;
} }
foreach (explode('.', $key) as $value) { $array = explode('.', $key);
if (empty($value)) { $data = static::$data[array_shift($array)] ?? null;
continue; if (($data == null || !is_array($data)) && count($array) > 0) {
return $default;
} }
if (!isset($instance[$value])) { foreach ($array as $value) {
$data = $data[$value] ?? null;
if ($data === null) {
if ($try) { if ($try) {
throw new ConfigException(sprintf(self::ERROR_MESSAGE, $key)); throw new ConfigException(sprintf(self::ERROR_MESSAGE, $key));
} }
return $default; return $default;
} }
if (!is_array($instance[$value])) {
return $instance[$value];
} }
$instance = $instance[$value]; return $data;
}
return empty($instance) ? $default : $instance;
} }
/** /**