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