场景
- App\Http\Kernel 在处理请求之前,完成了对几项核心bootstrap的实现, 下面来简单聊聊\Illuminate\Foundation\Bootstrap\LoadConfiguration::class
分析
if (file_exists($cached = $app->getCachedConfigPath())) { $items = require $cached; }
如果已经缓存了配置文件, 则直接读取缓存文件$app->instance('config', $config = new Repository($items));
则测config instance到容器,if (! isset($loadedFromCache)) { $this->loadConfigurationFiles($app, $config);}
如果没有缓存配置文件 则此时config instance的items属性是空的, 所以需要加载配置文件到容器$files = $this->getConfigurationFiles($app);
得到一个数组: 扫描config/*.php, 以文件名为keyforeach ($files as $key => $path) {$repository->set($key, require $path);}
设置config instance 的items属性
$app->detectEnvironment(function () use ($config) { return $config->get('app.env', 'production'); });
设置service container的env属性, 这个值是从config/app.php中读取
总结
- 其实这里实现的就是将config/*.php 读取到service container的过程