|
1
|
|
|
<?php |
|
2
|
|
|
namespace Consolidation\Config; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Overlay different configuration objects that implement ConfigInterface |
|
6
|
|
|
* to make a priority-based, merged configuration object. |
|
7
|
|
|
* |
|
8
|
|
|
* Note that using a ConfigOverlay hides the defaults stored in each |
|
9
|
|
|
* individual configuration context. When using overlays, always call |
|
10
|
|
|
* getDefault / setDefault on the ConfigOverlay object itself. |
|
11
|
|
|
*/ |
|
12
|
|
|
class ConfigOverlay implements ConfigInterface |
|
13
|
|
|
{ |
|
14
|
|
|
protected $contexts = []; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct() |
|
17
|
|
|
{ |
|
18
|
|
|
$this->contexts['default'] = new Config(); |
|
19
|
|
|
$this->contexts['process'] = new Config(); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Add a named configuration object to the configuration overlay. |
|
24
|
|
|
* Configuration objects added LAST have HIGHEST priority, with the |
|
25
|
|
|
* exception of the fact that the process context always has the |
|
26
|
|
|
* highest priority. |
|
27
|
|
|
*/ |
|
28
|
|
|
public function addContext($name, ConfigInterface $config) |
|
29
|
|
|
{ |
|
30
|
|
|
$process = $this->contexts['process']; |
|
31
|
|
|
unset($this->contexts['process']); |
|
32
|
|
|
unset($this->contexts[$name]); |
|
33
|
|
|
$this->contexts[$name] = $config; |
|
34
|
|
|
$this->contexts['process'] = $process; |
|
35
|
|
|
|
|
36
|
|
|
return $this; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function hasContext($name) |
|
40
|
|
|
{ |
|
41
|
|
|
return isset($this->contexts[$name]); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function getContext($name) |
|
45
|
|
|
{ |
|
46
|
|
|
if ($this->hasContext($name)) { |
|
47
|
|
|
return $this->contexts[$name]; |
|
48
|
|
|
} |
|
49
|
|
|
return new Config(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Determine if a non-default config value exists. |
|
54
|
|
|
*/ |
|
55
|
|
|
public function findContext($key) |
|
56
|
|
|
{ |
|
57
|
|
|
foreach (array_reverse($this->contexts) as $name => $config) { |
|
58
|
|
|
if ($config->has($key)) { |
|
59
|
|
|
return $config; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
return false; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @inheritdoc |
|
67
|
|
|
*/ |
|
68
|
|
|
public function has($key) |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->findContext($key) != false; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @inheritdoc |
|
75
|
|
|
*/ |
|
76
|
|
|
public function get($key, $default = null) |
|
77
|
|
|
{ |
|
78
|
|
|
$context = $this->findContext($key); |
|
79
|
|
|
if ($context) { |
|
80
|
|
|
return $context->get($key, $default); |
|
81
|
|
|
} |
|
82
|
|
|
return $default; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @inheritdoc |
|
87
|
|
|
*/ |
|
88
|
|
|
public function set($key, $value) |
|
89
|
|
|
{ |
|
90
|
|
|
$this->contexts['process']->set($key, $value); |
|
91
|
|
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @inheritdoc |
|
96
|
|
|
*/ |
|
97
|
|
|
public function import($data) |
|
98
|
|
|
{ |
|
99
|
|
|
throw new \Exception('The method "import" is not supported for the ConfigOverlay class.'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @inheritdoc |
|
104
|
|
|
*/ |
|
105
|
|
|
public function export() |
|
106
|
|
|
{ |
|
107
|
|
|
$export = []; |
|
108
|
|
|
foreach ($this->contexts as $name => $config) { |
|
109
|
|
|
$export = array_merge_recursive($export, $config->export()); |
|
110
|
|
|
} |
|
111
|
|
|
return $export; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @inheritdoc |
|
116
|
|
|
*/ |
|
117
|
|
|
public function hasDefault($key) |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->contexts['default']->has($key); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @inheritdoc |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getDefault($key, $default = null) |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->contexts['default']->get($key, $default); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @inheritdoc |
|
132
|
|
|
*/ |
|
133
|
|
|
public function setDefault($key, $value) |
|
134
|
|
|
{ |
|
135
|
|
|
$this->contexts['default']->set($key, $value); |
|
136
|
|
|
return $this; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|