/**
 * 配置信息合并
 *
 * @param $arr1
 * @param $arr2
 * @return array
 */
if (!function_exists('array_recursive_merge')) {
    function array_recursive_merge($arr1, $arr2)
    {
        if(is_array($arr1) && is_array($arr2)) {
            foreach ($arr2 as $key => $item) {
                $arr1[$key] = array_recursive_merge($arr1[$key] ?? null, $item);
            }
            return $arr1;
        }
        return $arr2;
    }
}

//绑定配置文件加载事件
$app->afterBootstrapping(\Illuminate\Foundation\Bootstrap\LoadConfiguration::class, function($app){
    //检查是否存在config cache
    if($app->configurationIsCached()) {
        return;
    }

    $config = $app['config'];
    $url = $app['config']['config']['uri'] ?? null;
    if($url) {
        $appName = strtolower($app['config']['app']['name']);
        $env = strtolower($app['config']['app']['env']);
        $url .= $appName . '-' . $env . '.yml';
        $content = file_get_contents($url);
        if($content) {
            $content = \Symfony\Component\Yaml\Yaml::parse($content);
            foreach($content as $key=>$value) {
                $config[$key] = array_recursive_merge($config[$key]??null, $value);
            }
        }
    }

    //cache
    if($config['config']['cache'] ?? false) {
        $configPath = $app->getCachedConfigPath();
        (new Illuminate\Filesystem\Filesystem)->put(
            $configPath, 'all(), true).';'.PHP_EOL
        );
    }

});

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注