Passed
Push — main ( 5ca287...d84479 )
by Thierry
05:24
created

ConfigManager::loggingEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * ConfigManager.php - Jaxon config reader
5
 *
6
 * Extends the config reader in the jaxon-config 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\App\Config;
16
17
use Jaxon\App\I18n\Translator;
18
use Jaxon\Config\Config;
19
use Jaxon\Config\ConfigReader;
20
use Jaxon\Config\ConfigSetter;
21
use Jaxon\Config\Exception\DataDepth;
22
use Jaxon\Config\Exception\FileAccess;
23
use Jaxon\Config\Exception\FileContent;
24
use Jaxon\Config\Exception\FileExtension;
25
use Jaxon\Config\Exception\YamlExtension;
26
use Jaxon\Exception\SetupException;
27
28
use function dirname;
29
30
class ConfigManager
31
{
32
    /**
33
     * @var Config
34
     */
35
    protected $xLibConfig;
36
37
    /**
38
     * @var Config
39
     */
40
    protected $xAppConfig;
41
42
    /**
43
     * The constructor
44
     *
45
     * @param array $aDefaultOptions
46
     * @param Translator $xTranslator
47
     * @param ConfigReader $xConfigReader
48
     * @param ConfigSetter $xConfigSetter
49
     * @param ConfigEventManager $xEventManager
50
     */
51
    public function __construct(array $aDefaultOptions, private Translator $xTranslator,
52
        private ConfigReader $xConfigReader, private ConfigSetter $xConfigSetter,
53
        private ConfigEventManager $xEventManager)
54
    {
55
        $this->xLibConfig = $xConfigSetter->newConfig($aDefaultOptions);
56
        $this->xAppConfig = $xConfigSetter->newConfig();
57
    }
58
59
    /**
60
     * Read a config file
61
     *
62
     * @param string $sConfigFile
63
     *
64
     * @return array
65
     * @throws SetupException
66
     */
67
    public function read(string $sConfigFile): array
68
    {
69
        try
70
        {
71
            return $this->xConfigReader->read($sConfigFile);
72
        }
73
        catch(YamlExtension $e)
74
        {
75
            $sMessage = $this->xTranslator->trans('errors.yaml.install');
76
            throw new SetupException($sMessage);
77
        }
78
        catch(FileExtension $e)
79
        {
80
            $sMessage = $this->xTranslator->trans('errors.file.extension', ['path' => $sConfigFile]);
81
            throw new SetupException($sMessage);
82
        }
83
        catch(FileAccess $e)
84
        {
85
            $sMessage = $this->xTranslator->trans('errors.file.access', ['path' => $sConfigFile]);
86
            throw new SetupException($sMessage);
87
        }
88
        catch(FileContent $e)
89
        {
90
            $sMessage = $this->xTranslator->trans('errors.file.content', ['path' => $sConfigFile]);
91
            throw new SetupException($sMessage);
92
        }
93
    }
94
95
    /**
96
     * Read options from a config file and set the library config
97
     *
98
     * @param string $sConfigFile The full path to the config file
99
     * @param string $sConfigSection The section of the config file to be loaded
100
     *
101
     * @return void
102
     * @throws SetupException
103
     */
104
    public function load(string $sConfigFile, string $sConfigSection = ''): void
105
    {
106
        try
107
        {
108
            // Read the options and save in the config.
109
            $this->xLibConfig = $this->xConfigSetter
110
                ->setOptions($this->xLibConfig, $this->read($sConfigFile), $sConfigSection);
111
            // Call the config change listeners.
112
            $this->xEventManager->libConfigChanged($this->xLibConfig, '');
113
        }
114
        catch(DataDepth $e)
115
        {
116
            $sMessage = $this->xTranslator->trans('errors.data.depth', [
117
                'key' => $e->sPrefix,
118
                'depth' => $e->nDepth,
119
            ]);
120
            throw new SetupException($sMessage);
121
        }
122
    }
123
124
    /**
125
     * Set the config options of the library
126
     *
127
     * @param array $aOptions
128
     * @param string $sNamePrefix A prefix for the config option names
129
     *
130
     * @return bool
131
     * @throws SetupException
132
     */
133
    public function setOptions(array $aOptions, string $sNamePrefix = ''): bool
134
    {
135
        try
136
        {
137
            $this->xLibConfig = $this->xConfigSetter
138
                ->setOptions($this->xLibConfig, $aOptions, $sNamePrefix);
139
            // Call the config change listeners.
140
            $this->xEventManager->libConfigChanged($this->xLibConfig, '');
141
            return $this->xLibConfig->changed();
142
        }
143
        catch(DataDepth $e)
144
        {
145
            $sMessage = $this->xTranslator->trans('errors.data.depth', [
146
                'key' => $e->sPrefix,
147
                'depth' => $e->nDepth,
148
            ]);
149
            throw new SetupException($sMessage);
150
        }
151
    }
152
153
    /**
154
     * Set the value of a config option
155
     *
156
     * @param string $sName The option name
157
     * @param mixed $xValue The option value
158
     *
159
     * @return void
160
     */
161
    public function setOption(string $sName, $xValue): void
162
    {
163
        $this->xLibConfig = $this->xConfigSetter
164
            ->setOption($this->xLibConfig, $sName, $xValue);
165
        // Call the config change listeners.
166
        $this->xEventManager->libConfigChanged($this->xLibConfig, $sName);
167
    }
168
169
    /**
170
     * Get the value of a config option
171
     *
172
     * @param string $sName The option name
173
     * @param mixed $xDefault The default value, to be returned if the option is not defined
174
     *
175
     * @return mixed
176
     */
177
    public function getOption(string $sName, $xDefault = null): mixed
178
    {
179
        return $this->xLibConfig->getOption($sName, $xDefault);
180
    }
181
182
    /**
183
     * Check the presence of a config option
184
     *
185
     * @param string $sName The option name
186
     *
187
     * @return bool
188
     */
189
    public function hasOption(string $sName): bool
190
    {
191
        return $this->xLibConfig->hasOption($sName);
192
    }
193
194
    /**
195
     * Get the names of the options matching a given prefix
196
     *
197
     * @param string $sPrefix The prefix to match
198
     *
199
     * @return array
200
     */
201
    public function getOptionNames(string $sPrefix): array
202
    {
203
        return $this->xLibConfig->getOptionNames($sPrefix);
204
    }
205
206
    /**
207
     * Set the value of a config option
208
     *
209
     * @param string $sName The option name
210
     * @param mixed $xValue The option value
211
     *
212
     * @return void
213
     */
214
    public function setAppOption(string $sName, $xValue): void
215
    {
216
        $this->xAppConfig = $this->xConfigSetter
217
            ->setOption($this->xAppConfig, $sName, $xValue);
218
        // Call the config change listeners.
219
        $this->xEventManager->appConfigChanged($this->xAppConfig, $sName);
220
    }
221
222
    /**
223
     * Get the application config
224
     *
225
     * @return Config
226
     */
227
    public function getAppConfig(): Config
228
    {
229
        return $this->xAppConfig;
230
    }
231
232
    /**
233
     * Set the application config options
234
     *
235
     * @param array $aAppOptions
236
     * @param string $sNamePrefix A prefix for the config option names
237
     *
238
     * @return bool
239
     */
240
    public function setAppOptions(array $aAppOptions, string $sNamePrefix = ''): bool
241
    {
242
        try
243
        {
244
            $this->xAppConfig = $this->xConfigSetter
245
                ->setOptions($this->xAppConfig, $aAppOptions, $sNamePrefix);
246
            // Call the config change listeners.
247
            $this->xEventManager->appConfigChanged($this->xAppConfig, '');
248
            return $this->xAppConfig->changed();
249
        }
250
        catch(DataDepth $e)
251
        {
252
            $sMessage = $this->xTranslator->trans('errors.data.depth', [
253
                'key' => $e->sPrefix,
254
                'depth' => $e->nDepth,
255
            ]);
256
            throw new SetupException($sMessage);
257
        }
258
    }
259
260
    /**
261
     * Get the value of an application config option
262
     *
263
     * @param string $sName The option name
264
     * @param mixed $xDefault The default value, to be returned if the option is not defined
265
     *
266
     * @return mixed
267
     */
268
    public function getAppOption(string $sName, $xDefault = null): mixed
269
    {
270
        return $this->xAppConfig->getOption($sName, $xDefault);
271
    }
272
273
    /**
274
     * Check the presence of an application config option
275
     *
276
     * @param string $sName The option name
277
     *
278
     * @return bool
279
     */
280
    public function hasAppOption(string $sName): bool
281
    {
282
        return $this->xAppConfig->hasOption($sName);
283
    }
284
285
    /**
286
     * Create a new the config object
287
     *
288
     * @param array $aOptions     The options array
289
     * @param string $sNamePrefix A prefix for the config option names
290
     *
291
     * @return Config
292
     * @throws SetupException
293
     */
294
    public function newConfig(array $aOptions = [], string $sNamePrefix = ''): Config
295
    {
296
        try
297
        {
298
            return $this->xConfigSetter->newConfig($aOptions, $sNamePrefix);
299
        }
300
        catch(DataDepth $e)
301
        {
302
            $sMessage = $this->xTranslator->trans('errors.data.depth', [
303
                'key' => $e->sPrefix,
304
                'depth' => $e->nDepth,
305
            ]);
306
            throw new SetupException($sMessage);
307
        }
308
    }
309
310
    /**
311
     * Check if the remote logging is enabled
312
     *
313
     * @return bool
314
     */
315
    public function loggingEnabled(): bool
316
    {
317
        return $this->getAppOption('options.logging.enabled', false);
318
    }
319
320
    /**
321
     * @param string $sClassName
322
     *
323
     * @return void
324
     */
325
    public function addLibEventListener(string $sClassName): void
326
    {
327
        $this->xEventManager->addLibConfigListener($sClassName);
328
    }
329
330
    /**
331
     * @param string $sClassName
332
     *
333
     * @return void
334
     */
335
    public function addAppEventListener(string $sClassName): void
336
    {
337
        $this->xEventManager->addAppConfigListener($sClassName);
338
    }
339
340
    /**
341
     * Make the helpers functions available in the global namespace.
342
     *
343
     * @param bool $bForce
344
     *
345
     * @return void
346
     */
347
    public function globals(bool $bForce = false): void
348
    {
349
        if($bForce || $this->getAppOption('helpers.global', true))
350
        {
351
            require_once dirname(__DIR__, 2) . '/globals.php';
352
        }
353
    }
354
}
355