1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Config Manager |
5
|
|
|
* |
6
|
|
|
* @category core |
7
|
|
|
* @author Judicaël Paquet <[email protected]> |
8
|
|
|
* @copyright Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93) |
9
|
|
|
* @license https://github.com/las93/venus2/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël |
10
|
|
|
* @version Release: 1.0.0 |
11
|
|
|
* @filesource https://github.com/las93/venus2 |
12
|
|
|
* @link https://github.com/las93 |
13
|
|
|
* @since 1.0 |
14
|
|
|
*/ |
15
|
|
|
namespace Venus\core; |
16
|
|
|
|
17
|
|
|
use \Venus\lib\Debug as Debug; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Config Manager |
21
|
|
|
* |
22
|
|
|
* @category core |
23
|
|
|
* @author Judicaël Paquet <[email protected]> |
24
|
|
|
* @copyright Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93) |
25
|
|
|
* @license https://github.com/las93/venus3/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël |
26
|
|
|
* @version Release: 3.0.0 |
27
|
|
|
* @filesource https://github.com/las93/venus3 |
28
|
|
|
* @link https://github.com/las93 |
29
|
|
|
* @since 3.0 |
30
|
|
|
*/ |
31
|
|
|
class Config |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* conf in a cache array |
35
|
|
|
* |
36
|
|
|
* @access private |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private static $_aConfCache = array(); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* get a configuration |
43
|
|
|
* |
44
|
|
|
* @access public |
45
|
|
|
* @param string $sName name of the configuration |
46
|
|
|
* @param string $sPortal portal name if you specify it |
47
|
|
|
* @param bool $bNoDoRedirect not allowed the redirect parameter |
48
|
|
|
* @return void |
49
|
|
|
*/ |
50
|
|
|
public static function get(string $sName, string $sPortal = null, bool $bNoDoRedirect = false) |
51
|
|
|
{ |
52
|
|
|
if ($bNoDoRedirect === true) { $sNameCache = $sName.'_true'; } else { $sNameCache = $sName; } |
53
|
|
|
|
54
|
|
|
if ($sPortal === null || !is_string($sPortal)) { |
55
|
|
|
|
56
|
|
|
if (defined('PORTAL')) { |
57
|
|
|
|
58
|
|
|
$sPortal = PORTAL; |
59
|
|
|
$aDirectories = array($sPortal); |
60
|
|
|
} else { |
61
|
|
|
|
62
|
|
|
$sPortal = ''; |
63
|
|
|
$aDirectories = scandir(str_replace('core', 'src', __DIR__)); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (!isset(self::$_aConfCache[$sNameCache])) { |
68
|
|
|
|
69
|
|
|
$base = new \StdClass; |
70
|
|
|
|
71
|
|
|
foreach ($aDirectories as $sPortal) { |
|
|
|
|
72
|
|
|
|
73
|
|
|
if ($sPortal != '..' && $sPortal != '.') { |
74
|
|
|
|
75
|
|
View Code Duplication |
if (file_exists(str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-local')) { |
|
|
|
|
76
|
|
|
|
77
|
|
|
$sJsonFile = str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-local'; |
78
|
|
|
$base = self::_mergeAndGetConf($sJsonFile, $base); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (file_exists(str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-local')) { |
82
|
|
|
|
83
|
|
|
$sJsonFile = str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-local'; |
84
|
|
|
$base = self::_mergeAndGetConf($sJsonFile, $base); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
View Code Duplication |
if (file_exists(str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-dev') && getenv('DEV') == 1) { |
|
|
|
|
88
|
|
|
|
89
|
|
|
$sJsonFile = str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-dev'; |
90
|
|
|
$base = self::_mergeAndGetConf($sJsonFile, $base); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
View Code Duplication |
if (file_exists(str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-dev') && getenv('DEV') == 1) { |
|
|
|
|
94
|
|
|
|
95
|
|
|
$sJsonFile = str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-dev'; |
96
|
|
|
$base = self::_mergeAndGetConf($sJsonFile, $base); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
View Code Duplication |
if (file_exists(str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-dev') && getenv('PROD') == 1) { |
|
|
|
|
100
|
|
|
|
101
|
|
|
$sJsonFile = str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-prod'; |
102
|
|
|
$base = self::_mergeAndGetConf($sJsonFile, $base); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
View Code Duplication |
if (file_exists(str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-dev') && getenv('PROD') == 1) { |
|
|
|
|
106
|
|
|
|
107
|
|
|
$sJsonFile = str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-prod'; |
108
|
|
|
$base = self::_mergeAndGetConf($sJsonFile, $base); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
View Code Duplication |
if (file_exists(str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-dev') && getenv('PREPROD') == 1) { |
|
|
|
|
112
|
|
|
|
113
|
|
|
$sJsonFile = str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-pprod'; |
114
|
|
|
$base = self::_mergeAndGetConf($sJsonFile, $base); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
View Code Duplication |
if (file_exists(str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-dev') && getenv('PREPROD') == 1) { |
|
|
|
|
118
|
|
|
|
119
|
|
|
$sJsonFile = str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-pprod'; |
120
|
|
|
$base = self::_mergeAndGetConf($sJsonFile, $base); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
View Code Duplication |
if (file_exists(str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-dev') && getenv('RECETTE') == 1) { |
|
|
|
|
124
|
|
|
|
125
|
|
|
$sJsonFile = str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-rec'; |
126
|
|
|
$base = self::_mergeAndGetConf($sJsonFile, $base); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
View Code Duplication |
if (file_exists(str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-dev') && getenv('RECETTE') == 1) { |
|
|
|
|
130
|
|
|
|
131
|
|
|
$sJsonFile = str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-rec'; |
132
|
|
|
$base = self::_mergeAndGetConf($sJsonFile, $base); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if (file_exists(str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-local')) { |
136
|
|
|
|
137
|
|
|
$sJsonFile = str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-local'; |
138
|
|
|
$base = self::_mergeAndGetConf($sJsonFile, $base); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if (file_exists(str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf')) { |
142
|
|
|
|
143
|
|
|
$sJsonFile = str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf'; |
144
|
|
|
$base = self::_mergeAndGetConf($sJsonFile, $base); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$sJsonFile = str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf'; |
148
|
|
|
$base = self::_mergeAndGetConf($sJsonFile, $base); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if ($base === '') { |
153
|
|
|
|
154
|
|
|
trigger_error("Error in your Json format in this file : ".$sJsonFile, E_USER_NOTICE); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if (isset($base->redirect) && $bNoDoRedirect === false) { |
158
|
|
|
|
159
|
|
|
$base = self::get($sName, $base->redirect); |
|
|
|
|
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
self::$_aConfCache[$sNameCache] = $base; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if (!self::$_aConfCache[$sNameCache]) { |
166
|
|
|
|
167
|
|
|
$oDebug = Debug::getInstance(); |
168
|
|
|
$oDebug->error('The configuration file '.$sName.' is in error!'); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return self::$_aConfCache[$sNameCache]; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* get the bundle name location or the actualy bundle name if they isn't location |
176
|
|
|
* |
177
|
|
|
* @access public |
178
|
|
|
* @param string $sName name of the configuration |
179
|
|
|
* @return string |
180
|
|
|
*/ |
181
|
|
|
public static function getBundleLocationName(string $sName): string |
182
|
|
|
{ |
183
|
|
|
$oConfig = self::get($sName, null, true); |
|
|
|
|
184
|
|
|
|
185
|
|
|
if (isset($oConfig->redirect)) { return $oConfig->redirect; } else { return PORTAL; } |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* get file content and merge if not exists |
190
|
|
|
* |
191
|
|
|
* @access private |
192
|
|
|
* @param string $sFileToMerge file to get |
193
|
|
|
* @param \stdClass $base base |
194
|
|
|
* @return \stdClass |
195
|
|
|
*/ |
196
|
|
|
private static function _mergeAndGetConf(string $sFileToMerge, \stdClass $base) : \stdClass |
197
|
|
|
{ |
198
|
|
|
$oConfFiles = json_decode(file_get_contents($sFileToMerge)); |
199
|
|
|
|
200
|
|
|
if (is_object($oConfFiles)) { |
201
|
|
|
|
202
|
|
|
list($oConfFiles, $base) = self::_recursiveGet($oConfFiles, $base); |
|
|
|
|
203
|
|
|
return $base; |
204
|
|
|
} else { |
205
|
|
|
|
206
|
|
|
echo "The Json ".$sFileToMerge." has an error! Please verify!\n"; |
207
|
|
|
$oDebug = Debug::getInstance(); |
208
|
|
|
$oDebug->error("The Json ".$sFileToMerge." has an error! Please verify!\n"); |
209
|
|
|
new \Exception("The Json ".$sFileToMerge." has an error! Please verify!\n"); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* recursive merge |
215
|
|
|
* |
216
|
|
|
* @access private |
217
|
|
|
* @param $oConfFiles |
218
|
|
|
* @param \stdClass $base |
219
|
|
|
* @return multitype:array multitype:array |
|
|
|
|
220
|
|
|
*/ |
221
|
|
|
private static function _recursiveGet($oConfFiles, \stdClass $base) : array |
222
|
|
|
{ |
223
|
|
|
foreach ($oConfFiles as $sKey => $mOne) { |
224
|
|
|
|
225
|
|
|
if (is_object($oConfFiles) && is_object($base) && !isset($base->$sKey)) { |
226
|
|
|
|
227
|
|
|
$base->$sKey = $oConfFiles->$sKey; |
228
|
|
|
} else if (is_array($oConfFiles) && is_array($base) && !isset($base[$sKey])) { |
229
|
|
|
|
230
|
|
|
$base[$sKey] = $oConfFiles[$sKey]; |
231
|
|
|
} else if (!isset($base->$sKey) && is_array($mOne)) { |
232
|
|
|
|
233
|
|
|
$base->$sKey = new \StdClass; |
234
|
|
|
list($oConfFiles, $base) = self::_recursiveGet($mOne, $base->$sKey); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
return array($oConfFiles, $base); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: