ConfigManager   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 242
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 54
c 1
b 0
f 0
dl 0
loc 242
rs 10
wmc 20

12 Methods

Rating   Name   Duplication   Size   Complexity  
A hasAppOption() 0 3 1
A newConfig() 0 11 2
A getOptionNames() 0 3 1
A __construct() 0 6 1
A load() 0 14 2
A setOptions() 0 17 3
A read() 0 25 5
A hasOption() 0 3 1
A getOption() 0 3 1
A setAppConfig() 0 3 1
A setOption() 0 5 1
A getAppOption() 0 3 1
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\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
28
{
29
    /**
30
     * @var Config
31
     */
32
    protected $xLibConfig;
33
34
    /**
35
     * @var Config
36
     */
37
    protected $xAppConfig = null;
38
39
    /**
40
     * @var ConfigReader
41
     */
42
    protected $xConfigReader;
43
44
    /**
45
     * @var ConfigEventManager
46
     */
47
    protected $xEventManager;
48
49
    /**
50
     * @var Translator
51
     */
52
    protected $xTranslator;
53
54
    /**
55
     * The constructor
56
     *
57
     * @param ConfigReader $xConfigReader
58
     * @param ConfigEventManager $xEventManager
59
     * @param Translator $xTranslator
60
     */
61
    public function __construct(ConfigReader $xConfigReader, ConfigEventManager $xEventManager, Translator $xTranslator)
62
    {
63
        $this->xConfigReader = $xConfigReader;
64
        $this->xEventManager = $xEventManager;
65
        $this->xTranslator = $xTranslator;
66
        $this->xLibConfig = new Config();
67
    }
68
69
    /**
70
     * Read a config file
71
     *
72
     * @param string $sConfigFile
73
     *
74
     * @return array
75
     * @throws SetupException
76
     */
77
    public function read(string $sConfigFile): array
78
    {
79
        try
80
        {
81
            return $this->xConfigReader->read($sConfigFile);
82
        }
83
        catch(YamlExtension $e)
84
        {
85
            $sMessage = $this->xTranslator->trans('errors.yaml.install');
86
            throw new SetupException($sMessage);
87
        }
88
        catch(FileExtension $e)
89
        {
90
            $sMessage = $this->xTranslator->trans('errors.file.extension', ['path' => $sConfigFile]);
91
            throw new SetupException($sMessage);
92
        }
93
        catch(FileAccess $e)
94
        {
95
            $sMessage = $this->xTranslator->trans('errors.file.access', ['path' => $sConfigFile]);
96
            throw new SetupException($sMessage);
97
        }
98
        catch(FileContent $e)
99
        {
100
            $sMessage = $this->xTranslator->trans('errors.file.content', ['path' => $sConfigFile]);
101
            throw new SetupException($sMessage);
102
        }
103
    }
104
105
    /**
106
     * Read options from a config file and set the library config
107
     *
108
     * @param string $sConfigFile The full path to the config file
109
     * @param string $sConfigSection The section of the config file to be loaded
110
     *
111
     * @return void
112
     * @throws SetupException
113
     */
114
    public function load(string $sConfigFile, string $sConfigSection = '')
115
    {
116
        try
117
        {
118
            // Read the options and save in the config.
119
            $this->xLibConfig->setOptions($this->read($sConfigFile), $sConfigSection);
120
            // Call the config change listeners.
121
            $this->xEventManager->onChange($this->xLibConfig, '');
122
        }
123
        catch(DataDepth $e)
124
        {
125
            $sMessage = $this->xTranslator->trans('errors.data.depth',
126
                ['key' => $e->sPrefix, 'depth' => $e->nDepth]);
127
            throw new SetupException($sMessage);
128
        }
129
    }
130
131
    /**
132
     * Set the config options of the library
133
     *
134
     * @param array $aOptions
135
     * @param string $sKeys
136
     *
137
     * @return bool
138
     * @throws SetupException
139
     */
140
    public function setOptions(array $aOptions, string $sKeys = ''): bool
141
    {
142
        try
143
        {
144
            if(!$this->xLibConfig->setOptions($aOptions, $sKeys))
145
            {
146
                return false;
147
            }
148
            // Call the config change listeners.
149
            $this->xEventManager->onChange($this->xLibConfig, '');
150
            return true;
151
        }
152
        catch(DataDepth $e)
153
        {
154
            $sMessage = $this->xTranslator->trans('errors.data.depth',
155
                ['key' => $e->sPrefix, 'depth' => $e->nDepth]);
156
            throw new SetupException($sMessage);
157
        }
158
    }
159
160
    /**
161
     * Set the value of a config option
162
     *
163
     * @param string $sName The option name
164
     * @param mixed $xValue The option value
165
     *
166
     * @return void
167
     */
168
    public function setOption(string $sName, $xValue)
169
    {
170
        $this->xLibConfig->setOption($sName, $xValue);
171
        // Call the config change listeners.
172
        $this->xEventManager->onChange($this->xLibConfig, $sName);
173
    }
174
175
    /**
176
     * Get the value of a config option
177
     *
178
     * @param string $sName The option name
179
     * @param mixed $xDefault The default value, to be returned if the option is not defined
180
     *
181
     * @return mixed
182
     */
183
    public function getOption(string $sName, $xDefault = null)
184
    {
185
        return $this->xLibConfig->getOption($sName, $xDefault);
186
    }
187
188
    /**
189
     * Check the presence of a config option
190
     *
191
     * @param string $sName The option name
192
     *
193
     * @return bool
194
     */
195
    public function hasOption(string $sName): bool
196
    {
197
        return $this->xLibConfig->hasOption($sName);
198
    }
199
200
    /**
201
     * Get the names of the options matching a given prefix
202
     *
203
     * @param string $sPrefix The prefix to match
204
     *
205
     * @return array
206
     */
207
    public function getOptionNames(string $sPrefix): array
208
    {
209
        return $this->xLibConfig->getOptionNames($sPrefix);
210
    }
211
212
    /**
213
     * Set the application config
214
     *
215
     * @param Config $xConfig
216
     *
217
     * @return void
218
     */
219
    public function setAppConfig(Config $xConfig)
220
    {
221
        $this->xAppConfig = $xConfig;
222
    }
223
224
    /**
225
     * Get the value of an application config option
226
     *
227
     * @param string $sName The option name
228
     * @param mixed $xDefault The default value, to be returned if the option is not defined
229
     *
230
     * @return mixed
231
     */
232
    public function getAppOption(string $sName, $xDefault = null)
233
    {
234
        return $this->xAppConfig->getOption($sName, $xDefault);
235
    }
236
237
    /**
238
     * Check the presence of an application config option
239
     *
240
     * @param string $sName The option name
241
     *
242
     * @return bool
243
     */
244
    public function hasAppOption(string $sName): bool
245
    {
246
        return $this->xAppConfig->hasOption($sName);
247
    }
248
249
    /**
250
     * Create a new the config object
251
     *
252
     * @param array $aOptions    The options array
253
     * @param string $sKeys    The prefix of key of the config options
254
     *
255
     * @return Config
256
     * @throws SetupException
257
     */
258
    public function newConfig(array $aOptions = [], string $sKeys = ''): Config
259
    {
260
        try
261
        {
262
            return new Config($aOptions, $sKeys);
263
        }
264
        catch(DataDepth $e)
265
        {
266
            $sMessage = $this->xTranslator->trans('errors.data.depth',
267
                ['key' => $e->sPrefix, 'depth' => $e->nDepth]);
268
            throw new SetupException($sMessage);
269
        }
270
    }
271
}
272