This commit is contained in:
2025-12-18 15:03:43 +08:00
parent 174d2f1659
commit 377a9c17c3
+42 -15
View File
@@ -819,7 +819,7 @@ if (!function_exists('config')) {
* @param null $default * @param null $default
* @return mixed * @return mixed
*/ */
function config($key, $default = NULL): mixed function config($key, mixed $default = NULL): mixed
{ {
return make(ConfigProvider::class)->get($key, $default); return make(ConfigProvider::class)->get($key, $default);
} }
@@ -864,20 +864,47 @@ if (!function_exists('di')) {
if (!function_exists('sweep')) { if (!function_exists('sweep')) {
/** /**
* @param string $configPath * @param string $configPath
* @return array|false|string|null * @return array
*/ */
function sweep(string $configPath = APP_PATH . 'config'): bool|array|string|null function sweep(string $configPath = APP_PATH . 'config'): array
{ {
$array = []; $array = [];
foreach (glob($configPath . '/*.php') as $config) {
$array = array_merge(require "$config", $array); // 检查目录是否存在
} if (!is_dir($configPath)) {
return $array; 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;
}
} }