forked from Respect/Config
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIniLoader.php
More file actions
313 lines (250 loc) · 8.18 KB
/
IniLoader.php
File metadata and controls
313 lines (250 loc) · 8.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
declare(strict_types=1);
namespace Respect\Config;
use Closure;
use InvalidArgumentException;
use function constant;
use function count;
use function defined;
use function explode;
use function file_exists;
use function is_array;
use function is_numeric;
use function is_string;
use function parse_ini_file;
use function parse_ini_string;
use function preg_match;
use function preg_replace;
use function preg_replace_callback;
use function str_contains;
use function strstr;
use function trim;
class IniLoader
{
public function __construct(protected Container $container)
{
}
public static function load(mixed $input, Container|null $container = null): Container
{
$container ??= new Container();
return (new self($container))->interpret($input);
}
public function interpret(mixed $input): Container
{
if ($input === null) {
return $this->container;
}
if (is_array($input)) {
return $this->fromArray($input);
}
if (is_string($input) && file_exists($input)) {
return $this->fromFile($input);
}
if (is_string($input)) {
return $this->fromString($input);
}
throw new InvalidArgumentException('Invalid input. Must be a valid file or array');
}
public function fromString(string $configurator): Container
{
$iniData = parse_ini_string($configurator, true);
if ($iniData === false || count($iniData) === 0) {
throw new InvalidArgumentException('Invalid configuration string');
}
return $this->fromArray($iniData);
}
public function fromFile(string $configurator): Container
{
$iniData = parse_ini_file($configurator, true);
if ($iniData === false) {
throw new InvalidArgumentException('Invalid configuration INI file');
}
return $this->fromArray($iniData);
}
/** @param array<string, mixed> $configurator */
public function fromArray(array $configurator): Container
{
foreach ($configurator as $key => $value) {
$stringKey = (string) $key;
// State takes precedence: use existing non-Instantiator value instead
if (
$this->container->offsetExists($stringKey)
&& !$this->container[$stringKey] instanceof Instantiator
) {
$value = $this->container[$stringKey];
}
if ($value instanceof Closure) {
$this->container->offsetSet($stringKey, $value);
continue;
}
if ($value instanceof Instantiator) {
$this->container->offsetSet($stringKey, $value);
continue;
}
$this->parseItem($key, $value);
}
return $this->container;
}
protected function existingKeyFrom(string $key): string|false
{
$k = strstr($key, ' ', true) ?: $key;
return $this->container->offsetExists($k) ? $k : false;
}
protected function keyHasInstantiator(string $key): bool
{
return str_contains($key, ' ');
}
protected function parseItem(string|int $key, mixed $value): void
{
$key = trim((string) $key);
if ($this->keyHasInstantiator($key)) {
$existingKey = $this->existingKeyFrom($key);
if ($existingKey !== false) {
$this->container->offsetSet($key, $this->container[$existingKey]);
} else {
$this->parseInstantiator($key, $value);
}
} else {
$this->parseStandardItem($key, $value);
}
}
/**
* @param array<mixed> $value
*
* @return array<mixed>
*/
protected function parseSubValues(array &$value): array
{
foreach ($value as &$subValue) {
$subValue = $this->parseValue($subValue);
}
return $value;
}
protected function parseStandardItem(string $key, mixed $value): void
{
if (is_array($value)) {
$this->parseSubValues($value);
} else {
$value = $this->parseValue($value);
}
$this->container->offsetSet($key, $value);
}
protected function removeDuplicatedSpaces(string $string): string
{
if (!str_contains($string, ' ')) {
return $string;
}
return (string) preg_replace('/\s+/', ' ', $string);
}
protected function parseInstantiator(string $key, mixed $value): void
{
$key = $this->removeDuplicatedSpaces($key);
/** @var class-string $keyClass */
[$keyName, $keyClass] = explode(' ', $key, 2);
if ($keyName === 'instanceof') {
$keyName = $keyClass;
}
$instantiator = $this->createInstantiator($keyClass);
if (is_array($value)) {
foreach ($value as $property => $pValue) {
$instantiator->setParam($property, $this->parseValue($pValue));
}
} else {
$instantiator->setParam('__construct', $this->parseValue($value));
}
$this->container->offsetSet($keyName, $instantiator);
}
/** @param class-string $keyClass */
protected function createInstantiator(string $keyClass): Instantiator
{
if (!str_contains($keyClass, ' ')) {
return new Instantiator($keyClass);
}
/** @var class-string $className */
[$modifier, $className] = explode(' ', $keyClass, 2);
return match ($modifier) {
'new' => new Factory($className),
'autowire' => new Autowire($className),
default => new Instantiator($keyClass),
};
}
protected function parseValue(mixed $value): mixed
{
if ($value instanceof Instantiator) {
return $value;
}
if (is_array($value)) {
return $this->parseSubValues($value);
}
if ($value === '' || $value === null) {
return null;
}
if (!is_string($value)) {
return $value;
}
return $this->parseSingleValue($value);
}
protected function hasCompleteBrackets(string $value): bool
{
return str_contains($value, '[') && str_contains($value, ']');
}
protected function parseSingleValue(string $value): mixed
{
$value = trim($value);
if ($this->hasCompleteBrackets($value)) {
return $this->parseBrackets($value);
}
return $this->parseConstants($value);
}
protected function parseConstants(string $value): mixed
{
if (preg_match('/^[\\\\a-zA-Z_]+([:]{2}[A-Z_]+)?$/', $value) && defined($value)) {
return constant($value);
}
if (is_numeric($value)) {
$isFloat = str_contains($value, '.') || str_contains($value, 'e') || str_contains($value, 'E');
return $isFloat ? (float) $value : (int) $value;
}
return $value;
}
protected function matchSequence(string &$value): bool
{
if (preg_match('/^\[(.*?,.*?)\]$/', $value, $match)) {
$value = $match[1];
return true;
}
return false;
}
protected function matchReference(string &$value): bool
{
if (preg_match('/^\[([[:alnum:]_\\\\]+)\]$/', $value, $match)) {
$value = $match[1];
return true;
}
return false;
}
protected function parseBrackets(string $value): mixed
{
if ($this->matchSequence($value)) {
return $this->parseArgumentList($value);
}
if ($this->matchReference($value)) {
return $this->container->getItem($value, true);
}
return $this->parseVariables($value);
}
protected function parseVariables(string $value): string
{
return (string) preg_replace_callback(
'/\[(\w+)\]/',
fn(array $match): string => (string) ($this->container[$match[1]] ?? ''),
$value,
);
}
/** @return array<mixed> */
protected function parseArgumentList(string $value): array
{
$subValues = explode(',', $value);
return $this->parseSubValues($subValues);
}
}