|
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\Utils\Config\Config; |
|
18
|
|
|
use Jaxon\Utils\Config\Reader; |
|
19
|
|
|
use Jaxon\Utils\Translation\Translator; |
|
20
|
|
|
use Jaxon\Exception\SetupException; |
|
21
|
|
|
use Jaxon\Utils\Config\Exception\DataDepth; |
|
22
|
|
|
use Jaxon\Utils\Config\Exception\FileAccess; |
|
23
|
|
|
use Jaxon\Utils\Config\Exception\FileContent; |
|
24
|
|
|
use Jaxon\Utils\Config\Exception\FileExtension; |
|
25
|
|
|
use Jaxon\Utils\Config\Exception\YamlExtension; |
|
26
|
|
|
|
|
27
|
|
|
class ConfigManager |
|
|
|
|
|
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var Config |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $xConfig; |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var Reader |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $xReader; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var Translator |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $xTranslator; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The constructor |
|
46
|
|
|
* |
|
47
|
|
|
* @param Config $xConfig |
|
|
|
|
|
|
48
|
|
|
* @param Reader $xReader |
|
|
|
|
|
|
49
|
|
|
* @param Translator $xTranslator |
|
|
|
|
|
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct(Config $xConfig, Reader $xReader, Translator $xTranslator) |
|
|
|
|
|
|
52
|
|
|
{ |
|
53
|
|
|
$this->xConfig = $xConfig; |
|
|
|
|
|
|
54
|
|
|
$this->xReader = $xReader; |
|
|
|
|
|
|
55
|
|
|
$this->xTranslator = $xTranslator; |
|
56
|
|
|
} |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Read a config file |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $sConfigFile |
|
|
|
|
|
|
62
|
|
|
* |
|
63
|
|
|
* @return array |
|
64
|
|
|
* @throws SetupException |
|
65
|
|
|
*/ |
|
66
|
|
|
public function read(string $sConfigFile): array |
|
67
|
|
|
{ |
|
68
|
|
|
try |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->xReader->read($sConfigFile); |
|
71
|
|
|
} |
|
72
|
|
|
catch(YamlExtension $e) |
|
73
|
|
|
{ |
|
74
|
|
|
$sMessage = $this->xTranslator->trans('errors.yaml.install'); |
|
75
|
|
|
throw new SetupException($sMessage); |
|
76
|
|
|
} |
|
77
|
|
|
catch(FileExtension $e) |
|
78
|
|
|
{ |
|
79
|
|
|
$sMessage = $this->xTranslator->trans('errors.file.extension', ['path' => $sConfigFile]); |
|
80
|
|
|
throw new SetupException($sMessage); |
|
81
|
|
|
} |
|
82
|
|
|
catch(FileAccess $e) |
|
83
|
|
|
{ |
|
84
|
|
|
$sMessage = $this->xTranslator->trans('errors.file.access', ['path' => $sConfigFile]); |
|
85
|
|
|
throw new SetupException($sMessage); |
|
86
|
|
|
} |
|
87
|
|
|
catch(FileContent $e) |
|
88
|
|
|
{ |
|
89
|
|
|
$sMessage = $this->xTranslator->trans('errors.file.content', ['path' => $sConfigFile]); |
|
90
|
|
|
throw new SetupException($sMessage); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Read options from a config file and set the library config |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $sConfigFile The full path to the config file |
|
|
|
|
|
|
98
|
|
|
* @param string $sConfigSection The section of the config file to be loaded |
|
99
|
|
|
* |
|
100
|
|
|
* @return void |
|
101
|
|
|
* @throws SetupException |
|
102
|
|
|
*/ |
|
103
|
|
|
public function load(string $sConfigFile, string $sConfigSection = '') |
|
104
|
|
|
{ |
|
105
|
|
|
try |
|
106
|
|
|
{ |
|
107
|
|
|
$this->xReader->load($this->xConfig, $sConfigFile, $sConfigSection); |
|
108
|
|
|
} |
|
109
|
|
|
catch(YamlExtension $e) |
|
110
|
|
|
{ |
|
111
|
|
|
$sMessage = $this->xTranslator->trans('errors.yaml.install'); |
|
112
|
|
|
throw new SetupException($sMessage); |
|
113
|
|
|
} |
|
114
|
|
|
catch(FileExtension $e) |
|
115
|
|
|
{ |
|
116
|
|
|
$sMessage = $this->xTranslator->trans('errors.file.extension', ['path' => $sConfigFile]); |
|
117
|
|
|
throw new SetupException($sMessage); |
|
118
|
|
|
} |
|
119
|
|
|
catch(FileAccess $e) |
|
120
|
|
|
{ |
|
121
|
|
|
$sMessage = $this->xTranslator->trans('errors.file.access', ['path' => $sConfigFile]); |
|
122
|
|
|
throw new SetupException($sMessage); |
|
123
|
|
|
} |
|
124
|
|
|
catch(FileContent $e) |
|
125
|
|
|
{ |
|
126
|
|
|
$sMessage = $this->xTranslator->trans('errors.file.content', ['path' => $sConfigFile]); |
|
127
|
|
|
throw new SetupException($sMessage); |
|
128
|
|
|
} |
|
129
|
|
|
catch(DataDepth $e) |
|
130
|
|
|
{ |
|
131
|
|
|
$sMessage = $this->xTranslator->trans('errors.data.depth', |
|
132
|
|
|
['key' => $e->sPrefix, 'depth' => $e->nDepth]); |
|
133
|
|
|
throw new SetupException($sMessage); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Set the config options of the library |
|
139
|
|
|
* |
|
140
|
|
|
* @param array $aOptions |
|
|
|
|
|
|
141
|
|
|
* |
|
142
|
|
|
* @return void |
|
143
|
|
|
* @throws SetupException |
|
144
|
|
|
*/ |
|
145
|
|
|
public function setOptions(array $aOptions): void |
|
146
|
|
|
{ |
|
147
|
|
|
try |
|
148
|
|
|
{ |
|
149
|
|
|
$this->xConfig->setOptions($aOptions); |
|
150
|
|
|
} |
|
151
|
|
|
catch(DataDepth $e) |
|
152
|
|
|
{ |
|
153
|
|
|
$sMessage = $this->xTranslator->trans('errors.data.depth', |
|
154
|
|
|
['key' => $e->sPrefix, 'depth' => $e->nDepth]); |
|
155
|
|
|
throw new SetupException($sMessage); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
|
|
|
|
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Set the value of a config option |
|
161
|
|
|
* |
|
162
|
|
|
* @param string $sName The option name |
|
|
|
|
|
|
163
|
|
|
* @param mixed $xValue The option value |
|
|
|
|
|
|
164
|
|
|
* |
|
165
|
|
|
* @return void |
|
166
|
|
|
*/ |
|
167
|
|
|
public function setOption(string $sName, $xValue) |
|
168
|
|
|
{ |
|
169
|
|
|
$this->xConfig->setOption($sName, $xValue); |
|
170
|
|
|
} |
|
|
|
|
|
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Get the value of a config option |
|
174
|
|
|
* |
|
175
|
|
|
* @param string $sName The option name |
|
|
|
|
|
|
176
|
|
|
* @param mixed $xDefault The default value, to be returned if the option is not defined |
|
|
|
|
|
|
177
|
|
|
* |
|
178
|
|
|
* @return mixed |
|
179
|
|
|
*/ |
|
180
|
|
|
public function getOption(string $sName, $xDefault = null) |
|
181
|
|
|
{ |
|
182
|
|
|
return $this->xConfig->getOption($sName, $xDefault); |
|
183
|
|
|
} |
|
|
|
|
|
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Check the presence of a config option |
|
187
|
|
|
* |
|
188
|
|
|
* @param string $sName The option name |
|
189
|
|
|
* |
|
190
|
|
|
* @return bool |
|
191
|
|
|
*/ |
|
192
|
|
|
public function hasOption(string $sName): bool |
|
193
|
|
|
{ |
|
194
|
|
|
return $this->xConfig->hasOption($sName); |
|
195
|
|
|
} |
|
|
|
|
|
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Get the library config |
|
199
|
|
|
* |
|
200
|
|
|
* @return Config |
|
201
|
|
|
*/ |
|
202
|
|
|
public function getConfig(): Config |
|
203
|
|
|
{ |
|
204
|
|
|
return $this->xConfig; |
|
205
|
|
|
} |
|
|
|
|
|
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Create a new the config object |
|
209
|
|
|
* |
|
210
|
|
|
* @param array $aOptions The options array |
|
|
|
|
|
|
211
|
|
|
* @param string $sKeys The prefix of key of the config options |
|
212
|
|
|
* |
|
213
|
|
|
* @return Config |
|
214
|
|
|
* @throws SetupException |
|
215
|
|
|
*/ |
|
216
|
|
|
public function newConfig(array $aOptions = [], string $sKeys = ''): Config |
|
217
|
|
|
{ |
|
218
|
|
|
try |
|
219
|
|
|
{ |
|
220
|
|
|
return new Config($aOptions, $sKeys); |
|
221
|
|
|
} |
|
222
|
|
|
catch(DataDepth $e) |
|
223
|
|
|
{ |
|
224
|
|
|
$sMessage = $this->xTranslator->trans('errors.data.depth', |
|
225
|
|
|
['key' => $e->sPrefix, 'depth' => $e->nDepth]); |
|
226
|
|
|
throw new SetupException($sMessage); |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
|
|
|
|
|
229
|
|
|
} |
|
230
|
|
|
|