LoadConfiguration.php
TLDR
The file LoadConfiguration.php
is a part of the Illuminate\Foundation\Bootstrap namespace in the Laravel framework. It contains the LoadConfiguration
class, which is responsible for loading and merging configuration files for the application. It boots up the application by loading the configuration files from the cache if available, or by loading them directly from the configuration directory.
Methods
bootstrap
This method is responsible for bootstrapping the given application by loading the configuration files. It first checks if there is a cached configuration file and loads the items from that file. If there is no cached configuration file, it loads the configuration files directly from the configuration directory. It then sets the application's environment based on the loaded configuration values and sets the default timezone and internal encoding.
loadConfigurationFiles
This method is called by the bootstrap
method and is responsible for loading the configuration items from all the files. It retrieves the configuration files using the getConfigurationFiles
method and loads each file into the repository. It merges the loaded configuration with the base configuration and sets the merged configuration items in the repository.
loadConfigurationFile
This method is called by the loadConfigurationFiles
method and is responsible for loading a given configuration file. It loads the configuration file using require
, merges it with the base configuration if necessary, and sets the configuration items in the repository.
getConfigurationFiles
This method is called by the loadConfigurationFiles
method and is responsible for retrieving all the configuration files for the application. It uses the Symfony Finder component to search for *.php files in the configuration directory and returns an array of file paths.
getNestedDirectory
This method is used by the getConfigurationFiles
method to get the nested directory path for a given configuration file. It calculates the nested directory path based on the configuration file's path and the configuration directory path.
getBaseConfiguration
This method is called by the loadConfigurationFiles
method and is responsible for retrieving the base configuration files. It searches for *.php files in a specific directory and returns an array of base configuration items.
Classes
There are no additional classes in this file.
<?php
namespace Illuminate\Foundation\Bootstrap;
use Exception;
use Illuminate\Config\Repository;
use Illuminate\Contracts\Config\Repository as RepositoryContract;
use Illuminate\Contracts\Foundation\Application;
use SplFileInfo;
use Symfony\Component\Finder\Finder;
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;
$app->instance('config_loaded_from_cache', $loadedFromCache = true);
}
// 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.
$app->instance('config', $config = new Repository($items));
if (! isset($loadedFromCache)) {
$this->loadConfigurationFiles($app, $config);
}
// Finally, we will set the application's environment based on the configuration
// values that were loaded. We will pass a callback which will be used to get
// the environment in a web context where an "--env" switch is not present.
$app->detectEnvironment(fn () => $config->get('app.env', 'production'));
date_default_timezone_set($config->get('app.timezone', 'UTC'));
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 $repository
* @return void
*
* @throws \Exception
*/
protected function loadConfigurationFiles(Application $app, RepositoryContract $repository)
{
$files = $this->getConfigurationFiles($app);
// if (! isset($files['app'])) {
// throw new Exception('Unable to load the "app" configuration file.');
// }
$base = $this->getBaseConfiguration();
foreach ($files as $name => $path) {
$base = $this->loadConfigurationFile($repository, $name, $path, $base);
}
foreach ($base as $name => $config) {
$repository->set($name, $config);
}
}
/**
* Load the given configuration file.
*
* @param \Illuminate\Contracts\Config\Repository $repository
* @param string $name
* @param string $path
* @param array $base
* @return array
*/
protected function loadConfigurationFile(RepositoryContract $repository, $name, $path, array $base)
{
$config = require $path;
if (isset($base[$name])) {
$config = array_merge($base[$name], $config);
unset($base[$name]);
}
$repository->set($name, $config);
return $base;
}
/**
* Get all of the configuration files for the application.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @return array
*/
protected function getConfigurationFiles(Application $app)
{
$files = [];
$configPath = realpath($app->configPath());
foreach (Finder::create()->files()->name('*.php')->in($configPath) as $file) {
$directory = $this->getNestedDirectory($file, $configPath);
$files[$directory.basename($file->getRealPath(), '.php')] = $file->getRealPath();
}
ksort($files, SORT_NATURAL);
return $files;
}
/**
* Get the configuration file nesting path.
*
* @param \SplFileInfo $file
* @param string $configPath
* @return string
*/
protected function getNestedDirectory(SplFileInfo $file, $configPath)
{
$directory = $file->getPath();
if ($nested = trim(str_replace($configPath, '', $directory), DIRECTORY_SEPARATOR)) {
$nested = str_replace(DIRECTORY_SEPARATOR, '.', $nested).'.';
}
return $nested;
}
/**
* Get the base configuration files.
*
* @return array
*/
protected function getBaseConfiguration()
{
$config = [];
foreach (Finder::create()->files()->name('*.php')->in(__DIR__.'/../../../../config') as $file) {
$config[basename($file->getRealPath(), '.php')] = require $file->getRealPath();
}
return $config;
}
}