1
|
|
|
<?php |
2
|
|
|
namespace Consolidation\Config\Util; |
3
|
|
|
|
4
|
|
|
use Consolidation\Config\Config; |
5
|
|
|
use Consolidation\Config\ConfigInterface; |
6
|
|
|
use Consolidation\Config\Util\ArrayUtil; |
7
|
|
|
use Consolidation\Config\Util\ConfigInterpolatorInterface; |
8
|
|
|
use Consolidation\Config\Util\ConfigInterpolatorTrait; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Overlay different configuration objects that implement ConfigInterface |
12
|
|
|
* to make a priority-based, merged configuration object. |
13
|
|
|
* |
14
|
|
|
* Note that using a ConfigOverlay hides the defaults stored in each |
15
|
|
|
* individual configuration context. When using overlays, always call |
16
|
|
|
* getDefault / setDefault on the ConfigOverlay object itself. |
17
|
|
|
*/ |
18
|
|
|
class ConfigOverlay implements ConfigInterface, ConfigInterpolatorInterface, ConfigRuntimeInterface |
19
|
|
|
{ |
20
|
|
|
use ConfigInterpolatorTrait; |
21
|
|
|
protected $contexts = []; |
22
|
|
|
|
23
|
|
|
const DEFAULT_CONTEXT = 'default'; |
24
|
|
|
const PROCESS_CONTEXT = 'process'; |
25
|
|
|
|
26
|
|
|
public function __construct() |
27
|
|
|
{ |
28
|
|
|
$this->contexts[self::DEFAULT_CONTEXT] = new Config(); |
29
|
|
|
$this->contexts[self::PROCESS_CONTEXT] = new Config(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Add a named configuration object to the configuration overlay. |
34
|
|
|
* Configuration objects added LAST have HIGHEST priority, with the |
35
|
|
|
* exception of the fact that the process context always has the |
36
|
|
|
* highest priority. |
37
|
|
|
* |
38
|
|
|
* If a context has already been added, its priority will not change. |
39
|
|
|
*/ |
40
|
|
|
public function addContext($name, ConfigInterface $config) |
41
|
|
|
{ |
42
|
|
|
$process = $this->contexts[self::PROCESS_CONTEXT]; |
43
|
|
|
unset($this->contexts[self::PROCESS_CONTEXT]); |
44
|
|
|
$this->contexts[$name] = $config; |
45
|
|
|
$this->contexts[self::PROCESS_CONTEXT] = $process; |
46
|
|
|
|
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Add a placeholder context that will be prioritized higher than |
52
|
|
|
* existing contexts. This is done to ensure that contexts added |
53
|
|
|
* later will maintain a higher priority if the placeholder context |
54
|
|
|
* is later relaced with a different configuration set via addContext(). |
55
|
|
|
* |
56
|
|
|
* @param string $name |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
|
|
public function addPlaceholder($name) |
60
|
|
|
{ |
61
|
|
|
return $this->addContext($name, new Config()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Increase the priority of the named context such that it is higher |
66
|
|
|
* in priority than any existing context except for the 'process' |
67
|
|
|
* context. |
68
|
|
|
* |
69
|
|
|
* @param string $name |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
|
|
public function increasePriority($name) |
73
|
|
|
{ |
74
|
|
|
$config = $this->getContext($name); |
75
|
|
|
unset($this->contexts[$name]); |
76
|
|
|
return $this->addContext($name, $config); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function hasContext($name) |
80
|
|
|
{ |
81
|
|
|
return isset($this->contexts[$name]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getContext($name) |
85
|
|
|
{ |
86
|
|
|
if ($this->hasContext($name)) { |
87
|
|
|
return $this->contexts[$name]; |
88
|
|
|
} |
89
|
|
|
return new Config(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function runtimeConfig() |
93
|
|
|
{ |
94
|
|
|
return $this->getContext(self::PROCESS_CONTEXT); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function removeContext($name) |
98
|
|
|
{ |
99
|
|
|
unset($this->contexts[$name]); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Determine if a non-default config value exists. |
104
|
|
|
*/ |
105
|
|
|
public function findContext($key) |
106
|
|
|
{ |
107
|
|
|
foreach (array_reverse($this->contexts) as $name => $config) { |
108
|
|
|
if ($config->has($key)) { |
109
|
|
|
return $config; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @inheritdoc |
117
|
|
|
*/ |
118
|
|
|
public function has($key) |
119
|
|
|
{ |
120
|
|
|
return $this->findContext($key) != false; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @inheritdoc |
125
|
|
|
*/ |
126
|
|
|
public function get($key, $default = null) |
127
|
|
|
{ |
128
|
|
|
if (is_array($default)) { |
129
|
|
|
return $this->getUnion($key); |
130
|
|
|
} |
131
|
|
|
return $this->getSingle($key, $default); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function getSingle($key, $default = null) |
135
|
|
|
{ |
136
|
|
|
$context = $this->findContext($key); |
137
|
|
|
if ($context) { |
138
|
|
|
return $context->get($key, $default); |
139
|
|
|
} |
140
|
|
|
return $default; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function getUnion($key) |
144
|
|
|
{ |
145
|
|
|
$result = []; |
146
|
|
|
foreach (array_reverse($this->contexts) as $name => $config) { |
147
|
|
|
$item = (array) $config->get($key, []); |
148
|
|
|
if ($item !== null) { |
149
|
|
|
$result = array_merge($result, $item); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
return $result; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @inheritdoc |
157
|
|
|
*/ |
158
|
|
|
public function set($key, $value) |
159
|
|
|
{ |
160
|
|
|
$this->contexts[self::PROCESS_CONTEXT]->set($key, $value); |
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @inheritdoc |
166
|
|
|
*/ |
167
|
|
|
public function import($data) |
168
|
|
|
{ |
169
|
|
|
$this->unsupported(__FUNCTION__); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @inheritdoc |
174
|
|
|
*/ |
175
|
|
|
public function replace($data) |
|
|
|
|
176
|
|
|
{ |
177
|
|
|
$this->unsupported(__FUNCTION__); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @inheritdoc |
182
|
|
|
*/ |
183
|
|
|
public function combine($data) |
|
|
|
|
184
|
|
|
{ |
185
|
|
|
$this->unsupported(__FUNCTION__); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @inheritdoc |
190
|
|
|
*/ |
191
|
|
|
protected function unsupported($fn) |
192
|
|
|
{ |
193
|
|
|
throw new \Exception("The method '$fn' is not supported for the ConfigOverlay class."); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @inheritdoc |
198
|
|
|
*/ |
199
|
|
|
public function export() |
200
|
|
|
{ |
201
|
|
|
$export = []; |
202
|
|
|
foreach ($this->contexts as $name => $config) { |
203
|
|
|
$exportToMerge = $config->export(); |
204
|
|
|
$export = \array_replace_recursive($export, $exportToMerge); |
205
|
|
|
} |
206
|
|
|
return $export; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @inheritdoc |
211
|
|
|
*/ |
212
|
|
|
public function hasDefault($key) |
213
|
|
|
{ |
214
|
|
|
return $this->contexts[self::DEFAULT_CONTEXT]->has($key); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @inheritdoc |
219
|
|
|
*/ |
220
|
|
|
public function getDefault($key, $default = null) |
221
|
|
|
{ |
222
|
|
|
return $this->contexts[self::DEFAULT_CONTEXT]->get($key, $default); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @inheritdoc |
227
|
|
|
*/ |
228
|
|
|
public function setDefault($key, $value) |
229
|
|
|
{ |
230
|
|
|
$this->contexts[self::DEFAULT_CONTEXT]->set($key, $value); |
231
|
|
|
return $this; |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.