|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* ConfigReader.php |
|
5
|
|
|
* |
|
6
|
|
|
* Read config values from files. |
|
7
|
|
|
* |
|
8
|
|
|
* @package jaxon-config |
|
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 function trim; |
|
18
|
|
|
use function pathinfo; |
|
19
|
|
|
|
|
20
|
|
|
class ConfigReader |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @param ConfigSetter $xConfigSetter |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(private ConfigSetter $xConfigSetter) |
|
26
|
|
|
{} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Read options from a config file to an array |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $sConfigFile The full path to the config file |
|
32
|
|
|
* |
|
33
|
|
|
* @return array |
|
34
|
|
|
* @throws Exception\FileAccess |
|
35
|
|
|
* @throws Exception\FileExtension |
|
36
|
|
|
* @throws Exception\FileContent |
|
37
|
|
|
* @throws Exception\YamlExtension |
|
38
|
|
|
*/ |
|
39
|
|
|
public function read(string $sConfigFile): array |
|
40
|
|
|
{ |
|
41
|
|
|
if(!($sConfigFile = trim($sConfigFile))) |
|
42
|
|
|
{ |
|
43
|
|
|
return []; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$sExt = pathinfo($sConfigFile, PATHINFO_EXTENSION); |
|
47
|
|
|
switch($sExt) |
|
48
|
|
|
{ |
|
49
|
|
|
case 'php': |
|
50
|
|
|
$aConfigOptions = Reader\PhpReader::read($sConfigFile); |
|
51
|
|
|
break; |
|
52
|
|
|
case 'yaml': |
|
53
|
|
|
case 'yml': |
|
54
|
|
|
$aConfigOptions = Reader\YamlReader::read($sConfigFile); |
|
55
|
|
|
break; |
|
56
|
|
|
case 'json': |
|
57
|
|
|
$aConfigOptions = Reader\JsonReader::read($sConfigFile); |
|
58
|
|
|
break; |
|
59
|
|
|
default: |
|
60
|
|
|
throw new Exception\FileExtension($sConfigFile); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $aConfigOptions; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Read options from a config file to a config object |
|
68
|
|
|
* |
|
69
|
|
|
* @param Config $xConfig |
|
70
|
|
|
* @param string $sConfigFile The full path to the config file |
|
71
|
|
|
* @param string $sConfigSection The section of the config file to be loaded |
|
72
|
|
|
* |
|
73
|
|
|
* @return Config |
|
74
|
|
|
* @throws Exception\DataDepth |
|
75
|
|
|
* @throws Exception\FileAccess |
|
76
|
|
|
* @throws Exception\FileExtension |
|
77
|
|
|
* @throws Exception\FileContent |
|
78
|
|
|
* @throws Exception\YamlExtension |
|
79
|
|
|
*/ |
|
80
|
|
|
public function load(Config $xConfig, string $sConfigFile, string $sConfigSection = ''): Config |
|
81
|
|
|
{ |
|
82
|
|
|
// Read the options and save in the config. |
|
83
|
|
|
return $this->xConfigSetter |
|
84
|
|
|
->setOptions($xConfig, $this->read($sConfigFile), '', $sConfigSection); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|