1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Abstract Config Object |
4
|
|
|
* |
5
|
|
|
* @package BrightNucleus\Config |
6
|
|
|
* @author Alain Schlesser <[email protected]> |
7
|
|
|
* @license GPL-2.0+ |
8
|
|
|
* @link http://www.brightnucleus.com/ |
9
|
|
|
* @copyright 2016 Alain Schlesser, Bright Nucleus |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace BrightNucleus\Config; |
13
|
|
|
|
14
|
|
|
use ArrayObject; |
15
|
|
|
use OutOfRangeException; |
16
|
|
|
use BadMethodCallException; |
17
|
|
|
use BrightNucleus\Config\ConfigInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Config loader used to load config PHP files as objects. |
21
|
|
|
* |
22
|
|
|
* @since 0.1.0 |
23
|
|
|
* |
24
|
|
|
* @package BrightNucleus\Config |
25
|
|
|
* @author Alain Schlesser <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
abstract class AbstractConfig extends ArrayObject implements ConfigInterface |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Array of strings that are used as delimiters to parse configuration keys. |
32
|
|
|
* |
33
|
|
|
* @since 0.1.6 |
34
|
|
|
* |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $delimiter = ['\\', '/', '.']; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Instantiate the AbstractConfig object. |
41
|
|
|
* |
42
|
|
|
* @since 0.1.0 |
43
|
|
|
* @since 0.1.6 Accepts a delimiter to parse configuration keys. |
44
|
|
|
* |
45
|
|
|
* @param array $config Array with settings. |
46
|
|
|
* @param string[]|string|null $delimiter A string or array of strings that are used as delimiters to parse |
47
|
|
|
* configuration keys. Defaults to "\", "/" & ".". |
48
|
|
|
*/ |
49
|
1 |
|
public function __construct(array $config, $delimiter = null) |
50
|
|
|
{ |
51
|
|
|
// Make sure the config entries can be accessed as properties. |
52
|
1 |
|
parent::__construct($config, ArrayObject::ARRAY_AS_PROPS); |
53
|
|
|
|
54
|
1 |
|
if (null !== $delimiter) { |
55
|
|
|
$this->delimiter = (array)$delimiter; |
56
|
|
|
} |
57
|
1 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Check whether the Config has a specific key. |
61
|
|
|
* |
62
|
|
|
* To check a value several levels deep, add the keys for each level as a comma-separated list. |
63
|
|
|
* |
64
|
|
|
* @since 0.1.0 |
65
|
|
|
* @since 0.1.4 Accepts list of keys. |
66
|
|
|
* |
67
|
|
|
* @param string ... List of keys. |
68
|
|
|
* @return bool |
69
|
|
|
* @throws BadMethodCallException If no argument was provided. |
70
|
|
|
*/ |
71
|
2 |
|
public function hasKey() |
72
|
|
|
{ |
73
|
2 |
|
$keys = array_reverse($this->getKeyArguments(func_get_args())); |
74
|
|
|
|
75
|
2 |
|
$array = $this->getArrayCopy(); |
76
|
2 |
|
while (count($keys) > 0) { |
77
|
2 |
|
$key = array_pop($keys); |
78
|
2 |
|
if ( ! array_key_exists($key, $array)) { |
79
|
2 |
|
return false; |
80
|
|
|
} |
81
|
2 |
|
$array = $array[$key]; |
82
|
|
|
} |
83
|
|
|
|
84
|
2 |
|
return true; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get the value of a specific key. |
89
|
|
|
* |
90
|
|
|
* To get a value several levels deep, add the keys for each level as a comma-separated list. |
91
|
|
|
* |
92
|
|
|
* @since 0.1.0 |
93
|
|
|
* @since 0.1.4 Accepts list of keys. |
94
|
|
|
* |
95
|
|
|
* @param string ... List of keys. |
96
|
|
|
* @return mixed |
97
|
|
|
* @throws BadMethodCallException If no argument was provided. |
98
|
|
|
* @throws OutOfRangeException If an unknown key is requested. |
99
|
|
|
*/ |
100
|
2 |
|
public function getKey() |
101
|
|
|
{ |
102
|
2 |
|
$keys = $this->getKeyArguments(func_get_args()); |
103
|
|
|
|
104
|
2 |
|
if ( ! $this->hasKey($keys)) { |
105
|
2 |
|
throw new OutOfRangeException(sprintf(_('The configuration key %1$s does not exist.'), |
106
|
2 |
|
implode('->', $keys))); |
107
|
|
|
} |
108
|
|
|
|
109
|
2 |
|
$keys = array_reverse($keys); |
110
|
2 |
|
$array = $this->getArrayCopy(); |
111
|
2 |
|
while (count($keys) > 0) { |
112
|
2 |
|
$key = array_pop($keys); |
113
|
2 |
|
$array = $array[$key]; |
114
|
|
|
} |
115
|
|
|
|
116
|
2 |
|
return $array; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get a (multi-dimensional) array of all the configuration settings. |
121
|
|
|
* |
122
|
|
|
* @since 0.1.4 |
123
|
|
|
* |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
1 |
|
public function getAll() |
127
|
|
|
{ |
128
|
1 |
|
return $this->getArrayCopy(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get the an array with all the keys |
133
|
|
|
* |
134
|
|
|
* @since 0.1.0 |
135
|
|
|
* @return array |
|
|
|
|
136
|
|
|
*/ |
137
|
1 |
|
public function getKeys() |
138
|
|
|
{ |
139
|
1 |
|
return array_keys((array)$this); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Extract the configuration key arguments from an arbitrary array. |
144
|
|
|
* |
145
|
|
|
* @since 0.1.6 |
146
|
|
|
* |
147
|
|
|
* @param array $arguments Array as fetched through get_func_args(). |
148
|
|
|
* @return array Array of strings. |
149
|
|
|
* @throws BadMethodCallException If no argument was provided. |
150
|
|
|
*/ |
151
|
1 |
|
protected function getKeyArguments($arguments) |
152
|
|
|
{ |
153
|
1 |
|
if (count($arguments) < 1) { |
154
|
|
|
throw new BadMethodCallException(_('No configuration key was provided.')); |
155
|
|
|
} |
156
|
|
|
|
157
|
1 |
|
$keys = []; |
158
|
1 |
|
foreach ($arguments as $argument) { |
159
|
1 |
|
if (is_array($argument)) { |
160
|
1 |
|
$keys = array_merge($keys, $this->getKeyArguments($argument)); |
161
|
|
|
} |
162
|
1 |
|
if (is_string($argument)) { |
163
|
1 |
|
$keys = array_merge($keys, $this->parseKeysString($argument)); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
1 |
|
return $keys; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Extract individual keys from a delimited string. |
172
|
|
|
* |
173
|
|
|
* @since 0.1.6 |
174
|
|
|
* |
175
|
|
|
* @param string $keyString Delimited string of keys. |
176
|
|
|
* @return array Array of key strings. |
177
|
|
|
*/ |
178
|
1 |
|
protected function parseKeysString($keyString) |
179
|
|
|
{ |
180
|
|
|
// Replace all of the configured delimiters by the first one, so that we can then use explode(). |
181
|
1 |
|
$normalizedString = str_replace($this->delimiter, $this->delimiter[0], $keyString); |
182
|
|
|
|
183
|
1 |
|
return (array)explode($this->delimiter[0], $normalizedString); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Validate the Config file. |
188
|
|
|
* |
189
|
|
|
* @since 0.1.0 |
190
|
|
|
* @return boolean |
191
|
|
|
*/ |
192
|
|
|
abstract public function isValid(); |
193
|
|
|
} |
194
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.