diff --git a/function.php b/function.php index 51d23756..01e03cb9 100644 --- a/function.php +++ b/function.php @@ -819,7 +819,7 @@ if (!function_exists('config')) { * @param null $default * @return mixed */ - function config($key, $default = NULL): mixed + function config($key, mixed $default = NULL): mixed { return make(ConfigProvider::class)->get($key, $default); } @@ -864,20 +864,47 @@ if (!function_exists('di')) { if (!function_exists('sweep')) { - - /** - * @param string $configPath - * @return array|false|string|null - */ - function sweep(string $configPath = APP_PATH . 'config'): bool|array|string|null - { - $array = []; - foreach (glob($configPath . '/*.php') as $config) { - $array = array_merge(require "$config", $array); - } - return $array; - } - + + /** + * @param string $configPath + * @return array + */ + function sweep(string $configPath = APP_PATH . 'config'): array + { + $array = []; + + // 检查目录是否存在 + if (!is_dir($configPath)) { + throw new InvalidArgumentException("Config directory does not exist: {$configPath}"); + } + + // 获取所有PHP配置文件 + $configFiles = glob($configPath . '/*.php'); + foreach ($configFiles as $config) { + // 使用 pathinfo 更安全地获取文件名 + $filename = pathinfo($config, PATHINFO_FILENAME); + $key = strtolower($filename); + + // 验证文件是否可读 + if (!is_readable($config)) { + throw new RuntimeException("Cannot read config file: {$config}"); + } + + // 使用 include 而不是 require_once,这样不会跳过已加载的文件 + // 在配置加载场景中,通常希望每次都重新加载 + $configData = include $config; + + if (!is_array($configData)) { + continue; + } + + $array[$key] = $configData; + } + + return $array; + } + + }