-
Notifications
You must be signed in to change notification settings - Fork 30
/
IniParser.php
272 lines (241 loc) · 7.99 KB
/
IniParser.php
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
<?php
/**
* [MIT Licensed](http://www.opensource.org/licenses/mit-license.php)
* Copyright (c) 2013 Austin Hyde
*
* Implements a parser for INI files that supports
* * Section inheritance
* * Property nesting
* * Simple arrays
*
* Compatible with PHP 5.2.0+
*
* @author Austin Hyde
* @author Till Klampaeckel <till@php.net>
*/
class IniParser {
/**
* Filename of our .ini file.
* @var string
*/
protected $file;
/**
* Enable/disable property nesting feature
* @var boolean
*/
public $property_nesting = true;
/**
* Use ArrayObject to allow array work as object (true) or use native arrays (false)
* @var boolean
*/
public $use_array_object = true;
/**
* Include original sections (pre-inherit names) on the final output
* @var boolean
*/
public $include_original_sections = false;
/**
* Disable array literal parsing
*/
const NO_PARSE = 0;
/**
* Parse simple arrays using regex (ex: [a,b,c,...])
*/
const PARSE_SIMPLE = 1;
/**
* Parse array literals using JSON, allowing advanced features like
* dictionaries, array nesting, etc.
*/
const PARSE_JSON = 2;
/**
* Array literals parse mode
* @var int
*/
public $array_literals_behavior = self::PARSE_SIMPLE;
/**
* @param string $file
*
* @return IniParser
*/
public function __construct($file = null) {
if ($file !== null) {
$this->setFile($file);
}
}
/**
* Parses an INI file
*
* @param string $file
* @return array
*/
public function parse($file = null) {
if ($file !== null) {
$this->setFile($file);
}
if (empty($this->file)) {
throw new LogicException("Need a file to parse.");
}
$simple_parsed = parse_ini_file($this->file, true);
$inheritance_parsed = $this->parseSections($simple_parsed);
return $this->parseKeys($inheritance_parsed);
}
/**
* Parses a string with INI contents
*
* @param string $src
*
* @return array
*/
public function process($src) {
$simple_parsed = parse_ini_string($src, true);
$inheritance_parsed = $this->parseSections($simple_parsed);
return $this->parseKeys($inheritance_parsed);
}
/**
* @param string $file
*
* @return IniParser
* @throws InvalidArgumentException
*/
public function setFile($file) {
if (!file_exists($file) || !is_readable($file)) {
throw new InvalidArgumentException("The file '{$file}' cannot be opened.");
}
$this->file = $file;
return $this;
}
/**
* Parse sections and inheritance.
* @param array $simple_parsed
* @return array Parsed sections
*/
private function parseSections(array $simple_parsed) {
// do an initial pass to gather section names
$sections = array();
$globals = array();
foreach ($simple_parsed as $k => $v) {
if (is_array($v)) {
// $k is a section name
$sections[$k] = $v;
} else {
$globals[$k] = $v;
}
}
// now for each section, see if it uses inheritance
$output_sections = array();
foreach ($sections as $k => $v) {
$sects = array_map('trim', array_reverse(explode(':', $k)));
$root = array_pop($sects);
$arr = $v;
foreach ($sects as $s) {
if ($s === '^') {
$arr = array_merge($globals, $arr);
} elseif (array_key_exists($s, $output_sections)) {
$arr = array_merge($output_sections[$s], $arr);
} elseif (array_key_exists($s, $sections)) {
$arr = array_merge($sections[$s], $arr);
} else {
throw new UnexpectedValueException("IniParser: In file '{$this->file}', section '{$root}': Cannot inherit from unknown section '{$s}'");
}
}
if ($this->include_original_sections) {
$output_sections[$k] = $v;
}
$output_sections[$root] = $arr;
}
return $globals + $output_sections;
}
/**
* @param array $arr
*
* @return array
*/
private function parseKeys(array $arr) {
$output = $this->getArrayValue();
$append_regex = '/\s*\+\s*$/';
foreach ($arr as $k => $v) {
if (is_array($v) && FALSE === strpos($k, '.')) {
// this element represents a section; recursively parse the value
$output[$k] = $this->parseKeys($v);
} else {
// if the key ends in a +, it means we should append to the previous value, if applicable
$append = false;
if (preg_match($append_regex, $k)) {
$k = preg_replace($append_regex, '', $k);
$append = true;
}
// transform "a.b.c = x" into $output[a][b][c] = x
$current = & $output;
$path = $this->property_nesting ? explode('.', $k) : array($k);
while (($current_key = array_shift($path)) !== null) {
if ('string' === gettype($current)) {
$current = array($current);
}
if (!array_key_exists($current_key, $current)) {
if (!empty($path)) {
$current[$current_key] = $this->getArrayValue();
} else {
$current[$current_key] = null;
}
}
$current = & $current[$current_key];
}
// parse value
$value = $v;
if (!is_array($v)) {
$value = $this->parseValue($v);
}
if ($append && $current !== null) {
if (is_array($value)) {
if (!is_array($current)) {
throw new LogicException("Cannot append array to inherited value '{$k}'");
}
$value = array_merge($current, $value);
} else {
$value = $current . $value;
}
}
$current = $value;
}
}
return $output;
}
/**
* Parses and formats the value in a key-value pair
*
* @param string $value
*
* @return mixed
*/
protected function parseValue($value) {
switch ($this->array_literals_behavior) {
case self::PARSE_JSON:
if (in_array(substr($value, 0, 1), array('[', '{')) && in_array(substr($value, -1), array(']', '}'))) {
if (defined('JSON_BIGINT_AS_STRING')) {
$output = json_decode($value, true, 512, JSON_BIGINT_AS_STRING);
} else {
$output = json_decode($value, true);
}
if ($output !== NULL) {
return $output;
}
}
// fallthrough
// try regex parser for simple estructures not JSON-compatible (ex: colors = [blue, green, red])
case self::PARSE_SIMPLE:
// if the value looks like [a,b,c,...], interpret as array
if (preg_match('/^\[\s*.*?(?:\s*,\s*.*?)*\s*\]$/', trim($value))) {
return array_map('trim', explode(',', trim(trim($value), '[]')));
}
break;
}
return $value;
}
protected function getArrayValue($array = array()) {
if ($this->use_array_object) {
return new ArrayObject($array, ArrayObject::ARRAY_AS_PROPS);
} else {
return $array;
}
}
}