|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Jellyfish\Config; |
|
6
|
|
|
|
|
7
|
|
|
use ArrayObject; |
|
8
|
|
|
use Exception; |
|
9
|
|
|
use Jellyfish\Config\Exception\ConfigKeyNotFoundException; |
|
10
|
|
|
use Jellyfish\Config\Exception\NotSupportedConfigValueTypeException; |
|
11
|
|
|
|
|
12
|
|
|
use function is_bool; |
|
13
|
|
|
use function is_float; |
|
14
|
|
|
use function is_int; |
|
15
|
|
|
use function is_string; |
|
16
|
|
|
|
|
17
|
|
|
class Config implements ConfigInterface |
|
18
|
|
|
{ |
|
19
|
|
|
protected const CONFIG_FILE_PREFIX = 'config-'; |
|
20
|
|
|
protected const CONFIG_FILE = 'default'; |
|
21
|
|
|
protected const CONFIG_FILE_SUFFIX = '.php'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var \ArrayObject |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $config; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $appDir; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $environment; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $appDir |
|
40
|
|
|
* @param string $environment |
|
41
|
|
|
* |
|
42
|
|
|
* @throws Exception |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct( |
|
45
|
|
|
string $appDir, |
|
46
|
|
|
string $environment |
|
47
|
|
|
) { |
|
48
|
|
|
$this->appDir = $appDir; |
|
49
|
|
|
$this->environment = $environment; |
|
50
|
|
|
|
|
51
|
|
|
$this->initialize(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $key |
|
56
|
|
|
* @param string|null $default |
|
57
|
|
|
* |
|
58
|
|
|
* @return string |
|
59
|
|
|
* |
|
60
|
|
|
* @throws \Jellyfish\Config\Exception\ConfigKeyNotFoundException |
|
61
|
|
|
* @throws \Jellyfish\Config\Exception\NotSupportedConfigValueTypeException |
|
62
|
|
|
*/ |
|
63
|
|
|
public function get(string $key, ?string $default = null): string |
|
64
|
|
|
{ |
|
65
|
|
|
if ($default !== null && !$this->hasValue($key)) { |
|
66
|
|
|
return $default; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if (!$this->hasValue($key)) { |
|
70
|
|
|
throw new ConfigKeyNotFoundException(sprintf('Could not find key "%s" in "%s"', $key, __CLASS__)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $this->getValue($key); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param string $key |
|
78
|
|
|
* |
|
79
|
|
|
* @return string |
|
80
|
|
|
* |
|
81
|
|
|
* @throws \Jellyfish\Config\Exception\NotSupportedConfigValueTypeException |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function getValue(string $key): string |
|
84
|
|
|
{ |
|
85
|
|
|
$value = $this->config[$key]; |
|
86
|
|
|
|
|
87
|
|
|
if (is_string($value)) { |
|
88
|
|
|
return $value; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if (is_int($value) || is_float($value) || is_bool($value)) { |
|
92
|
|
|
return (string) $value; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
throw new NotSupportedConfigValueTypeException(sprintf('Value type for key "%s" is not supported.', $key)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param string $key |
|
100
|
|
|
* |
|
101
|
|
|
* @return bool |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function hasValue(string $key): bool |
|
104
|
|
|
{ |
|
105
|
|
|
return isset($this->config[$key]); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return void |
|
110
|
|
|
* |
|
111
|
|
|
* @throws Exception |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function initialize(): void |
|
114
|
|
|
{ |
|
115
|
|
|
$config = new ArrayObject(); |
|
116
|
|
|
|
|
117
|
|
|
$this->buildConfig($config); |
|
118
|
|
|
$this->buildConfig($config, $this->environment); |
|
119
|
|
|
|
|
120
|
|
|
$this->config = $config; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param \ArrayObject $config |
|
125
|
|
|
* @param string|null $environment |
|
126
|
|
|
* |
|
127
|
|
|
* @return \ArrayObject |
|
128
|
|
|
*/ |
|
129
|
|
|
protected function buildConfig(ArrayObject $config, string $environment = null): ArrayObject |
|
130
|
|
|
{ |
|
131
|
|
|
$configFile = $environment ?? self::CONFIG_FILE; |
|
132
|
|
|
$fileName = self::CONFIG_FILE_PREFIX . $configFile . self::CONFIG_FILE_SUFFIX; |
|
133
|
|
|
$pathToConfigFile = $this->appDir . $fileName; |
|
134
|
|
|
|
|
135
|
|
|
if (\file_exists($pathToConfigFile)) { |
|
136
|
|
|
include $pathToConfigFile; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return $config; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|