public static function get(string $key = null, mixed $default = null): mixed
{
if ($key === null) {
return static::$config;
}
$keyArray = explode('.', $key);
$value = static::$config;
$found = true;
foreach ($keyArray as $index) {
if (!isset($value[$index])) {
if (static::$loaded) {
return $default;
}
$found = false;
break;
}
$value = $value[$index];
}
if ($found) {
return $value;
}
return static::read($key, $default);
}
修改成:
public static function get(string $key = null, $default = null)
{
static $cache = [];
// Return full config if key is null
if ($key === null) {
return static::$config;
}
// Return cached value if exists
if (isset($cache[$key])) {
return $cache[$key];
}
$keyArray = explode('.', $key);
$value = static::$config;
foreach ($keyArray as $index) {
if (!isset($value[$index])) {
if (static::$loaded) {
// Save the default value in cache
$cache[$key] = $default;
return $default;
}
return static::read($key, $default);
}
$value = $value[$index];
}
// Save the value in cache
$cache[$key] = $value;
return $value;
}