Laravel 的配置缓存被保留在 bootstrap/cache/config.php 文件中。这个文件就是把 config 文件夹的所有文件合并成了一个大的配置文件。config.php 间接返回一个数组,数组的键名对应 config 文件夹下的文件名,数组的值对应 config 文件夹下文件返回的配置信息。

源码剖析

应用到配置缓存文件的类:

class Application extends Container implements ApplicationContract, HttpKernelInterface{    // 获取配置缓存文件的门路    public function getCachedConfigPath()    {        return $this->normalizeCachePath('APP_CONFIG_CACHE', 'cache/config.php');    }    // 利用配置是否曾经缓存(即配置缓存文件是否存在)    public function configurationIsCached()    {        return file_exists($this->getCachedConfigPath());    }

调用到 getCachedConfigPath() 的类:

class LoadConfiguration{    // 利用启动    public function bootstrap(Application $app)    {        $items = [];        // 如果配置文件已缓存,则间接加载;否则遍历每个配置文件而后全副加载。        if (file_exists($cached = $app->getCachedConfigPath())) {            $items = require $cached;            $loadedFromCache = true;        }        // 遍历配置目录中的所有配置文件,并逐个加载到仓库中        // 使配置选项对于开发者来说在利用的各个局部中均可用        $app->instance('config', $config = new Repository($items));        if (! isset($loadedFromCache)) {            $this->loadConfigurationFiles($app, $config);        }        // 依据加载的配置值设置应用程序的环境。        $app->detectEnvironment(function () use ($config) {            return $config->get('app.env', 'production');        });        date_default_timezone_set($config->get('app.timezone', 'UTC'));        mb_internal_encoding('UTF-8');    }}

调用到 configurationIsCached() 的类:

class LoadEnvironmentVariables{    // 利用启动    public function bootstrap(Application $app)    {        // 如果配置缓存文件已存在,则间接返回        if ($app->configurationIsCached()) {            return;        }        // 自定义环境配置文件        $this->checkForSpecificEnvironmentFile($app);        // 加载零碎配置文件 .env        try {            $this->createDotenv($app)->safeLoad();        } catch (InvalidFileException $e) {            $this->writeErrorAndDie($e);        }    }    // 检测是否存在 APP_ENV 中匹配的自定义环境配置文件    protected function checkForSpecificEnvironmentFile($app)    {        if ($app->runningInConsole() && ($input = new ArgvInput)->hasParameterOption('--env')) {            if ($this->setEnvironmentFilePath(                $app, $app->environmentFile().'.'.$input->getParameterOption('--env')            )) {                return;            }        }        $environment = Env::get('APP_ENV');        if (! $environment) {            return;        }        $this->setEnvironmentFilePath(            $app, $app->environmentFile().'.'.$environment        );    }    // 加载自定义环境配置文件,如 .env.local    protected function setEnvironmentFilePath($app, $file)    {        if (file_exists($app->environmentPath().'/'.$file)) {            $app->loadEnvironmentFrom($file);            return true;        }        return false;    }    // 创立 Dotenv 实例    protected function createDotenv($app)    {        return Dotenv::create(            $app->environmentPath(),            $app->environmentFile(),            Env::getFactory()        );    }

总结

如果配置文件缓存曾经存在,则间接应用配置文件缓存。

如果配置文件缓存不存在,则先加载自定义配置文件,再加载零碎配置文件。