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
|
14 |
|
public function __construct(array $items = []) |
26
|
|
|
{ |
27
|
14 |
|
$this->items = $items; |
28
|
14 |
|
} |
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 |
View Code Duplication |
foreach (explode('.', $key) as $segment) { |
|
|
|
|
52
|
2 |
|
if (!is_array($items) || !array_key_exists($segment, $items)) { |
53
|
2 |
|
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
|
9 |
|
public function get($key, $default = null) |
73
|
|
|
{ |
74
|
9 |
|
$items = $this->all(); |
75
|
|
|
|
76
|
9 |
|
if (array_key_exists($key, $items)) { |
77
|
8 |
|
return $items[$key]; |
78
|
|
|
} |
79
|
|
|
|
80
|
3 |
View Code Duplication |
foreach (explode('.', $key) as $segment) { |
|
|
|
|
81
|
3 |
|
if (!is_array($items) || !array_key_exists($segment, $items)) { |
82
|
3 |
|
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
|
14 |
|
public function all() |
99
|
|
|
{ |
100
|
14 |
|
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
|
7 |
|
public function set($key, $value = null) |
114
|
|
|
{ |
115
|
7 |
|
$items = &$this->items; |
116
|
|
|
|
117
|
7 |
|
$keys = explode('.', $key); |
118
|
|
|
|
119
|
7 |
|
foreach ($keys as $key) { |
120
|
7 |
|
if (!isset($items[$key]) || !is_array($items[$key])) { |
121
|
7 |
|
$items[$key] = []; |
122
|
7 |
|
} |
123
|
|
|
|
124
|
7 |
|
$items = &$items[$key]; |
125
|
7 |
|
} |
126
|
|
|
|
127
|
7 |
|
$items = $value; |
128
|
|
|
|
129
|
7 |
|
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
|
1 |
|
public function offsetExists($key) |
174
|
|
|
{ |
175
|
1 |
|
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
|
3 |
|
public function offsetGet($key) |
188
|
|
|
{ |
189
|
3 |
|
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
|
1 |
|
public function offsetSet($key, $value) |
201
|
|
|
{ |
202
|
1 |
|
$this->set($key, $value); |
203
|
1 |
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Unset a configuration option. |
207
|
|
|
* |
208
|
|
|
* @param string $key |
209
|
|
|
* |
210
|
|
|
* @author Glenn McEwan <[email protected]> |
211
|
|
|
*/ |
212
|
1 |
|
public function offsetUnset($key) |
213
|
|
|
{ |
214
|
1 |
|
$this->set($key, null); |
215
|
1 |
|
} |
216
|
|
|
} |
217
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.