From f09c2800ef5b7ec3cfd400e177cf8be427ba8df6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Mon, 31 Aug 2020 22:54:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- system/Abstracts/Config.php | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/system/Abstracts/Config.php b/system/Abstracts/Config.php index 7f540e3b..967ec62d 100644 --- a/system/Abstracts/Config.php +++ b/system/Abstracts/Config.php @@ -34,14 +34,36 @@ class Config extends Component public static function get($key, $try = FALSE, $default = null) { $config = Snowflake::get()->config; - if (isset($config->data[$key])) { - return $config->data[$key]; - } else if ($default !== null) { - return $default; - } else if ($try) { - throw new ConfigException(str_replace(':key', $key, self::ERROR_MESSAGE)); + + if (strpos($key, '.') === false) { + if (isset($config->data[$key])) { + return $config->data[$key]; + } else if ($default !== null) { + return $default; + } else if ($try) { + throw new ConfigException(str_replace(':key', $key, self::ERROR_MESSAGE)); + } + return NULL; } - return NULL; + $keys = explode('.', $key); + + $parent = $config->data[array_shift($keys)] ?? null; + if (empty($parent)) { + return $default; + } + if (empty($keys) || !is_array($parent)) { + return $parent; + } + foreach ($keys as $key) { + if (!isset($parent[$key])) { + return $parent; + } + $parent = $parent[$key]; + if (empty($keys) || !is_array($parent)) { + return $parent; + } + } + return $parent; } /**