|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Noodlehaus\Parser; |
|
4
|
|
|
|
|
5
|
|
|
use Noodlehaus\Exception\ParseException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* INI parser |
|
9
|
|
|
* |
|
10
|
|
|
* @package Config |
|
11
|
|
|
* @author Jesus A. Domingo <[email protected]> |
|
12
|
|
|
* @author Hassan Khan <[email protected]> |
|
13
|
|
|
* @author Filip Š <[email protected]> |
|
14
|
|
|
* @link https://github.com/noodlehaus/config |
|
15
|
|
|
* @license MIT |
|
16
|
|
|
*/ |
|
17
|
|
|
class Ini implements ParserInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* {@inheritDoc} |
|
21
|
|
|
* Parses an INI file as an array |
|
22
|
|
|
* |
|
23
|
|
|
* @throws ParseException If there is an error parsing the INI file |
|
24
|
|
|
*/ |
|
25
|
|
|
public function parseFile($filename) |
|
26
|
|
|
{ |
|
27
|
12 |
|
$data = @parse_ini_file($filename, true); |
|
28
|
|
|
return $this->parse($data, $filename); |
|
|
|
|
|
|
29
|
12 |
|
} |
|
30
|
|
|
|
|
31
|
12 |
|
/** |
|
32
|
6 |
|
* {@inheritDoc} |
|
33
|
|
|
* Parses an INI string as an array |
|
34
|
|
|
* |
|
35
|
6 |
|
* @throws ParseException If there is an error parsing the INI string |
|
36
|
3 |
|
*/ |
|
37
|
1 |
|
public function parseString($config) |
|
38
|
|
|
{ |
|
39
|
6 |
|
$data = @parse_ini_string($config, true); |
|
40
|
|
|
return $this->parse($data); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
6 |
|
/** |
|
44
|
4 |
|
* Completes parsing of INI data |
|
45
|
|
|
* |
|
46
|
|
|
* @param array $data |
|
47
|
6 |
|
* @param strring $filename |
|
48
|
|
|
* |
|
49
|
|
|
* @throws ParseException If there is an error parsing the INI data |
|
50
|
6 |
|
*/ |
|
51
|
|
|
protected function parse($data = null, $filename = null) |
|
52
|
|
|
{ |
|
53
|
|
|
if (!$data) { |
|
54
|
|
|
$error = error_get_last(); |
|
55
|
|
|
|
|
56
|
|
|
// Parse functions may return NULL but set no error if the string contains no parsable data |
|
57
|
|
|
if (!is_array($error)) { |
|
58
|
|
|
$error["message"] = "No parsable content in data."; |
|
59
|
|
|
} |
|
60
|
3 |
|
|
|
61
|
|
|
$error["file"] = $filename; |
|
62
|
3 |
|
|
|
63
|
3 |
|
// if string contains no parsable data, no error is set, resulting in any previous error |
|
64
|
3 |
|
// persisting in error_get_last(). in php 7 this can be addressed with error_clear_last() |
|
65
|
3 |
|
if (function_exists("error_clear_last")) { |
|
66
|
|
|
error_clear_last(); |
|
67
|
3 |
|
} |
|
68
|
3 |
|
|
|
69
|
3 |
|
throw new ParseException($error); |
|
70
|
1 |
|
} |
|
71
|
3 |
|
|
|
72
|
|
|
return $this->expandDottedKey($data); |
|
73
|
3 |
|
} |
|
74
|
1 |
|
|
|
75
|
1 |
|
/** |
|
76
|
3 |
|
* Expand array with dotted keys to multidimensional array |
|
77
|
|
|
* |
|
78
|
|
|
* @param array $data |
|
79
|
|
|
* |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
3 |
|
protected function expandDottedKey($data) |
|
83
|
|
|
{ |
|
84
|
3 |
|
foreach ($data as $key => $value) { |
|
85
|
|
|
if (($found = strpos($key, '.')) !== false) { |
|
86
|
|
|
$newKey = substr($key, 0, $found); |
|
87
|
|
|
$remainder = substr($key, $found + 1); |
|
88
|
|
|
|
|
89
|
|
|
$expandedValue = $this->expandDottedKey([$remainder => $value]); |
|
90
|
|
|
if (isset($data[$newKey])) { |
|
91
|
|
|
$data[$newKey] = array_merge_recursive($data[$newKey], $expandedValue); |
|
92
|
|
|
} else { |
|
93
|
|
|
$data[$newKey] = $expandedValue; |
|
94
|
|
|
} |
|
95
|
|
|
unset($data[$key]); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
return $data; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* {@inheritDoc} |
|
103
|
|
|
*/ |
|
104
|
|
|
public static function getSupportedExtensions() |
|
105
|
|
|
{ |
|
106
|
|
|
return ['ini']; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: