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
|
|
|
$aDirectories = []; |
53
|
|
|
$sJsonFile=''; |
54
|
|
|
|
55
|
|
|
if ($bNoDoRedirect === true) { $sNameCache = $sName.'_true'; } else { $sNameCache = $sName; } |
56
|
|
|
|
57
|
|
|
if ($sPortal === null || !is_string($sPortal)) { |
58
|
|
|
|
59
|
|
|
if (defined('PORTAL')) { |
60
|
|
|
|
61
|
|
|
$sPortal = PORTAL; |
62
|
|
|
$aDirectories = array($sPortal); |
63
|
|
|
} else { |
64
|
|
|
|
65
|
|
|
$sPortal = ''; |
66
|
|
|
$aDirectories = scandir(str_replace('core', 'src', __DIR__)); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (!isset(self::$_aConfCache[$sNameCache])) { |
71
|
|
|
|
72
|
|
|
$base = new \stdClass; |
73
|
|
|
|
74
|
|
|
if (count($aDirectories) < 1) { $aDirectories = [$sPortal]; } |
75
|
|
|
|
76
|
|
|
foreach ($aDirectories as $sPortal) { |
77
|
|
|
|
78
|
|
|
if ($sPortal != '..' && $sPortal != '.') { |
79
|
|
|
|
80
|
|
View Code Duplication |
if (file_exists(str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-local')) { |
|
|
|
|
81
|
|
|
|
82
|
|
|
$sJsonFile = str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-local'; |
83
|
|
|
$base = self::_mergeAndGetConf($sJsonFile, $base); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (file_exists(str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-local')) { |
87
|
|
|
|
88
|
|
|
$sJsonFile = str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-local'; |
89
|
|
|
$base = self::_mergeAndGetConf($sJsonFile, $base); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
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) { |
|
|
|
|
93
|
|
|
|
94
|
|
|
$sJsonFile = str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-dev'; |
95
|
|
|
$base = self::_mergeAndGetConf($sJsonFile, $base); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
View Code Duplication |
if (file_exists(str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-dev') && getenv('DEV') == 1) { |
|
|
|
|
99
|
|
|
|
100
|
|
|
$sJsonFile = str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-dev'; |
101
|
|
|
$base = self::_mergeAndGetConf($sJsonFile, $base); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
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) { |
|
|
|
|
105
|
|
|
|
106
|
|
|
$sJsonFile = str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-prod'; |
107
|
|
|
$base = self::_mergeAndGetConf($sJsonFile, $base); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
View Code Duplication |
if (file_exists(str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-dev') && getenv('PROD') == 1) { |
|
|
|
|
111
|
|
|
|
112
|
|
|
$sJsonFile = str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-prod'; |
113
|
|
|
$base = self::_mergeAndGetConf($sJsonFile, $base); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
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) { |
|
|
|
|
117
|
|
|
|
118
|
|
|
$sJsonFile = str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-pprod'; |
119
|
|
|
$base = self::_mergeAndGetConf($sJsonFile, $base); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
View Code Duplication |
if (file_exists(str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-dev') && getenv('PREPROD') == 1) { |
|
|
|
|
123
|
|
|
|
124
|
|
|
$sJsonFile = str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-pprod'; |
125
|
|
|
$base = self::_mergeAndGetConf($sJsonFile, $base); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
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) { |
|
|
|
|
129
|
|
|
|
130
|
|
|
$sJsonFile = str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-rec'; |
131
|
|
|
$base = self::_mergeAndGetConf($sJsonFile, $base); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
View Code Duplication |
if (file_exists(str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-dev') && getenv('RECETTE') == 1) { |
|
|
|
|
135
|
|
|
|
136
|
|
|
$sJsonFile = str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-rec'; |
137
|
|
|
$base = self::_mergeAndGetConf($sJsonFile, $base); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if (file_exists(str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-local')) { |
141
|
|
|
|
142
|
|
|
$sJsonFile = str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-local'; |
143
|
|
|
$base = self::_mergeAndGetConf($sJsonFile, $base); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if (file_exists(str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf')) { |
147
|
|
|
|
148
|
|
|
$sJsonFile = str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf'; |
149
|
|
|
$base = self::_mergeAndGetConf($sJsonFile, $base); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$sJsonFile = str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf'; |
153
|
|
|
$base = self::_mergeAndGetConf($sJsonFile, $base); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if ($base === '') { |
158
|
|
|
|
159
|
|
|
trigger_error("Error in your Json format in this file : ".$sJsonFile, E_USER_NOTICE); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if (isset($base->redirect) && $bNoDoRedirect === false) { |
163
|
|
|
|
164
|
|
|
$base = self::get($sName, $base->redirect); |
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
self::$_aConfCache[$sNameCache] = $base; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
if (!self::$_aConfCache[$sNameCache]) { |
171
|
|
|
|
172
|
|
|
$oDebug = Debug::getInstance(); |
173
|
|
|
$oDebug->error('The configuration file '.$sName.' is in error!'); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return self::$_aConfCache[$sNameCache]; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* get the bundle name location or the actualy bundle name if they isn't location |
181
|
|
|
* |
182
|
|
|
* @access public |
183
|
|
|
* @param string $sName name of the configuration |
184
|
|
|
* @return string |
185
|
|
|
*/ |
186
|
|
|
public static function getBundleLocationName(string $sName): string |
187
|
|
|
{ |
188
|
|
|
$oConfig = self::get($sName, null, true); |
|
|
|
|
189
|
|
|
|
190
|
|
|
if (isset($oConfig->redirect)) { return $oConfig->redirect; } else { return PORTAL; } |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* get file content and merge if not exists |
195
|
|
|
* |
196
|
|
|
* @access private |
197
|
|
|
* @param string $sFileToMerge file to get |
198
|
|
|
* @param \stdClass $base base |
199
|
|
|
* @return \stdClass |
200
|
|
|
*/ |
201
|
|
|
private static function _mergeAndGetConf(string $sFileToMerge, \stdClass $base) : \stdClass |
202
|
|
|
{ |
203
|
|
|
$oConfFiles = json_decode(file_get_contents($sFileToMerge)); |
204
|
|
|
|
205
|
|
|
if (is_object($oConfFiles)) { |
206
|
|
|
|
207
|
|
|
list($oConfFiles, $base) = self::_recursiveGet($oConfFiles, $base); |
|
|
|
|
208
|
|
|
return $base; |
209
|
|
|
} else { |
210
|
|
|
|
211
|
|
|
echo "The Json ".$sFileToMerge." has an error! Please verify!\n"; |
212
|
|
|
$oDebug = Debug::getInstance(); |
213
|
|
|
$oDebug->error("The Json ".$sFileToMerge." has an error! Please verify!\n"); |
214
|
|
|
new \Exception("The Json ".$sFileToMerge." has an error! Please verify!\n"); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* recursive merge |
220
|
|
|
* |
221
|
|
|
* @access private |
222
|
|
|
* @param $oConfFiles |
223
|
|
|
* @param \stdClass $base |
224
|
|
|
* @return multitype:array multitype:array |
|
|
|
|
225
|
|
|
*/ |
226
|
|
|
private static function _recursiveGet($oConfFiles, \stdClass $base) : array |
227
|
|
|
{ |
228
|
|
|
foreach ($oConfFiles as $sKey => $mOne) { |
229
|
|
|
|
230
|
|
|
if (is_object($oConfFiles) && is_object($base) && !isset($base->$sKey)) { |
231
|
|
|
|
232
|
|
|
$base->$sKey = $oConfFiles->$sKey; |
233
|
|
|
} else if (is_array($oConfFiles) && is_array($base) && !isset($base[$sKey])) { |
234
|
|
|
|
235
|
|
|
$base[$sKey] = $oConfFiles[$sKey]; |
236
|
|
|
} else if (!isset($base->$sKey) && is_array($mOne)) { |
237
|
|
|
|
238
|
|
|
$base->$sKey = new \StdClass; |
239
|
|
|
list($oConfFiles, $base) = self::_recursiveGet($mOne, $base->$sKey); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
return array($oConfFiles, $base); |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
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.