/
Config.php
100 lines (84 loc) · 2.52 KB
/
Config.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace Aura\Framework;
use Aura\Autoload\Loader;
use Aura\Di\Container;
class Config
{
public $system;
public $loader;
public $di;
public $files;
public function __construct(System $system, Loader $loader, Container $di)
{
$this->system = $system;
$this->loader = $loader;
$this->di = $di;
$this->mode = empty($_ENV['AURA_CONFIG_MODE'])
? 'default'
: $_ENV['AURA_CONFIG_MODE'];
}
public function getMode()
{
return $this->mode;
}
public function getFiles()
{
return $this->files;
}
public function exec()
{
$cache_file = $this->getCacheFile();
if ($cache_file) {
$this->load($cache_file);
} else {
$this->loadFromPackages();
}
$this->loadMode();
}
public function getCacheFile()
{
$file = $this->system->getTmpPath("cache/config/{$this->mode}.php");
if (is_readable($file)) {
return $file;
}
}
public function loadFromPackages()
{
$package_glob = $this->system->getPackagePath('*');
$package_list = glob($package_glob, GLOB_ONLYDIR);
foreach ($package_list as $package_path) {
// load its default config file, if any
$package_file = $package_path . DIRECTORY_SEPARATOR
. 'config' . DIRECTORY_SEPARATOR
. 'default.php';
if (is_readable($package_file)) {
$this->load($package_file, $this->system, $this->loader, $this->di);
}
// load its config-mode-specific file, if any
if ($this->mode == 'default') {
continue;
}
$package_file = $package_path . DIRECTORY_SEPARATOR
. 'config' . DIRECTORY_SEPARATOR
. "{$config_mode}.php";
if (is_readable($package_file)) {
$this->load($package_file);
}
}
}
public function loadMode()
{
$file = $this->system->getConfigPath("{$this->mode}.php");
if (is_readable($file)) {
$this->load($file);
}
}
public function load($file)
{
$system = $this->system->getRootPath();
$loader = $this->loader;
$di = $this->di;
require $file;
$this->files[] = $file;
}
}