1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Konfig. |
5
|
|
|
* |
6
|
|
|
* Yet another simple configuration loader library. |
7
|
|
|
* |
8
|
|
|
* PHP version 5 |
9
|
|
|
* |
10
|
|
|
* @category Library |
11
|
|
|
* @package Konfig |
12
|
|
|
* @author Xeriab Nabil (aka KodeBurner) <[email protected]> |
13
|
|
|
* @license https://raw.github.com/xeriab/konfig/master/LICENSE MIT |
14
|
|
|
* @link https://xeriab.github.io/projects/konfig |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace Exen\Konfig\FileParser; |
18
|
|
|
|
19
|
|
|
use Exception; |
20
|
|
|
use Exen\Konfig\Arr; |
21
|
|
|
use Exen\Konfig\Utils; |
22
|
|
|
use Exen\Konfig\Exception\ParseException; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Properties |
26
|
|
|
* Konfig's Java-Properties parser class. |
27
|
|
|
* |
28
|
|
|
* @category FileParser |
29
|
|
|
* @package Konfig |
30
|
|
|
* @author Xeriab Nabil (aka KodeBurner) <[email protected]> |
31
|
|
|
* @license https://raw.github.com/xeriab/konfig/master/LICENSE MIT |
32
|
|
|
* @link https://xeriab.github.io/projects/konfig |
33
|
|
|
* |
34
|
|
|
* @implements Exen\Konfig\FileParser\AbstractFileParser |
35
|
|
|
*/ |
36
|
|
|
class Properties extends AbstractFileParser |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* Loads a PROPERTIES file as an array. |
40
|
|
|
* |
41
|
|
|
* @param string $path File path |
42
|
|
|
* |
43
|
|
|
* @throws ParseException If there is an error parsing PROPERTIES file |
44
|
|
|
* |
45
|
|
|
* @return array The parsed data |
46
|
|
|
* |
47
|
|
|
* @since 0.2.4 |
48
|
|
|
*/ |
49
|
|
|
public function parse($path) |
50
|
|
|
{ |
51
|
6 |
|
$data = null; |
52
|
|
|
|
53
|
6 |
|
$this->loadFile($path); |
54
|
|
|
|
55
|
6 |
|
// if (!$data || empty($data) || !is_array($data)) { |
56
|
|
|
// throw new ParseException( |
57
|
6 |
|
// [ |
58
|
3 |
|
// 'message' => 'Error parsing PROPERTIES file', |
59
|
|
|
// 'file' => $path, |
60
|
3 |
|
// ] |
61
|
3 |
|
// ); |
62
|
|
|
// } |
63
|
2 |
|
|
64
|
|
|
try { |
65
|
|
|
$data = $this->getProperties(); |
66
|
3 |
|
} catch (Exception $ex) { |
67
|
|
|
throw new ParseException( |
68
|
|
|
[ |
69
|
|
|
'message' => 'Error parsing PROPERTIES file', |
70
|
|
|
'exception' => $ex |
71
|
|
|
] |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $data; |
76
|
3 |
|
} |
77
|
|
|
|
78
|
3 |
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
* |
81
|
|
|
* @return array Supported extensions |
82
|
|
|
* |
83
|
|
|
* @since 0.1.0 |
84
|
|
|
*/ |
85
|
|
|
public function getSupportedFileExtensions() |
86
|
|
|
{ |
87
|
|
|
return ['properties']; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Parse Java-Properties |
92
|
|
|
* |
93
|
|
|
* @return array The parsed data |
94
|
|
|
* @since 0.2.6 |
95
|
|
|
* @codeCoverageIgnore |
96
|
|
|
*/ |
97
|
|
|
private function parseProperties() |
98
|
|
|
{ |
99
|
|
|
$analysis = []; |
100
|
|
|
|
101
|
|
|
static $isWaitingForOtherLine = false; |
102
|
|
|
|
103
|
|
|
foreach ($this->parsedFile as $lineNb => $line) { |
|
|
|
|
104
|
|
|
$ifl = array_flip(array_keys($this->parsedFile))[$lineNb] + 1; |
105
|
|
|
|
106
|
|
|
if ((empty($line) || is_null($line)) && !$isWaitingForOtherLine) { |
107
|
|
|
$analysis[$lineNb] = ['emptyline', null, 'line' => $ifl]; |
108
|
|
|
continue; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (!$isWaitingForOtherLine && Utils::stringStart('#', $line)) { |
112
|
|
|
$analysis[$lineNb] = [ |
113
|
|
|
'comment', |
114
|
|
|
trim(substr($line, 0)), |
115
|
|
|
'line' => $ifl |
116
|
|
|
]; |
117
|
|
|
|
118
|
|
|
continue; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// Property name, check for escaped equal sign |
122
|
|
|
if (substr_count($line, '=') > substr_count($line, '\=')) { |
123
|
|
|
$temp = explode('=', $line, 2); |
124
|
|
|
$temp = Utils::trimArrayElements($temp); |
125
|
|
|
|
126
|
|
|
if (count($temp) === 2) { |
127
|
|
|
$temp[1] = Utils::removeQuotes($temp[1]); |
128
|
|
|
$analysis[$lineNb] = [ |
129
|
|
|
'property', |
130
|
|
|
$temp[0], |
131
|
|
|
$temp[1], |
132
|
|
|
'line' => $ifl |
133
|
|
|
]; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
unset($temp); |
137
|
|
|
|
138
|
|
|
continue; |
139
|
|
|
} else { |
140
|
|
|
throw new Exception('Wrong Configuration'); |
141
|
|
|
// break; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
// Multiline data |
145
|
|
|
if (substr_count($line, '=') === 0) { |
|
|
|
|
146
|
|
|
$analysis[$lineNb] = ['multiline', null, $line, 'line' => $ifl]; |
147
|
|
|
continue; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
// Second pass, we associate comments to entities |
152
|
|
|
$counter = Utils::getNumberLinesMatching('comment', $analysis); |
153
|
|
|
|
154
|
|
|
while ($counter > 0) { |
155
|
|
|
foreach ($analysis as $lineNb => $line) { |
156
|
|
|
if ($line[0] === 'comment' |
157
|
|
|
&& isset($analysis[$lineNb + 1][0]) |
158
|
|
|
&& $analysis[$lineNb + 1][0] === 'comment' |
159
|
|
|
) { |
160
|
|
|
$analysis[$lineNb][1] .= ' ' . $analysis[$lineNb + 1][1]; |
161
|
|
|
$analysis[$lineNb + 1][0] = 'erase'; |
162
|
|
|
|
163
|
|
|
break; |
164
|
|
|
} elseif ($line[0] === 'comment' |
165
|
|
|
&& isset($analysis[$lineNb + 1][0]) |
166
|
|
|
&& $analysis[$lineNb + 1][0] === 'property' |
167
|
|
|
) { |
168
|
|
|
// $analysis[$lineNb + 1][3] = $line[1]; |
169
|
|
|
$analysis[$lineNb][0] = 'erase'; |
170
|
|
|
} elseif ($line[0] === 'comment' |
171
|
|
|
&& isset($analysis[$lineNb + 1][0]) |
172
|
|
|
&& $analysis[$lineNb + 1][0] === 'emptyline' |
173
|
|
|
) { |
174
|
|
|
// $analysis[$lineNb + 1][3] = $line[1]; |
175
|
|
|
$analysis[$lineNb][0] = 'erase'; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$counter = Utils::getNumberLinesMatching('comment', $analysis); |
180
|
|
|
$analysis = $this->deleteFields('erase', $analysis); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
// Count emptylines |
184
|
|
|
$counter = Utils::getNumberLinesMatching('emptyline', $analysis); |
185
|
|
|
|
186
|
|
|
while ($counter > 0) { |
187
|
|
|
foreach ($analysis as $lineNb => $line) { |
188
|
|
|
if ($line[0] === 'emptyline') { |
189
|
|
|
$analysis[$lineNb][0] = 'erase'; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$counter = Utils::getNumberLinesMatching('emptyline', $analysis); |
194
|
|
|
$analysis = $this->deleteFields('erase', $analysis); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
// Third pass, we merge multiline strings |
198
|
|
|
|
199
|
|
|
// We remove the backslashes at end of strings if they exist |
200
|
|
|
$analysis = Utils::stripBackslashes($analysis); |
201
|
|
|
|
202
|
|
|
// Count # of multilines |
203
|
|
|
$counter = Utils::getNumberLinesMatching('multiline', $analysis); |
|
|
|
|
204
|
|
|
|
205
|
|
|
while ($counter > 0) { |
206
|
|
|
foreach ($analysis as $lineNb => $line) { |
|
|
|
|
207
|
|
|
if ($line[0] === 'multiline' |
208
|
|
|
&& isset($analysis[$lineNb - 1][0]) |
209
|
|
|
&& $analysis[$lineNb - 1][0] === 'property' |
210
|
|
|
) { |
211
|
|
|
$analysis[$lineNb - 1][2] .= ' ' . trim($line[2]); |
212
|
|
|
$analysis[$lineNb][0] = 'erase'; |
213
|
|
|
|
214
|
|
|
break; |
215
|
|
|
} elseif ($line[0] === 'multiline' |
216
|
|
|
&& isset($analysis[$lineNb - 1][0]) |
217
|
|
|
&& $analysis[$lineNb - 1][0] === 'emptyline' |
218
|
|
|
) { |
219
|
|
|
// $analysis[$lineNb - 1][2] .= ' ' . trim($line[2]); |
220
|
|
|
$analysis[$lineNb][0] = 'erase'; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
$counter = Utils::getNumberLinesMatching('multiline', $analysis); |
|
|
|
|
225
|
|
|
$analysis = $this->deleteFields('erase', $analysis); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
// Step 4, we clean up strings from escaped characters in properties |
229
|
|
|
$analysis = $this->unescapeProperties($analysis); |
230
|
|
|
|
231
|
|
|
// Step 5, we only have properties now, remove redondant field 0 |
232
|
|
|
foreach ($analysis as $key => $value) { |
|
|
|
|
233
|
|
|
if (preg_match('/^[1-9][0-9]*$/', $value[2])) { |
234
|
|
|
$value[2] = intval($value[2]); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
array_splice($analysis[$key], 0, 1); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return $analysis; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* {@inheritdoc} |
245
|
|
|
* |
246
|
|
|
* @return array The exteacted data |
247
|
|
|
* |
248
|
|
|
* @since 0.2.4 |
249
|
|
|
* @codeCoverageIgnore |
250
|
|
|
*/ |
251
|
|
|
public function extractData() |
252
|
|
|
{ |
253
|
|
|
$analysis = []; |
254
|
|
|
|
255
|
|
|
// First pass, we categorize each line |
256
|
|
|
foreach ($this->parsedFile as $lineNb => $line) { |
|
|
|
|
257
|
|
|
// Property name, check for escaped equal sign |
258
|
|
|
if (Utils::stringStart('#', $line)) { |
259
|
|
|
$analysis[$lineNb] = ['comment', trim(substr($line, 0))]; |
260
|
|
|
continue; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
// Property name, check for escaped equal sign |
264
|
|
|
if (substr_count($line, '=') > substr_count($line, '\=')) { |
265
|
|
|
$temp = explode('=', $line, 2); |
266
|
|
|
$temp = Utils::trimArrayElements($temp); |
267
|
|
|
|
268
|
|
|
if (count($temp) === 2) { |
269
|
|
|
$temp[1] = Utils::removeQuotes($temp[1]); |
270
|
|
|
$analysis[$lineNb] = ['property', $temp[0], $temp[1]]; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
unset($temp); |
274
|
|
|
|
275
|
|
|
continue; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
// Multiline data |
279
|
|
|
if (substr_count($line, '=') === 0) { |
280
|
|
|
$analysis[$lineNb] = ['multiline', '', $line]; |
281
|
|
|
continue; |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
// Second pass, we associate comments to entities |
286
|
|
|
$counter = Utils::getNumberLinesMatching('comment', $analysis); |
287
|
|
|
|
288
|
|
|
while ($counter > 0) { |
289
|
|
|
foreach ($analysis as $lineNb => $line) { |
290
|
|
|
if ($line[0] === 'comment' |
291
|
|
|
&& isset($analysis[$lineNb + 1][0]) |
292
|
|
|
&& $analysis[$lineNb + 1][0] === 'comment' |
293
|
|
|
) { |
294
|
|
|
$analysis[$lineNb][1] .= ' '.$analysis[$lineNb + 1][1]; |
295
|
|
|
$analysis[$lineNb + 1][0] = 'erase'; |
296
|
|
|
|
297
|
|
|
break; |
298
|
|
|
} elseif ($line[0] === 'comment' |
299
|
|
|
&& isset($analysis[$lineNb + 1][0]) |
300
|
|
|
&& $analysis[$lineNb + 1][0] === 'property' |
301
|
|
|
) { |
302
|
|
|
$analysis[$lineNb + 1][3] = $line[1]; |
303
|
|
|
$analysis[$lineNb][0] = 'erase'; |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
$counter = Utils::getNumberLinesMatching('comment', $analysis); |
308
|
|
|
$analysis = $this->deleteFields('erase', $analysis); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
// Third pass, we merge multiline strings |
312
|
|
|
|
313
|
|
|
// We remove the backslashes at end of strings if they exist |
314
|
|
|
$analysis = Utils::stripBackslashes($analysis); |
315
|
|
|
|
316
|
|
|
// Count # of multilines |
317
|
|
|
$counter = Utils::getNumberLinesMatching('multiline', $analysis); |
|
|
|
|
318
|
|
|
|
319
|
|
|
while ($counter > 0) { |
320
|
|
|
foreach ($analysis as $lineNb => $line) { |
|
|
|
|
321
|
|
|
if ($line[0] === 'multiline' |
322
|
|
|
&& isset($analysis[$lineNb - 1][0]) |
323
|
|
|
&& $analysis[$lineNb - 1][0] === 'property' |
324
|
|
|
) { |
325
|
|
|
$analysis[$lineNb - 1][2] .= ' ' . trim($line[2]); |
326
|
|
|
$analysis[$lineNb][0] = 'erase'; |
327
|
|
|
break; |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
$counter = Utils::getNumberLinesMatching('multiline', $analysis); |
|
|
|
|
332
|
|
|
$analysis = $this->deleteFields('erase', $analysis); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
// Step 4, we clean up strings from escaped characters in properties |
336
|
|
|
$analysis = $this->unescapeProperties($analysis); |
337
|
|
|
|
338
|
|
|
// Step 5, we only have properties now, remove redondant field 0 |
339
|
|
|
foreach ($analysis as $key => $value) { |
|
|
|
|
340
|
|
|
if (preg_match('/^[1-9][0-9]*$/', $value[2])) { |
341
|
|
|
$value[2] = intval($value[2]); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
array_splice($analysis[$key], 0, 1); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
return $analysis; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* {@inheritdoc} |
352
|
|
|
* |
353
|
|
|
* @param array|null $analysis Configuration items |
354
|
|
|
* |
355
|
|
|
* @return array The configuration items |
356
|
|
|
* |
357
|
|
|
* @since 0.2.4 |
358
|
|
|
* @codeCoverageIgnore |
359
|
|
|
*/ |
360
|
|
|
private function unescapeProperties($analysis) |
361
|
|
|
{ |
362
|
|
|
foreach ($analysis as $key => $value) { |
|
|
|
|
363
|
|
|
$analysis[$key][2] = str_replace('\=', '=', $value[2]); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
return $analysis; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* {@inheritdoc} |
371
|
|
|
* |
372
|
|
|
* @param string $field Field name |
373
|
|
|
* @param array|null $analysis Configuration items |
374
|
|
|
* |
375
|
|
|
* @return array Configuration items after deletion |
376
|
|
|
* |
377
|
|
|
* @since 0.2.4 |
378
|
|
|
* @codeCoverageIgnore |
379
|
|
|
*/ |
380
|
|
|
private function deleteFields($field, $analysis) |
381
|
|
|
{ |
382
|
|
|
foreach ($analysis as $key => $value) { |
|
|
|
|
383
|
|
|
if ($value[0] === $field) { |
384
|
|
|
unset($analysis[$key]); |
385
|
|
|
} |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
return array_values($analysis); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* {@inheritdoc} |
393
|
|
|
* |
394
|
|
|
* @param string|bool|null $file File path |
395
|
|
|
* |
396
|
|
|
* @return array Configuration items |
397
|
|
|
* |
398
|
|
|
* @since 0.2.4 |
399
|
|
|
* @codeCoverageIgnore |
400
|
|
|
*/ |
401
|
|
|
public function getProperties($file = null) |
402
|
|
|
{ |
403
|
|
|
if ($file && !is_null($file)) { |
404
|
|
|
$this->loadFile($file); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
// $source = $this->extractData(); |
408
|
|
|
$source = $this->parseProperties(); |
409
|
|
|
$data = []; |
410
|
|
|
|
411
|
|
|
foreach ($source as $value) { |
|
|
|
|
412
|
|
|
Arr::set($data, $value[0], $value[1]); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
unset($this->parsedFile); |
416
|
|
|
|
417
|
|
|
return $data; |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* Loads in the given file and parses it. |
422
|
|
|
* |
423
|
|
|
* @param string|bool|null $file File to load |
424
|
|
|
* |
425
|
|
|
* @return array The parsed file data |
426
|
|
|
* |
427
|
|
|
* @since 0.2.4 |
428
|
|
|
* @codeCoverageIgnore |
429
|
|
|
*/ |
430
|
|
|
protected function loadFile($file = null) |
431
|
|
|
{ |
432
|
|
|
$this->file = is_file($file) ? $file : false; |
|
|
|
|
433
|
|
|
|
434
|
|
|
$contents = $this->parseVars(Utils::getContent($this->file)); |
|
|
|
|
435
|
|
|
|
436
|
|
|
if ($this->file && !is_null($file)) { |
437
|
|
|
$this->parsedFile = Utils::fileContentToArray($contents); |
|
|
|
|
438
|
|
|
// $this->parsedFile = Utils::fileToArray($this->file); |
439
|
|
|
} else { |
440
|
|
|
$this->parsedFile = false; |
|
|
|
|
441
|
|
|
} |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* Returns the formatted configuration file contents. |
446
|
|
|
* |
447
|
|
|
* @param array $contents configuration array |
448
|
|
|
* |
449
|
|
|
* @return string formatted configuration file contents |
450
|
|
|
* |
451
|
|
|
* @since 0.2.4 |
452
|
|
|
* @codeCoverageIgnore |
453
|
|
|
*/ |
454
|
|
|
protected function exportFormat($contents = null) |
455
|
|
|
{ |
456
|
|
|
throw new Exception( |
457
|
|
|
'Saving configuration to `Properties` is not supported at this time' |
458
|
|
|
); |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
/** |
462
|
|
|
* __toString. |
463
|
|
|
* |
464
|
|
|
* @return string |
465
|
|
|
* @since 0.1.2 |
466
|
|
|
* @codeCoverageIgnore |
467
|
|
|
*/ |
468
|
|
|
public function __toString() |
469
|
|
|
{ |
470
|
|
|
return 'Exen\Konfig\FileParser\Properties' . PHP_EOL; |
471
|
|
|
} |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
// END OF ./src/FileParser/Properties.php FILE |
475
|
|
|
|