1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Micro |
6
|
|
|
* |
7
|
|
|
* @author Raffael Sahli <[email protected]> |
8
|
|
|
* @copyright Copyright (c) 2017 gyselroth GmbH (https://gyselroth.com) |
9
|
|
|
* @license MIT https://opensource.org/licenses/MIT |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Micro; |
13
|
|
|
|
14
|
|
|
use \Micro\Config\Exception; |
|
|
|
|
15
|
|
|
use \Micro\Config\ConfigInterface; |
16
|
|
|
use \Iterator; |
17
|
|
|
use \ArrayAccess; |
18
|
|
|
use \Countable; |
19
|
|
|
use \InvalidArgumentException; |
20
|
|
|
|
21
|
|
|
class Config implements ArrayAccess, Iterator, Countable |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Store |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $_store = []; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Position |
33
|
|
|
* |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
protected $position = 0; |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Load config |
41
|
|
|
* |
42
|
|
|
* @param string $config |
43
|
|
|
* @return void |
|
|
|
|
44
|
|
|
*/ |
45
|
|
|
public function __construct(?ConfigInterface $config=null) |
46
|
|
|
{ |
47
|
|
|
if($config !== null) { |
48
|
|
|
$this->config = $config->map(); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Inject config adapter |
55
|
|
|
* |
56
|
|
|
* @param ConfigInterface $config |
57
|
|
|
* @return Config |
58
|
|
|
*/ |
59
|
|
|
public function inject(ConfigInterface $config): Config |
60
|
|
|
{ |
61
|
|
|
return $this->merge($config->map()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Merge |
67
|
|
|
* |
68
|
|
|
* @return Config |
69
|
|
|
*/ |
70
|
|
|
public function merge(Config $from): Config |
71
|
|
|
{ |
72
|
|
|
foreach($from as $key => $value) { |
73
|
|
|
if(isset($this->_store[$key]) && $this->_store[$key] instanceof Config) { |
74
|
|
|
$this->_store[$key]->merge($value); |
75
|
|
|
} else { |
76
|
|
|
$this->_store[$key] = $value; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Count |
86
|
|
|
* |
87
|
|
|
* @return int |
88
|
|
|
*/ |
89
|
|
|
public function count() |
90
|
|
|
{ |
91
|
|
|
return count($this->_store); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Count |
96
|
|
|
* |
97
|
|
|
* @return int |
98
|
|
|
*/ |
99
|
|
|
public function children() |
100
|
|
|
{ |
101
|
|
|
return $this->_store; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get entry |
107
|
|
|
* |
108
|
|
|
* @param string $key |
109
|
|
|
* @return mixed |
110
|
|
|
*/ |
111
|
|
|
public function __get($key) |
112
|
|
|
{ |
113
|
|
|
if (isset($this->_store[$key])) { |
114
|
|
|
return $this->_store[$key]; |
115
|
|
|
} else { |
116
|
|
|
throw new Exception('requested config key '.$key.' is not available'); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Set offset |
123
|
|
|
* |
124
|
|
|
* @param int $offset |
125
|
|
|
* @param mixed $value |
126
|
|
|
* @return void |
127
|
|
|
*/ |
128
|
|
|
public function offsetSet($offset, $value) |
129
|
|
|
{ |
130
|
|
|
if ($offset === null) { |
131
|
|
|
$this->_store[] = $value; |
132
|
|
|
} else { |
133
|
|
|
$this->_store[$offset] = $value; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Count |
140
|
|
|
* |
141
|
|
|
* @param int $offset |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
|
|
public function offsetExists($offset) |
145
|
|
|
{ |
146
|
|
|
return isset($this->_store[$offset]); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Unset offset |
152
|
|
|
* |
153
|
|
|
* @param int $offset |
154
|
|
|
* @return void |
155
|
|
|
*/ |
156
|
|
|
public function offsetUnset($offset) |
157
|
|
|
{ |
158
|
|
|
unset($this->_store[$offset]); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Get value from offset |
164
|
|
|
* |
165
|
|
|
* @param int $offset |
166
|
|
|
* @return mixed |
167
|
|
|
*/ |
168
|
|
|
public function offsetGet($offset) |
169
|
|
|
{ |
170
|
|
|
return isset($this->_store[$offset]) ? $this->_store[$offset] : null; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Rewind |
175
|
|
|
* |
176
|
|
|
* @return void |
177
|
|
|
*/ |
178
|
|
|
public function rewind() |
179
|
|
|
{ |
180
|
|
|
reset($this->_store); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Get current |
186
|
|
|
* |
187
|
|
|
* @return mixed |
188
|
|
|
*/ |
189
|
|
|
public function current() |
190
|
|
|
{ |
191
|
|
|
return current($this->_store); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Get key |
197
|
|
|
* |
198
|
|
|
* @return int |
199
|
|
|
*/ |
200
|
|
|
public function key() |
201
|
|
|
{ |
202
|
|
|
return key($this->_store); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Next |
208
|
|
|
* |
209
|
|
|
* @return void |
210
|
|
|
*/ |
211
|
|
|
public function next() |
212
|
|
|
{ |
213
|
|
|
next($this->_store); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Valid |
219
|
|
|
* |
220
|
|
|
* @return bool |
221
|
|
|
*/ |
222
|
|
|
public function valid() |
223
|
|
|
{ |
224
|
|
|
return key($this->_store) !== null; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: