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 |
||
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
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 | */ |
||
0 ignored issues
–
show
|
|||
14 | |||
15 | namespace Jaxon\App\Config; |
||
16 | |||
17 | use Jaxon\App\I18n\Translator; |
||
18 | use Jaxon\Exception\SetupException; |
||
19 | use Jaxon\Utils\Config\Config; |
||
20 | use Jaxon\Utils\Config\ConfigReader; |
||
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 |
||
0 ignored issues
–
show
|
|||
28 | { |
||
29 | /** |
||
30 | * @var Config |
||
31 | */ |
||
32 | protected $xConfig; |
||
0 ignored issues
–
show
|
|||
33 | |||
34 | /** |
||
35 | * @var ConfigReader |
||
36 | */ |
||
37 | protected $xConfigReader; |
||
38 | |||
39 | /** |
||
40 | * @var ConfigEventManager |
||
41 | */ |
||
42 | protected $xEventManager; |
||
43 | |||
44 | /** |
||
45 | * @var Translator |
||
46 | */ |
||
47 | protected $xTranslator; |
||
48 | |||
49 | /** |
||
50 | * The constructor |
||
51 | * |
||
52 | * @param ConfigReader $xConfigReader |
||
0 ignored issues
–
show
|
|||
53 | * @param ConfigEventManager $xEventManager |
||
0 ignored issues
–
show
|
|||
54 | * @param Translator $xTranslator |
||
0 ignored issues
–
show
|
|||
55 | */ |
||
56 | public function __construct(ConfigReader $xConfigReader, ConfigEventManager $xEventManager, Translator $xTranslator) |
||
0 ignored issues
–
show
|
|||
57 | { |
||
58 | $this->xConfigReader = $xConfigReader; |
||
59 | $this->xEventManager = $xEventManager; |
||
60 | $this->xTranslator = $xTranslator; |
||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
61 | $this->xConfig = new Config(); |
||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues.
Loading history...
|
|||
62 | } |
||
0 ignored issues
–
show
|
|||
63 | |||
64 | /** |
||
65 | * Read a config file |
||
66 | * |
||
67 | * @param string $sConfigFile |
||
0 ignored issues
–
show
|
|||
68 | * |
||
69 | * @return array |
||
70 | * @throws SetupException |
||
71 | */ |
||
72 | public function read(string $sConfigFile): array |
||
73 | { |
||
74 | try |
||
75 | { |
||
76 | return $this->xConfigReader->read($sConfigFile); |
||
77 | } |
||
78 | catch(YamlExtension $e) |
||
79 | { |
||
80 | $sMessage = $this->xTranslator->trans('errors.yaml.install'); |
||
81 | throw new SetupException($sMessage); |
||
82 | } |
||
83 | catch(FileExtension $e) |
||
84 | { |
||
85 | $sMessage = $this->xTranslator->trans('errors.file.extension', ['path' => $sConfigFile]); |
||
86 | throw new SetupException($sMessage); |
||
87 | } |
||
88 | catch(FileAccess $e) |
||
89 | { |
||
90 | $sMessage = $this->xTranslator->trans('errors.file.access', ['path' => $sConfigFile]); |
||
91 | throw new SetupException($sMessage); |
||
92 | } |
||
93 | catch(FileContent $e) |
||
94 | { |
||
95 | $sMessage = $this->xTranslator->trans('errors.file.content', ['path' => $sConfigFile]); |
||
96 | throw new SetupException($sMessage); |
||
97 | } |
||
98 | } |
||
0 ignored issues
–
show
|
|||
99 | |||
100 | /** |
||
101 | * Read options from a config file and set the library config |
||
102 | * |
||
103 | * @param string $sConfigFile The full path to the config file |
||
0 ignored issues
–
show
|
|||
104 | * @param string $sConfigSection The section of the config file to be loaded |
||
105 | * |
||
106 | * @return void |
||
107 | * @throws SetupException |
||
108 | */ |
||
109 | public function load(string $sConfigFile, string $sConfigSection = '') |
||
110 | { |
||
111 | try |
||
112 | { |
||
113 | // Read the options and save in the config. |
||
114 | $this->xConfig->setOptions($this->read($sConfigFile), $sConfigSection); |
||
115 | // Call the config change listeners. |
||
116 | $this->xEventManager->onChange($this->xConfig, ''); |
||
117 | } |
||
118 | catch(DataDepth $e) |
||
119 | { |
||
120 | $sMessage = $this->xTranslator->trans('errors.data.depth', |
||
121 | ['key' => $e->sPrefix, 'depth' => $e->nDepth]); |
||
122 | throw new SetupException($sMessage); |
||
123 | } |
||
124 | } |
||
0 ignored issues
–
show
|
|||
125 | |||
126 | /** |
||
127 | * Set the config options of the library |
||
128 | * |
||
129 | * @param array $aOptions |
||
0 ignored issues
–
show
|
|||
130 | * @param string $sKeys |
||
0 ignored issues
–
show
|
|||
131 | * |
||
132 | * @return bool |
||
133 | * @throws SetupException |
||
134 | */ |
||
135 | public function setOptions(array $aOptions, string $sKeys = ''): bool |
||
136 | { |
||
137 | try |
||
138 | { |
||
139 | if(!$this->xConfig->setOptions($aOptions, $sKeys)) |
||
140 | { |
||
141 | return false; |
||
142 | } |
||
143 | // Call the config change listeners. |
||
144 | $this->xEventManager->onChange($this->xConfig, ''); |
||
145 | return true; |
||
146 | } |
||
147 | catch(DataDepth $e) |
||
148 | { |
||
149 | $sMessage = $this->xTranslator->trans('errors.data.depth', |
||
150 | ['key' => $e->sPrefix, 'depth' => $e->nDepth]); |
||
151 | throw new SetupException($sMessage); |
||
152 | } |
||
153 | } |
||
0 ignored issues
–
show
|
|||
154 | |||
155 | /** |
||
156 | * Set the value of a config option |
||
157 | * |
||
158 | * @param string $sName The option name |
||
0 ignored issues
–
show
|
|||
159 | * @param mixed $xValue The option value |
||
0 ignored issues
–
show
|
|||
160 | * |
||
161 | * @return void |
||
162 | */ |
||
163 | public function setOption(string $sName, $xValue) |
||
164 | { |
||
165 | $this->xConfig->setOption($sName, $xValue); |
||
166 | // Call the config change listeners. |
||
167 | $this->xEventManager->onChange($this->xConfig, $sName); |
||
168 | } |
||
0 ignored issues
–
show
|
|||
169 | |||
170 | /** |
||
171 | * Get the value of a config option |
||
172 | * |
||
173 | * @param string $sName The option name |
||
0 ignored issues
–
show
|
|||
174 | * @param mixed $xDefault The default value, to be returned if the option is not defined |
||
0 ignored issues
–
show
|
|||
175 | * |
||
176 | * @return mixed |
||
177 | */ |
||
178 | public function getOption(string $sName, $xDefault = null) |
||
179 | { |
||
180 | return $this->xConfig->getOption($sName, $xDefault); |
||
181 | } |
||
0 ignored issues
–
show
|
|||
182 | |||
183 | /** |
||
184 | * Check the presence of a config option |
||
185 | * |
||
186 | * @param string $sName The option name |
||
187 | * |
||
188 | * @return bool |
||
189 | */ |
||
190 | public function hasOption(string $sName): bool |
||
191 | { |
||
192 | return $this->xConfig->hasOption($sName); |
||
193 | } |
||
0 ignored issues
–
show
|
|||
194 | |||
195 | /** |
||
196 | * Get the names of the options matching a given prefix |
||
197 | * |
||
198 | * @param string $sPrefix The prefix to match |
||
199 | * |
||
200 | * @return array |
||
201 | */ |
||
202 | public function getOptionNames(string $sPrefix): array |
||
203 | { |
||
204 | return $this->xConfig->getOptionNames($sPrefix); |
||
205 | } |
||
0 ignored issues
–
show
|
|||
206 | |||
207 | /** |
||
208 | * Create a new the config object |
||
209 | * |
||
210 | * @param array $aOptions The options array |
||
0 ignored issues
–
show
|
|||
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 | } |
||
0 ignored issues
–
show
|
|||
229 | } |
||
230 |