1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Application\Bootstrap\Bootstrapers; |
4
|
|
|
|
5
|
|
|
use Nip\Application; |
6
|
|
|
use Nip\Config\Config; |
7
|
|
|
use Nip\Config\Factory; |
8
|
|
|
use Symfony\Component\Finder\Finder; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class LoadConfiguration |
12
|
|
|
* |
13
|
|
|
* @package Nip\Application\Bootstrap\Bootstrapers |
14
|
|
|
*/ |
15
|
|
|
class LoadConfiguration extends AbstractBootstraper |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Bootstrap the given application. |
19
|
|
|
* |
20
|
|
|
* @param Application $app |
21
|
|
|
* @return void |
22
|
|
|
*/ |
23
|
|
|
public function bootstrap(Application $app) |
24
|
|
|
{ |
25
|
|
|
$items = []; |
26
|
|
|
// First we will see if we have a cache configuration file. If we do, we'll load |
27
|
|
|
// the configuration items from that file so that it is very quick. Otherwise |
28
|
|
|
// we will need to spin through every configuration file and load them all. |
29
|
|
|
// if (file_exists($cached = $app->getCachedConfigPath())) { |
30
|
|
|
// $items = require $cached; |
31
|
|
|
// $loadedFromCache = true; |
32
|
|
|
// } |
33
|
|
|
|
34
|
|
|
// Next we will spin through all of the configuration files in the configuration |
35
|
|
|
// directory and load each one into the repository. This will make all of the |
36
|
|
|
// options available to the developer for use in various parts of this app. |
37
|
|
|
$app->share('config', $config = new Config($items, true)); |
38
|
|
|
|
39
|
|
|
// if (!isset($loadedFromCache)) { |
|
|
|
|
40
|
|
|
$this->loadConfigurationFiles($app, $config); |
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(function () use ($config) { |
46
|
|
|
// return $config->get('app.env', 'production'); |
47
|
|
|
// }); |
48
|
|
|
date_default_timezone_set($config->get('app.timezone', 'UTC')); |
49
|
|
|
mb_internal_encoding('UTF-8'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Load the configuration items from all of the files. |
54
|
|
|
* |
55
|
|
|
* @param Application $app |
56
|
|
|
* @param Config $repository |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
protected function loadConfigurationFiles(Application $app, Config $repository) |
60
|
|
|
{ |
61
|
|
|
Factory::fromFiles($repository, $this->getConfigurationFiles($app)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get all of the configuration files for the application. |
66
|
|
|
* |
67
|
|
|
* @param Application $app |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
protected function getConfigurationFiles(Application $app) |
71
|
|
|
{ |
72
|
|
|
$files = []; |
73
|
|
|
$configPath = realpath($app->configPath()); |
74
|
|
|
foreach (Finder::create()->files()->name('*.php')->in($configPath) as $file) { |
75
|
|
|
$files[basename($file->getRealPath(), '.php')] = $file->getRealPath(); |
76
|
|
|
} |
77
|
|
|
return $files; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.