1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Glenn\Config; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
|
7
|
|
|
use Glenn\Config\Contracts\ManagerContract; |
8
|
|
|
|
9
|
|
|
class Manager implements ArrayAccess, ManagerContract |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* All of the configuration items. |
13
|
|
|
* |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
protected $items = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Construct a new Config Manager pre-loaded with items. |
20
|
|
|
* |
21
|
|
|
* @param array $items |
22
|
|
|
* |
23
|
|
|
* @author Glenn McEwan <[email protected]> |
24
|
|
|
*/ |
25
|
9 |
|
public function __construct(array $items = []) |
26
|
|
|
{ |
27
|
9 |
|
$this->items = $items; |
28
|
9 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Check if a key exists in the config. |
32
|
|
|
* |
33
|
|
|
* @param string $key The key in the config to check for existence |
34
|
|
|
* |
35
|
|
|
* @return bool |
36
|
|
|
* |
37
|
|
|
* @author Glenn McEwan <[email protected]> |
38
|
|
|
*/ |
39
|
4 |
|
public function has($key) |
40
|
|
|
{ |
41
|
4 |
|
$items = $this->all(); |
42
|
|
|
|
43
|
4 |
|
if (empty($items) || is_null($key)) { |
44
|
|
|
return false; |
45
|
|
|
} |
46
|
|
|
|
47
|
4 |
|
if (array_key_exists($key, $items)) { |
48
|
3 |
|
return true; |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
foreach (explode('.', $key) as $segment) { |
52
|
1 |
|
if (!is_array($items) || !array_key_exists($segment, $items)) { |
53
|
1 |
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
$items = $items[$segment]; |
57
|
1 |
|
} |
58
|
|
|
|
59
|
1 |
|
return true; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Retrieve a given config value by its key. |
64
|
|
|
* |
65
|
|
|
* @param string $key Config key |
66
|
|
|
* @param mixed $default Default value if the key doesn't exist |
67
|
|
|
* |
68
|
|
|
* @return mixed The config value |
69
|
|
|
* |
70
|
|
|
* @author Glenn McEwan <[email protected]> |
71
|
|
|
*/ |
72
|
6 |
|
public function get($key, $default = null) |
73
|
|
|
{ |
74
|
6 |
|
$items = $this->all(); |
75
|
|
|
|
76
|
6 |
|
if (array_key_exists($key, $items)) { |
77
|
5 |
|
return $items[$key]; |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
foreach (explode('.', $key) as $segment) { |
81
|
1 |
|
if (!is_array($items) || !array_key_exists($segment, $items)) { |
82
|
1 |
|
return $default; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$items = $items[$segment]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $items; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Retrieve all of the config items. |
93
|
|
|
* |
94
|
|
|
* @return array |
95
|
|
|
* |
96
|
|
|
* @author Glenn McEwan <[email protected]> |
97
|
|
|
*/ |
98
|
9 |
|
public function all() |
99
|
|
|
{ |
100
|
9 |
|
return $this->items; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Set a config entry by key, optional value. |
105
|
|
|
* |
106
|
|
|
* @param string $key Config key |
107
|
|
|
* @param mixed $value Config value |
108
|
|
|
* |
109
|
|
|
* @return static return self / $this for chain-ability |
110
|
|
|
* |
111
|
|
|
* @author Glenn McEwan <[email protected]> |
112
|
|
|
*/ |
113
|
5 |
|
public function set($key, $value = null) |
114
|
|
|
{ |
115
|
5 |
|
$items = &$this->items; |
116
|
|
|
|
117
|
5 |
|
$keys = explode('.', $key); |
118
|
|
|
|
119
|
5 |
|
foreach ($keys as $key) { |
120
|
5 |
|
if (!isset($items[$key]) || !is_array($items[$key])) { |
121
|
5 |
|
$items[$key] = []; |
122
|
5 |
|
} |
123
|
|
|
|
124
|
5 |
|
$items = &$items[$key]; |
125
|
5 |
|
} |
126
|
|
|
|
127
|
5 |
|
$items = $value; |
128
|
|
|
|
129
|
5 |
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* If a parent key is passed, for example, as 'deployment', |
134
|
|
|
* then the data will be put in to the Config as deployment.*, |
135
|
|
|
* otherwise it will be placed at the root level in the config. |
136
|
|
|
* |
137
|
|
|
* Optionally, a parent key in which the config data will reside. |
138
|
|
|
* |
139
|
|
|
* @param mixed $value The array to set in to the config |
140
|
|
|
* @param string $key [optional] A parent config key to set the array in to |
141
|
|
|
* |
142
|
|
|
* @author Glenn McEwan <[email protected]> |
143
|
|
|
*/ |
144
|
2 |
|
public function setArray($value, $key = null) |
145
|
|
|
{ |
146
|
2 |
|
if ($key !== null) { |
147
|
1 |
|
$this->set($key, $value); |
148
|
1 |
|
} else { |
149
|
1 |
|
foreach ($value as $head => $node) { |
150
|
1 |
|
$this->set($head, $node); |
151
|
1 |
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
2 |
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/* |
158
|
|
|
|-------------------------------------------------------------------------- |
159
|
|
|
| Interface - ArrayAccess |
160
|
|
|
|-------------------------------------------------------------------------- |
161
|
|
|
| |
162
|
|
|
*/ |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Check if a key exists in the config. |
166
|
|
|
* |
167
|
|
|
* @param string $key The key in the config to check for existence |
168
|
|
|
* |
169
|
|
|
* @return bool |
170
|
|
|
* |
171
|
|
|
* @author Glenn McEwan <[email protected]> |
172
|
|
|
*/ |
173
|
|
|
public function offsetExists($key) |
174
|
|
|
{ |
175
|
|
|
return $this->has($key); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Retrieve a given config value by its key. |
180
|
|
|
* |
181
|
|
|
* @param string $key Config key |
182
|
|
|
* |
183
|
|
|
* @return mixed The config value |
184
|
|
|
* |
185
|
|
|
* @author Glenn McEwan <[email protected]> |
186
|
|
|
*/ |
187
|
|
|
public function offsetGet($key) |
188
|
|
|
{ |
189
|
|
|
return $this->get($key); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Set a config entry by key, optional value. |
194
|
|
|
* |
195
|
|
|
* @param string $key Config key |
196
|
|
|
* @param mixed $value Config value |
197
|
|
|
* |
198
|
|
|
* @author Glenn McEwan <[email protected]> |
199
|
|
|
*/ |
200
|
|
|
public function offsetSet($key, $value) |
201
|
|
|
{ |
202
|
|
|
$this->set($key, $value); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Unset a configuration option. |
207
|
|
|
* |
208
|
|
|
* @param string $key |
209
|
|
|
* |
210
|
|
|
* @author Glenn McEwan <[email protected]> |
211
|
|
|
*/ |
212
|
|
|
public function offsetUnset($key) |
213
|
|
|
{ |
214
|
|
|
$this->set($key, null); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|