Illuminate/Foundation/Bootstrap/LoadConfiguration.php


<?php namespace Illuminate\Foundation\Bootstrap;

use Illuminate\Config\Repository;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Config\Repository as RepositoryContract;

class LoadConfiguration {

    
/** * Bootstrap the given application. * 引导给定的应用程序。 * * @param \Illuminate\Contracts\Foundation\Application $app * @return void */ public function bootstrap(Application $app) { $items = []; // First we will see if we have a cache configuration file. If we do, we'll load // the configuration items from that file so that it is very quick. Otherwise // we will need to spin through every configuration file and load them all. // 若存在缓存的配置文件 if (file_exists($cached = $app->getCachedConfigPath())) { // 引入配置参数数组 $items = require $cached; // 标记为已从缓存载入 $loadedFromCache = true; } // 注册 config 共享实例 $app->instance('config', $config = new Repository($items)); // Next we will spin through all of the configuration files in the configuration // directory and load each one into the repository. This will make all of the // options available to the developer for use in various parts of this app. // 若未从缓存载入 if ( ! isset($loadedFromCache)) { // 从所有文件加载配置项 $this->loadConfigurationFiles($app, $config); } // 设定默认时区 date_default_timezone_set($config['app.timezone']); // 设置内部字符编码 mb_internal_encoding('UTF-8'); }
/** * Load the configuration items from all of the files. * 从所有文件加载配置项。 * * @param \Illuminate\Contracts\Foundation\Application $app * @param \Illuminate\Contracts\Config\Repository $config * @return void */ protected function loadConfigurationFiles(Application $app, RepositoryContract $config) { foreach ($this->getConfigurationFiles($app) as $key => $path) { $config->set($key, require $path); } }
/** * Get all of the configuration files for the application. * * @param \Illuminate\Contracts\Foundation\Application $app * @return array */ protected function getConfigurationFiles(Application $app) { $files = []; foreach (Finder::create()->files()->name('*.php')->in($app->configPath()) as $file) { $nesting = $this->getConfigurationNesting($file); $files[$nesting.basename($file->getRealPath(), '.php')] = $file->getRealPath(); } return $files; }
/** * Get the configuration file nesting path. * * @param \Symfony\Component\Finder\SplFileInfo $file * @return string */ private function getConfigurationNesting(SplFileInfo $file) { $directory = dirname($file->getRealPath()); if ($tree = trim(str_replace(config_path(), '', $directory), DIRECTORY_SEPARATOR)) { $tree = str_replace(DIRECTORY_SEPARATOR, '.', $tree).'.'; } return $tree; } }