|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* ConfigManager.php - Jaxon config reader |
|
5
|
|
|
* |
|
6
|
|
|
* Extends the config reader in the jaxon-utils package, and provides exception handlers. |
|
7
|
|
|
* |
|
8
|
|
|
* @package jaxon-core |
|
|
|
|
|
|
9
|
|
|
* @author Thierry Feuzeu <[email protected]> |
|
10
|
|
|
* @copyright 2022 Thierry Feuzeu <[email protected]> |
|
11
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
|
12
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
|
13
|
|
|
*/ |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
namespace Jaxon\Config; |
|
16
|
|
|
|
|
17
|
|
|
use Jaxon\Jaxon; |
|
18
|
|
|
use Jaxon\Exception\SetupException; |
|
19
|
|
|
use Jaxon\Utils\Config\Config; |
|
20
|
|
|
use Jaxon\Utils\Config\ConfigReader; |
|
21
|
|
|
use Jaxon\Utils\Translation\Translator; |
|
22
|
|
|
use Jaxon\Utils\Config\Exception\DataDepth; |
|
23
|
|
|
use Jaxon\Utils\Config\Exception\FileAccess; |
|
24
|
|
|
use Jaxon\Utils\Config\Exception\FileContent; |
|
25
|
|
|
use Jaxon\Utils\Config\Exception\FileExtension; |
|
26
|
|
|
use Jaxon\Utils\Config\Exception\YamlExtension; |
|
27
|
|
|
|
|
28
|
|
|
class ConfigManager |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var Config |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $xConfig; |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var ConfigReader |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $xConfigReader; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var Translator |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $xTranslator; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var array The default config options |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $aConfig = [ |
|
|
|
|
|
|
49
|
|
|
'core' => [ |
|
50
|
|
|
'version' => Jaxon::VERSION, |
|
51
|
|
|
'language' => 'en', |
|
52
|
|
|
'encoding' => 'utf-8', |
|
53
|
|
|
'decode_utf8' => false, |
|
54
|
|
|
'prefix' => [ |
|
55
|
|
|
'function' => 'jaxon_', |
|
56
|
|
|
'class' => 'Jaxon', |
|
57
|
|
|
], |
|
58
|
|
|
'request' => [ |
|
59
|
|
|
// 'uri' => '', |
|
60
|
|
|
'mode' => 'asynchronous', |
|
61
|
|
|
'method' => 'POST', // W3C: Method is case sensitive |
|
62
|
|
|
], |
|
63
|
|
|
'response' => [ |
|
64
|
|
|
'send' => true, |
|
65
|
|
|
'merge.ap' => true, |
|
66
|
|
|
'merge.js' => true, |
|
67
|
|
|
], |
|
68
|
|
|
'debug' => [ |
|
69
|
|
|
'on' => false, |
|
70
|
|
|
'verbose' => false, |
|
71
|
|
|
], |
|
72
|
|
|
'process' => [ |
|
73
|
|
|
'exit' => true, |
|
74
|
|
|
'clean' => false, |
|
75
|
|
|
'timeout' => 6000, |
|
76
|
|
|
], |
|
77
|
|
|
'error' => [ |
|
78
|
|
|
'handle' => false, |
|
79
|
|
|
'log_file' => '', |
|
80
|
|
|
], |
|
81
|
|
|
'jquery' => [ |
|
82
|
|
|
'no_conflict' => false, |
|
83
|
|
|
], |
|
84
|
|
|
'upload' => [ |
|
85
|
|
|
'enabled' => true, |
|
86
|
|
|
], |
|
87
|
|
|
], |
|
88
|
|
|
'js' => [ |
|
89
|
|
|
'lib' => [ |
|
90
|
|
|
'output_id' => 0, |
|
91
|
|
|
'queue_size' => 0, |
|
92
|
|
|
'load_timeout' => 2000, |
|
93
|
|
|
'show_status' => false, |
|
94
|
|
|
'show_cursor' => true, |
|
95
|
|
|
], |
|
96
|
|
|
'app' => [ |
|
97
|
|
|
'dir' => '', |
|
98
|
|
|
'minify' => true, |
|
99
|
|
|
'options' => '', |
|
100
|
|
|
], |
|
101
|
|
|
], |
|
102
|
|
|
]; |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* The constructor |
|
106
|
|
|
* |
|
107
|
|
|
* @param ConfigReader $xConfigReader |
|
|
|
|
|
|
108
|
|
|
* @param Translator $xTranslator |
|
|
|
|
|
|
109
|
|
|
*/ |
|
110
|
|
|
public function __construct(ConfigReader $xConfigReader, Translator $xTranslator) |
|
|
|
|
|
|
111
|
|
|
{ |
|
112
|
|
|
$this->xConfigReader = $xConfigReader; |
|
113
|
|
|
$this->xTranslator = $xTranslator; |
|
|
|
|
|
|
114
|
|
|
try |
|
115
|
|
|
{ |
|
116
|
|
|
$this->xConfig = new Config($this->aConfig); |
|
117
|
|
|
} |
|
118
|
|
|
catch(DataDepth $e){} // This exception cannot actually be raised. |
|
119
|
|
|
} |
|
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Read a config file |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $sConfigFile |
|
|
|
|
|
|
125
|
|
|
* |
|
126
|
|
|
* @return array |
|
127
|
|
|
* @throws SetupException |
|
128
|
|
|
*/ |
|
129
|
|
|
public function read(string $sConfigFile): array |
|
130
|
|
|
{ |
|
131
|
|
|
try |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->xConfigReader->read($sConfigFile); |
|
134
|
|
|
} |
|
135
|
|
|
catch(YamlExtension $e) |
|
136
|
|
|
{ |
|
137
|
|
|
$sMessage = $this->xTranslator->trans('errors.yaml.install'); |
|
138
|
|
|
throw new SetupException($sMessage); |
|
139
|
|
|
} |
|
140
|
|
|
catch(FileExtension $e) |
|
141
|
|
|
{ |
|
142
|
|
|
$sMessage = $this->xTranslator->trans('errors.file.extension', ['path' => $sConfigFile]); |
|
143
|
|
|
throw new SetupException($sMessage); |
|
144
|
|
|
} |
|
145
|
|
|
catch(FileAccess $e) |
|
146
|
|
|
{ |
|
147
|
|
|
$sMessage = $this->xTranslator->trans('errors.file.access', ['path' => $sConfigFile]); |
|
148
|
|
|
throw new SetupException($sMessage); |
|
149
|
|
|
} |
|
150
|
|
|
catch(FileContent $e) |
|
151
|
|
|
{ |
|
152
|
|
|
$sMessage = $this->xTranslator->trans('errors.file.content', ['path' => $sConfigFile]); |
|
153
|
|
|
throw new SetupException($sMessage); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Read options from a config file and set the library config |
|
159
|
|
|
* |
|
160
|
|
|
* @param string $sConfigFile The full path to the config file |
|
|
|
|
|
|
161
|
|
|
* @param string $sConfigSection The section of the config file to be loaded |
|
162
|
|
|
* |
|
163
|
|
|
* @return void |
|
164
|
|
|
* @throws SetupException |
|
165
|
|
|
*/ |
|
166
|
|
|
public function load(string $sConfigFile, string $sConfigSection = '') |
|
167
|
|
|
{ |
|
168
|
|
|
try |
|
169
|
|
|
{ |
|
170
|
|
|
$this->xConfigReader->load($this->xConfig, $sConfigFile, $sConfigSection); |
|
171
|
|
|
// Set the library language any time the config is changed. |
|
172
|
|
|
$this->xTranslator->setLocale($this->xConfig->getOption('core.language')); |
|
|
|
|
|
|
173
|
|
|
} |
|
174
|
|
|
catch(YamlExtension $e) |
|
175
|
|
|
{ |
|
176
|
|
|
$sMessage = $this->xTranslator->trans('errors.yaml.install'); |
|
177
|
|
|
throw new SetupException($sMessage); |
|
178
|
|
|
} |
|
179
|
|
|
catch(FileExtension $e) |
|
180
|
|
|
{ |
|
181
|
|
|
$sMessage = $this->xTranslator->trans('errors.file.extension', ['path' => $sConfigFile]); |
|
182
|
|
|
throw new SetupException($sMessage); |
|
183
|
|
|
} |
|
184
|
|
|
catch(FileAccess $e) |
|
185
|
|
|
{ |
|
186
|
|
|
$sMessage = $this->xTranslator->trans('errors.file.access', ['path' => $sConfigFile]); |
|
187
|
|
|
throw new SetupException($sMessage); |
|
188
|
|
|
} |
|
189
|
|
|
catch(FileContent $e) |
|
190
|
|
|
{ |
|
191
|
|
|
$sMessage = $this->xTranslator->trans('errors.file.content', ['path' => $sConfigFile]); |
|
192
|
|
|
throw new SetupException($sMessage); |
|
193
|
|
|
} |
|
194
|
|
|
catch(DataDepth $e) |
|
195
|
|
|
{ |
|
196
|
|
|
$sMessage = $this->xTranslator->trans('errors.data.depth', |
|
197
|
|
|
['key' => $e->sPrefix, 'depth' => $e->nDepth]); |
|
198
|
|
|
throw new SetupException($sMessage); |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
|
|
|
|
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Set the config options of the library |
|
204
|
|
|
* |
|
205
|
|
|
* @param array $aOptions |
|
|
|
|
|
|
206
|
|
|
* |
|
207
|
|
|
* @return void |
|
208
|
|
|
* @throws SetupException |
|
209
|
|
|
*/ |
|
210
|
|
|
public function setOptions(array $aOptions): void |
|
211
|
|
|
{ |
|
212
|
|
|
try |
|
213
|
|
|
{ |
|
214
|
|
|
$this->xConfig->setOptions($aOptions); |
|
215
|
|
|
// Set the library language any time the config is changed. |
|
216
|
|
|
$this->xTranslator->setLocale($this->xConfig->getOption('core.language')); |
|
|
|
|
|
|
217
|
|
|
} |
|
218
|
|
|
catch(DataDepth $e) |
|
219
|
|
|
{ |
|
220
|
|
|
$sMessage = $this->xTranslator->trans('errors.data.depth', |
|
221
|
|
|
['key' => $e->sPrefix, 'depth' => $e->nDepth]); |
|
222
|
|
|
throw new SetupException($sMessage); |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
|
|
|
|
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Set the value of a config option |
|
228
|
|
|
* |
|
229
|
|
|
* @param string $sName The option name |
|
|
|
|
|
|
230
|
|
|
* @param mixed $xValue The option value |
|
|
|
|
|
|
231
|
|
|
* |
|
232
|
|
|
* @return void |
|
233
|
|
|
*/ |
|
234
|
|
|
public function setOption(string $sName, $xValue) |
|
235
|
|
|
{ |
|
236
|
|
|
$this->xConfig->setOption($sName, $xValue); |
|
237
|
|
|
// Set the library language any time the config is changed. |
|
238
|
|
|
$this->xTranslator->setLocale($this->xConfig->getOption('core.language')); |
|
|
|
|
|
|
239
|
|
|
} |
|
|
|
|
|
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Get the value of a config option |
|
243
|
|
|
* |
|
244
|
|
|
* @param string $sName The option name |
|
|
|
|
|
|
245
|
|
|
* @param mixed $xDefault The default value, to be returned if the option is not defined |
|
|
|
|
|
|
246
|
|
|
* |
|
247
|
|
|
* @return mixed |
|
248
|
|
|
*/ |
|
249
|
|
|
public function getOption(string $sName, $xDefault = null) |
|
250
|
|
|
{ |
|
251
|
|
|
return $this->xConfig->getOption($sName, $xDefault); |
|
252
|
|
|
} |
|
|
|
|
|
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* Check the presence of a config option |
|
256
|
|
|
* |
|
257
|
|
|
* @param string $sName The option name |
|
258
|
|
|
* |
|
259
|
|
|
* @return bool |
|
260
|
|
|
*/ |
|
261
|
|
|
public function hasOption(string $sName): bool |
|
262
|
|
|
{ |
|
263
|
|
|
return $this->xConfig->hasOption($sName); |
|
264
|
|
|
} |
|
|
|
|
|
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Get the names of the options matching a given prefix |
|
268
|
|
|
* |
|
269
|
|
|
* @param string $sPrefix The prefix to match |
|
270
|
|
|
* |
|
271
|
|
|
* @return array |
|
272
|
|
|
*/ |
|
273
|
|
|
public function getOptionNames(string $sPrefix): array |
|
274
|
|
|
{ |
|
275
|
|
|
return $this->xConfig->getOptionNames($sPrefix); |
|
276
|
|
|
} |
|
|
|
|
|
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* Create a new the config object |
|
280
|
|
|
* |
|
281
|
|
|
* @param array $aOptions The options array |
|
|
|
|
|
|
282
|
|
|
* @param string $sKeys The prefix of key of the config options |
|
283
|
|
|
* |
|
284
|
|
|
* @return Config |
|
285
|
|
|
* @throws SetupException |
|
286
|
|
|
*/ |
|
287
|
|
|
public function newConfig(array $aOptions = [], string $sKeys = ''): Config |
|
288
|
|
|
{ |
|
289
|
|
|
try |
|
290
|
|
|
{ |
|
291
|
|
|
return new Config($aOptions, $sKeys); |
|
292
|
|
|
} |
|
293
|
|
|
catch(DataDepth $e) |
|
294
|
|
|
{ |
|
295
|
|
|
$sMessage = $this->xTranslator->trans('errors.data.depth', |
|
296
|
|
|
['key' => $e->sPrefix, 'depth' => $e->nDepth]); |
|
297
|
|
|
throw new SetupException($sMessage); |
|
298
|
|
|
} |
|
299
|
|
|
} |
|
|
|
|
|
|
300
|
|
|
} |
|
301
|
|
|
|