| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Foundation\Bootstrap;
|
|
|
4 |
|
|
|
5 |
use Exception;
|
|
|
6 |
use Illuminate\Config\Repository;
|
|
|
7 |
use Illuminate\Contracts\Config\Repository as RepositoryContract;
|
|
|
8 |
use Illuminate\Contracts\Foundation\Application;
|
|
|
9 |
use SplFileInfo;
|
|
|
10 |
use Symfony\Component\Finder\Finder;
|
|
|
11 |
|
|
|
12 |
class LoadConfiguration
|
|
|
13 |
{
|
|
|
14 |
/**
|
|
|
15 |
* Bootstrap the given application.
|
|
|
16 |
*
|
|
|
17 |
* @param \Illuminate\Contracts\Foundation\Application $app
|
|
|
18 |
* @return void
|
|
|
19 |
*/
|
|
|
20 |
public function bootstrap(Application $app)
|
|
|
21 |
{
|
|
|
22 |
$items = [];
|
|
|
23 |
|
|
|
24 |
// First we will see if we have a cache configuration file. If we do, we'll load
|
|
|
25 |
// the configuration items from that file so that it is very quick. Otherwise
|
|
|
26 |
// we will need to spin through every configuration file and load them all.
|
|
|
27 |
if (file_exists($cached = $app->getCachedConfigPath())) {
|
|
|
28 |
$items = require $cached;
|
|
|
29 |
|
|
|
30 |
$loadedFromCache = true;
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
// Next we will spin through all of the configuration files in the configuration
|
|
|
34 |
// directory and load each one into the repository. This will make all of the
|
|
|
35 |
// options available to the developer for use in various parts of this app.
|
|
|
36 |
$app->instance('config', $config = new Repository($items));
|
|
|
37 |
|
|
|
38 |
if (! isset($loadedFromCache)) {
|
|
|
39 |
$this->loadConfigurationFiles($app, $config);
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
// Finally, we will set the application's environment based on the configuration
|
|
|
43 |
// values that were loaded. We will pass a callback which will be used to get
|
|
|
44 |
// the environment in a web context where an "--env" switch is not present.
|
|
|
45 |
$app->detectEnvironment(fn () => $config->get('app.env', 'production'));
|
|
|
46 |
|
|
|
47 |
date_default_timezone_set($config->get('app.timezone', 'UTC'));
|
|
|
48 |
|
|
|
49 |
mb_internal_encoding('UTF-8');
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Load the configuration items from all of the files.
|
|
|
54 |
*
|
|
|
55 |
* @param \Illuminate\Contracts\Foundation\Application $app
|
|
|
56 |
* @param \Illuminate\Contracts\Config\Repository $repository
|
|
|
57 |
* @return void
|
|
|
58 |
*
|
|
|
59 |
* @throws \Exception
|
|
|
60 |
*/
|
|
|
61 |
protected function loadConfigurationFiles(Application $app, RepositoryContract $repository)
|
|
|
62 |
{
|
|
|
63 |
$files = $this->getConfigurationFiles($app);
|
|
|
64 |
|
|
|
65 |
if (! isset($files['app'])) {
|
|
|
66 |
throw new Exception('Unable to load the "app" configuration file.');
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
foreach ($files as $key => $path) {
|
|
|
70 |
$repository->set($key, require $path);
|
|
|
71 |
}
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* Get all of the configuration files for the application.
|
|
|
76 |
*
|
|
|
77 |
* @param \Illuminate\Contracts\Foundation\Application $app
|
|
|
78 |
* @return array
|
|
|
79 |
*/
|
|
|
80 |
protected function getConfigurationFiles(Application $app)
|
|
|
81 |
{
|
|
|
82 |
$files = [];
|
|
|
83 |
|
|
|
84 |
$configPath = realpath($app->configPath());
|
|
|
85 |
|
|
|
86 |
foreach (Finder::create()->files()->name('*.php')->in($configPath) as $file) {
|
|
|
87 |
$directory = $this->getNestedDirectory($file, $configPath);
|
|
|
88 |
|
|
|
89 |
$files[$directory.basename($file->getRealPath(), '.php')] = $file->getRealPath();
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
ksort($files, SORT_NATURAL);
|
|
|
93 |
|
|
|
94 |
return $files;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
/**
|
|
|
98 |
* Get the configuration file nesting path.
|
|
|
99 |
*
|
|
|
100 |
* @param \SplFileInfo $file
|
|
|
101 |
* @param string $configPath
|
|
|
102 |
* @return string
|
|
|
103 |
*/
|
|
|
104 |
protected function getNestedDirectory(SplFileInfo $file, $configPath)
|
|
|
105 |
{
|
|
|
106 |
$directory = $file->getPath();
|
|
|
107 |
|
|
|
108 |
if ($nested = trim(str_replace($configPath, '', $directory), DIRECTORY_SEPARATOR)) {
|
|
|
109 |
$nested = str_replace(DIRECTORY_SEPARATOR, '.', $nested).'.';
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
return $nested;
|
|
|
113 |
}
|
|
|
114 |
}
|