1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Config |
4
|
|
|
* |
5
|
|
|
* @author Alexey Krupskiy <[email protected]> |
6
|
|
|
* @link http://inji.ru/ |
7
|
|
|
* @copyright 2015-2018 Alexey Krupskiy |
8
|
|
|
* @license https://github.com/injitools/cms-Inji/blob/master/LICENSE |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Inji; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Config |
15
|
|
|
* @package Inji |
16
|
|
|
*/ |
17
|
|
|
class Config { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Static config storage |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private static $_configs = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Load system config |
28
|
|
|
* |
29
|
|
|
* @param bool $forceLoad |
30
|
|
|
* @param bool|string $systemPath |
31
|
|
|
* @return array |
32
|
|
|
*/ |
33
|
|
|
public static function system($forceLoad = false, $systemPath = false) { |
34
|
|
|
if (!$forceLoad && isset(self::$_configs['system'])) { |
35
|
|
|
return self::$_configs['system']; |
36
|
|
|
} |
37
|
|
|
if ($systemPath === false) { |
38
|
|
|
$systemPath = INJI_SYSTEM_DIR; |
39
|
|
|
} |
40
|
|
|
if (!file_exists($systemPath . '/config/config.php')) { |
41
|
|
|
return []; |
42
|
|
|
} |
43
|
|
|
return self::$_configs['system'] = include $systemPath . '/config/config.php'; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Load custom config |
48
|
|
|
* |
49
|
|
|
* @param string $path |
50
|
|
|
* @param bool $forceLoad |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
public static function custom($path, $forceLoad = false) { |
54
|
|
View Code Duplication |
if (!$forceLoad && isset(self::$_configs['custom'][$path])) { |
|
|
|
|
55
|
|
|
return self::$_configs['custom'][$path]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (!file_exists($path)) { |
59
|
|
|
return []; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return self::$_configs['custom'][$path] = include $path; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Load app config |
67
|
|
|
* |
68
|
|
|
* @param App $app |
69
|
|
|
* @param bool $forceLoad |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
public static function app($app = null, $forceLoad = false) { |
73
|
|
|
if (!$app) { |
74
|
|
|
$app = App::$primary; |
75
|
|
|
} |
76
|
|
View Code Duplication |
if (!$forceLoad && isset(self::$_configs['app'][$app->name])) { |
|
|
|
|
77
|
|
|
return self::$_configs['app'][$app->name]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$path = $app->path . "/config/config.php"; |
81
|
|
|
if (!file_exists($path)) { |
82
|
|
|
return []; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return self::$_configs['app'][$app->name] = include $path; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Load share config |
90
|
|
|
* |
91
|
|
|
* @param string $module |
92
|
|
|
* @param bool $forceLoad |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
public static function share($module = '', $forceLoad = false) { |
96
|
|
|
if ($module) { |
97
|
|
|
if (!$forceLoad && isset(self::$_configs['shareModules'][$module])) { |
98
|
|
|
return self::$_configs['shareModules'][$module]; |
99
|
|
|
} |
100
|
|
|
$path = INJI_PROGRAM_DIR . "/config/modules/{$module}.php"; |
101
|
|
|
} else { |
102
|
|
|
if (!$forceLoad && isset(self::$_configs['share'])) { |
103
|
|
|
return self::$_configs['share']; |
104
|
|
|
} |
105
|
|
|
$path = INJI_PROGRAM_DIR . "/config/config.php"; |
106
|
|
|
} |
107
|
|
|
if (!file_exists($path)) { |
108
|
|
|
return []; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ($module) { |
112
|
|
|
return self::$_configs['shareModules'][$module] = include $path; |
113
|
|
|
} else { |
114
|
|
|
return self::$_configs['share'] = include $path; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Load module config |
120
|
|
|
* |
121
|
|
|
* @param string $module_name |
122
|
|
|
* @param App $app |
123
|
|
|
* @param bool $forceLoad |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
|
|
public static function module($module_name, $app = null, $forceLoad = false) { |
127
|
|
|
|
128
|
|
|
if (!$app) { |
129
|
|
|
$app = App::$primary; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if (!$forceLoad && isset(self::$_configs['module'][$app->name][$module_name])) { |
133
|
|
|
return self::$_configs['module'][$app->name][$module_name]; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$path = $app->path . "/config/modules/{$module_name}.php"; |
137
|
|
|
if (!file_exists($path)) { |
138
|
|
|
$path = INJI_SYSTEM_DIR . "/modules/{$module_name}/defaultConfig.php"; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if (!file_exists($path)) { |
142
|
|
|
return []; |
143
|
|
|
} |
144
|
|
|
return self::$_configs['module'][$app->name][$module_name] = include $path; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Save config |
149
|
|
|
* |
150
|
|
|
* @param string $type |
151
|
|
|
* @param array $data |
152
|
|
|
* @param string $module |
153
|
|
|
* @param App $app |
154
|
|
|
*/ |
155
|
|
|
public static function save($type, $data, $module = '', $app = null) { |
156
|
|
|
if (!$app) { |
157
|
|
|
$app = App::$primary; |
158
|
|
|
} |
159
|
|
|
switch ($type) { |
160
|
|
|
case 'system': |
161
|
|
|
$path = INJI_SYSTEM_DIR . '/config/config.php'; |
162
|
|
|
self::$_configs['system'] = $data; |
163
|
|
|
\Inji::$inst->event('Config-change-system', $data); |
164
|
|
|
break; |
165
|
|
|
case 'app': |
166
|
|
|
$path = $app->path . "/config/config.php"; |
167
|
|
|
self::$_configs['app'][$app->name] = $data; |
168
|
|
|
\Inji::$inst->event('Config-change-app-' . $app->name, $data); |
169
|
|
|
break; |
170
|
|
|
case 'module': |
171
|
|
|
$path = $app->path . "/config/modules/{$module}.php"; |
172
|
|
|
self::$_configs['module'][$app->name][$module] = $data; |
173
|
|
|
\Inji::$inst->event('Config-change-module-' . $app->name . '-' . $module, $data); |
174
|
|
|
break; |
175
|
|
|
case 'share': |
176
|
|
|
if ($module) { |
177
|
|
|
$path = INJI_PROGRAM_DIR . "/config/modules/{$module}.php"; |
178
|
|
|
self::$_configs['shareModules'][$module] = $data; |
179
|
|
|
\Inji::$inst->event('Config-change-shareModules-' . $module, $data); |
180
|
|
|
} else { |
181
|
|
|
$path = INJI_PROGRAM_DIR . "/config/config.php"; |
182
|
|
|
self::$_configs['share'] = $data; |
183
|
|
|
\Inji::$inst->event('Config-change-share', $data); |
184
|
|
|
} |
185
|
|
|
break; |
186
|
|
|
default: |
187
|
|
|
$path = $type; |
188
|
|
|
self::$_configs['custom'][$path] = $data; |
189
|
|
|
break; |
190
|
|
|
} |
191
|
|
|
$text = "<?php\nreturn " . CodeGenerator::genArray($data); |
192
|
|
|
Tools::createDir(substr($path, 0, strripos($path, '/'))); |
193
|
|
|
file_put_contents($path, $text); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
} |
197
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.