1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Igni\Application; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Application's config container. |
7
|
|
|
* Treats dots as an operator for accessing nested values. |
8
|
|
|
* If constant name is put in curly braces as a value, it wil be replaced |
9
|
|
|
* to the constant value. |
10
|
|
|
* |
11
|
|
|
* @example: |
12
|
|
|
* // Example usage. |
13
|
|
|
* $config = new Config(); |
14
|
|
|
* $config->set('some.key', true); |
15
|
|
|
* $some = $config->get('some'); // returns ['key' => true] |
16
|
|
|
* |
17
|
|
|
* @package Igni\Application |
18
|
|
|
*/ |
19
|
|
|
class Config |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $config; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Config constructor. |
28
|
|
|
* |
29
|
|
|
* @param array $config |
30
|
|
|
*/ |
31
|
19 |
|
public function __construct(array $config = []) |
32
|
|
|
{ |
33
|
19 |
|
$this->config = $config; |
34
|
19 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Checks if config key exists. |
38
|
|
|
* |
39
|
|
|
* @param string $key |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
|
|
public function has(string $key): bool |
43
|
|
|
{ |
44
|
|
|
return $this->lookup($key) !== null; |
45
|
|
|
} |
46
|
|
|
|
47
|
2 |
|
private function lookup(string $key) |
48
|
|
|
{ |
49
|
2 |
|
$result = $this->config; |
50
|
2 |
|
$key = explode('.', $key); |
51
|
2 |
|
foreach($key as $part) { |
52
|
2 |
|
if (!is_array($result) || !isset($result[$part])) { |
53
|
|
|
return null; |
54
|
|
|
} |
55
|
2 |
|
$result = $result[$part]; |
56
|
|
|
} |
57
|
|
|
|
58
|
2 |
|
return $result; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Gets value behind the key, or returns $default value if path does not exists. |
63
|
|
|
* |
64
|
|
|
* @param string $key |
65
|
|
|
* @param null $default |
|
|
|
|
66
|
|
|
* @return null|string|string[] |
67
|
|
|
*/ |
68
|
2 |
|
public function get(string $key, $default = null) |
69
|
|
|
{ |
70
|
2 |
|
$result = $this->lookup($key); |
71
|
2 |
|
return $result === null ? $default : $this->fetchConstants($result); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Merges one instance of Config class into current one and |
76
|
|
|
* returns current instance. |
77
|
|
|
* |
78
|
|
|
* @param Config $config |
79
|
|
|
* @return Config |
80
|
|
|
*/ |
81
|
1 |
|
public function merge(Config $config): Config |
82
|
|
|
{ |
83
|
1 |
|
$this->config = array_merge_recursive($this->config, $config->config); |
84
|
|
|
|
85
|
1 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Sets new value. |
90
|
|
|
* |
91
|
|
|
* @param string $key |
92
|
|
|
* @param $value |
93
|
|
|
*/ |
94
|
2 |
|
public function set(string $key, $value): void |
95
|
|
|
{ |
96
|
2 |
|
$key = explode('.', $key); |
97
|
2 |
|
$last = array_pop($key); |
98
|
2 |
|
$result = &$this->config; |
99
|
|
|
|
100
|
2 |
|
foreach ($key as $part) { |
101
|
2 |
|
if (!isset($result[$part]) || !is_array($result[$part])) { |
102
|
2 |
|
$result[$part] = []; |
103
|
|
|
} |
104
|
2 |
|
$result = &$result[$part]; |
105
|
|
|
} |
106
|
2 |
|
$result[$last] = $value; |
107
|
2 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Returns array representation of the config. |
111
|
|
|
* |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
2 |
|
public function toArray(): array |
115
|
|
|
{ |
116
|
2 |
|
return $this->config; |
117
|
|
|
} |
118
|
|
|
|
119
|
2 |
|
private function fetchConstants($value) |
120
|
|
|
{ |
121
|
2 |
|
if (!is_string($value)) { |
122
|
1 |
|
return $value; |
123
|
|
|
} |
124
|
1 |
|
return preg_replace_callback( |
125
|
1 |
|
'#\$\{([^{}]*)\}#', |
126
|
|
|
function($matches) { |
127
|
1 |
|
if (defined($matches[1])) { |
128
|
1 |
|
return constant($matches[1]); |
129
|
|
|
} |
130
|
|
|
return $matches[0]; |
131
|
1 |
|
}, |
132
|
1 |
|
$value |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|