Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Yaml;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Yaml\Exception\ParseException;
|
Chris@0
|
15 use Symfony\Component\Yaml\Exception\DumpException;
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * Inline implements a YAML parser/dumper for the YAML inline syntax.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
21 *
|
Chris@0
|
22 * @internal
|
Chris@0
|
23 */
|
Chris@0
|
24 class Inline
|
Chris@0
|
25 {
|
Chris@0
|
26 const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*+(?:\\\\.[^"\\\\]*+)*+)"|\'([^\']*+(?:\'\'[^\']*+)*+)\')';
|
Chris@0
|
27
|
Chris@0
|
28 public static $parsedLineNumber;
|
Chris@0
|
29
|
Chris@0
|
30 private static $exceptionOnInvalidType = false;
|
Chris@0
|
31 private static $objectSupport = false;
|
Chris@0
|
32 private static $objectForMap = false;
|
Chris@0
|
33 private static $constantSupport = false;
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * Converts a YAML string to a PHP value.
|
Chris@0
|
37 *
|
Chris@0
|
38 * @param string $value A YAML string
|
Chris@0
|
39 * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
|
Chris@0
|
40 * @param array $references Mapping of variable names to values
|
Chris@0
|
41 *
|
Chris@0
|
42 * @return mixed A PHP value
|
Chris@0
|
43 *
|
Chris@0
|
44 * @throws ParseException
|
Chris@0
|
45 */
|
Chris@0
|
46 public static function parse($value, $flags = 0, $references = array())
|
Chris@0
|
47 {
|
Chris@0
|
48 if (is_bool($flags)) {
|
Chris@0
|
49 @trigger_error('Passing a boolean flag to toggle exception handling is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED);
|
Chris@0
|
50
|
Chris@0
|
51 if ($flags) {
|
Chris@0
|
52 $flags = Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE;
|
Chris@0
|
53 } else {
|
Chris@0
|
54 $flags = 0;
|
Chris@0
|
55 }
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 if (func_num_args() >= 3 && !is_array($references)) {
|
Chris@0
|
59 @trigger_error('Passing a boolean flag to toggle object support is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::PARSE_OBJECT flag instead.', E_USER_DEPRECATED);
|
Chris@0
|
60
|
Chris@0
|
61 if ($references) {
|
Chris@0
|
62 $flags |= Yaml::PARSE_OBJECT;
|
Chris@0
|
63 }
|
Chris@0
|
64
|
Chris@0
|
65 if (func_num_args() >= 4) {
|
Chris@0
|
66 @trigger_error('Passing a boolean flag to toggle object for map support is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::PARSE_OBJECT_FOR_MAP flag instead.', E_USER_DEPRECATED);
|
Chris@0
|
67
|
Chris@0
|
68 if (func_get_arg(3)) {
|
Chris@0
|
69 $flags |= Yaml::PARSE_OBJECT_FOR_MAP;
|
Chris@0
|
70 }
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 if (func_num_args() >= 5) {
|
Chris@0
|
74 $references = func_get_arg(4);
|
Chris@0
|
75 } else {
|
Chris@0
|
76 $references = array();
|
Chris@0
|
77 }
|
Chris@0
|
78 }
|
Chris@0
|
79
|
Chris@0
|
80 self::$exceptionOnInvalidType = (bool) (Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE & $flags);
|
Chris@0
|
81 self::$objectSupport = (bool) (Yaml::PARSE_OBJECT & $flags);
|
Chris@0
|
82 self::$objectForMap = (bool) (Yaml::PARSE_OBJECT_FOR_MAP & $flags);
|
Chris@0
|
83 self::$constantSupport = (bool) (Yaml::PARSE_CONSTANT & $flags);
|
Chris@0
|
84
|
Chris@0
|
85 $value = trim($value);
|
Chris@0
|
86
|
Chris@0
|
87 if ('' === $value) {
|
Chris@0
|
88 return '';
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 if (2 /* MB_OVERLOAD_STRING */ & (int) ini_get('mbstring.func_overload')) {
|
Chris@0
|
92 $mbEncoding = mb_internal_encoding();
|
Chris@0
|
93 mb_internal_encoding('ASCII');
|
Chris@0
|
94 }
|
Chris@0
|
95
|
Chris@0
|
96 $i = 0;
|
Chris@0
|
97 switch ($value[0]) {
|
Chris@0
|
98 case '[':
|
Chris@0
|
99 $result = self::parseSequence($value, $flags, $i, $references);
|
Chris@0
|
100 ++$i;
|
Chris@0
|
101 break;
|
Chris@0
|
102 case '{':
|
Chris@0
|
103 $result = self::parseMapping($value, $flags, $i, $references);
|
Chris@0
|
104 ++$i;
|
Chris@0
|
105 break;
|
Chris@0
|
106 default:
|
Chris@0
|
107 $result = self::parseScalar($value, $flags, null, array('"', "'"), $i, true, $references);
|
Chris@0
|
108 }
|
Chris@0
|
109
|
Chris@0
|
110 // some comments are allowed at the end
|
Chris@0
|
111 if (preg_replace('/\s+#.*$/A', '', substr($value, $i))) {
|
Chris@0
|
112 throw new ParseException(sprintf('Unexpected characters near "%s".', substr($value, $i)));
|
Chris@0
|
113 }
|
Chris@0
|
114
|
Chris@0
|
115 if (isset($mbEncoding)) {
|
Chris@0
|
116 mb_internal_encoding($mbEncoding);
|
Chris@0
|
117 }
|
Chris@0
|
118
|
Chris@0
|
119 return $result;
|
Chris@0
|
120 }
|
Chris@0
|
121
|
Chris@0
|
122 /**
|
Chris@0
|
123 * Dumps a given PHP variable to a YAML string.
|
Chris@0
|
124 *
|
Chris@0
|
125 * @param mixed $value The PHP variable to convert
|
Chris@0
|
126 * @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
|
Chris@0
|
127 *
|
Chris@0
|
128 * @return string The YAML string representing the PHP value
|
Chris@0
|
129 *
|
Chris@0
|
130 * @throws DumpException When trying to dump PHP resource
|
Chris@0
|
131 */
|
Chris@0
|
132 public static function dump($value, $flags = 0)
|
Chris@0
|
133 {
|
Chris@0
|
134 if (is_bool($flags)) {
|
Chris@0
|
135 @trigger_error('Passing a boolean flag to toggle exception handling is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED);
|
Chris@0
|
136
|
Chris@0
|
137 if ($flags) {
|
Chris@0
|
138 $flags = Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE;
|
Chris@0
|
139 } else {
|
Chris@0
|
140 $flags = 0;
|
Chris@0
|
141 }
|
Chris@0
|
142 }
|
Chris@0
|
143
|
Chris@0
|
144 if (func_num_args() >= 3) {
|
Chris@0
|
145 @trigger_error('Passing a boolean flag to toggle object support is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::DUMP_OBJECT flag instead.', E_USER_DEPRECATED);
|
Chris@0
|
146
|
Chris@0
|
147 if (func_get_arg(2)) {
|
Chris@0
|
148 $flags |= Yaml::DUMP_OBJECT;
|
Chris@0
|
149 }
|
Chris@0
|
150 }
|
Chris@0
|
151
|
Chris@0
|
152 switch (true) {
|
Chris@0
|
153 case is_resource($value):
|
Chris@0
|
154 if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
|
Chris@0
|
155 throw new DumpException(sprintf('Unable to dump PHP resources in a YAML file ("%s").', get_resource_type($value)));
|
Chris@0
|
156 }
|
Chris@0
|
157
|
Chris@0
|
158 return 'null';
|
Chris@0
|
159 case $value instanceof \DateTimeInterface:
|
Chris@0
|
160 return $value->format('c');
|
Chris@0
|
161 case is_object($value):
|
Chris@0
|
162 if (Yaml::DUMP_OBJECT & $flags) {
|
Chris@0
|
163 return '!php/object:'.serialize($value);
|
Chris@0
|
164 }
|
Chris@0
|
165
|
Chris@0
|
166 if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \stdClass || $value instanceof \ArrayObject)) {
|
Chris@0
|
167 return self::dumpArray((array) $value, $flags);
|
Chris@0
|
168 }
|
Chris@0
|
169
|
Chris@0
|
170 if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
|
Chris@0
|
171 throw new DumpException('Object support when dumping a YAML file has been disabled.');
|
Chris@0
|
172 }
|
Chris@0
|
173
|
Chris@0
|
174 return 'null';
|
Chris@0
|
175 case is_array($value):
|
Chris@0
|
176 return self::dumpArray($value, $flags);
|
Chris@0
|
177 case null === $value:
|
Chris@0
|
178 return 'null';
|
Chris@0
|
179 case true === $value:
|
Chris@0
|
180 return 'true';
|
Chris@0
|
181 case false === $value:
|
Chris@0
|
182 return 'false';
|
Chris@0
|
183 case ctype_digit($value):
|
Chris@0
|
184 return is_string($value) ? "'$value'" : (int) $value;
|
Chris@0
|
185 case is_numeric($value):
|
Chris@0
|
186 $locale = setlocale(LC_NUMERIC, 0);
|
Chris@0
|
187 if (false !== $locale) {
|
Chris@0
|
188 setlocale(LC_NUMERIC, 'C');
|
Chris@0
|
189 }
|
Chris@0
|
190 if (is_float($value)) {
|
Chris@0
|
191 $repr = (string) $value;
|
Chris@0
|
192 if (is_infinite($value)) {
|
Chris@0
|
193 $repr = str_ireplace('INF', '.Inf', $repr);
|
Chris@0
|
194 } elseif (floor($value) == $value && $repr == $value) {
|
Chris@0
|
195 // Preserve float data type since storing a whole number will result in integer value.
|
Chris@0
|
196 $repr = '!!float '.$repr;
|
Chris@0
|
197 }
|
Chris@0
|
198 } else {
|
Chris@0
|
199 $repr = is_string($value) ? "'$value'" : (string) $value;
|
Chris@0
|
200 }
|
Chris@0
|
201 if (false !== $locale) {
|
Chris@0
|
202 setlocale(LC_NUMERIC, $locale);
|
Chris@0
|
203 }
|
Chris@0
|
204
|
Chris@0
|
205 return $repr;
|
Chris@0
|
206 case '' == $value:
|
Chris@0
|
207 return "''";
|
Chris@0
|
208 case self::isBinaryString($value):
|
Chris@0
|
209 return '!!binary '.base64_encode($value);
|
Chris@0
|
210 case Escaper::requiresDoubleQuoting($value):
|
Chris@0
|
211 return Escaper::escapeWithDoubleQuotes($value);
|
Chris@0
|
212 case Escaper::requiresSingleQuoting($value):
|
Chris@0
|
213 case Parser::preg_match('{^[0-9]+[_0-9]*$}', $value):
|
Chris@0
|
214 case Parser::preg_match(self::getHexRegex(), $value):
|
Chris@0
|
215 case Parser::preg_match(self::getTimestampRegex(), $value):
|
Chris@0
|
216 return Escaper::escapeWithSingleQuotes($value);
|
Chris@0
|
217 default:
|
Chris@0
|
218 return $value;
|
Chris@0
|
219 }
|
Chris@0
|
220 }
|
Chris@0
|
221
|
Chris@0
|
222 /**
|
Chris@0
|
223 * Check if given array is hash or just normal indexed array.
|
Chris@0
|
224 *
|
Chris@0
|
225 * @internal
|
Chris@0
|
226 *
|
Chris@0
|
227 * @param array $value The PHP array to check
|
Chris@0
|
228 *
|
Chris@0
|
229 * @return bool true if value is hash array, false otherwise
|
Chris@0
|
230 */
|
Chris@0
|
231 public static function isHash(array $value)
|
Chris@0
|
232 {
|
Chris@0
|
233 $expectedKey = 0;
|
Chris@0
|
234
|
Chris@0
|
235 foreach ($value as $key => $val) {
|
Chris@0
|
236 if ($key !== $expectedKey++) {
|
Chris@0
|
237 return true;
|
Chris@0
|
238 }
|
Chris@0
|
239 }
|
Chris@0
|
240
|
Chris@0
|
241 return false;
|
Chris@0
|
242 }
|
Chris@0
|
243
|
Chris@0
|
244 /**
|
Chris@0
|
245 * Dumps a PHP array to a YAML string.
|
Chris@0
|
246 *
|
Chris@0
|
247 * @param array $value The PHP array to dump
|
Chris@0
|
248 * @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
|
Chris@0
|
249 *
|
Chris@0
|
250 * @return string The YAML string representing the PHP array
|
Chris@0
|
251 */
|
Chris@0
|
252 private static function dumpArray($value, $flags)
|
Chris@0
|
253 {
|
Chris@0
|
254 // array
|
Chris@0
|
255 if ($value && !self::isHash($value)) {
|
Chris@0
|
256 $output = array();
|
Chris@0
|
257 foreach ($value as $val) {
|
Chris@0
|
258 $output[] = self::dump($val, $flags);
|
Chris@0
|
259 }
|
Chris@0
|
260
|
Chris@0
|
261 return sprintf('[%s]', implode(', ', $output));
|
Chris@0
|
262 }
|
Chris@0
|
263
|
Chris@0
|
264 // hash
|
Chris@0
|
265 $output = array();
|
Chris@0
|
266 foreach ($value as $key => $val) {
|
Chris@0
|
267 $output[] = sprintf('%s: %s', self::dump($key, $flags), self::dump($val, $flags));
|
Chris@0
|
268 }
|
Chris@0
|
269
|
Chris@0
|
270 return sprintf('{ %s }', implode(', ', $output));
|
Chris@0
|
271 }
|
Chris@0
|
272
|
Chris@0
|
273 /**
|
Chris@0
|
274 * Parses a YAML scalar.
|
Chris@0
|
275 *
|
Chris@0
|
276 * @param string $scalar
|
Chris@0
|
277 * @param int $flags
|
Chris@0
|
278 * @param string[] $delimiters
|
Chris@0
|
279 * @param string[] $stringDelimiters
|
Chris@0
|
280 * @param int &$i
|
Chris@0
|
281 * @param bool $evaluate
|
Chris@0
|
282 * @param array $references
|
Chris@0
|
283 *
|
Chris@0
|
284 * @return string
|
Chris@0
|
285 *
|
Chris@0
|
286 * @throws ParseException When malformed inline YAML string is parsed
|
Chris@0
|
287 *
|
Chris@0
|
288 * @internal
|
Chris@0
|
289 */
|
Chris@0
|
290 public static function parseScalar($scalar, $flags = 0, $delimiters = null, $stringDelimiters = array('"', "'"), &$i = 0, $evaluate = true, $references = array())
|
Chris@0
|
291 {
|
Chris@0
|
292 if (in_array($scalar[$i], $stringDelimiters)) {
|
Chris@0
|
293 // quoted scalar
|
Chris@0
|
294 $output = self::parseQuotedScalar($scalar, $i);
|
Chris@0
|
295
|
Chris@0
|
296 if (null !== $delimiters) {
|
Chris@0
|
297 $tmp = ltrim(substr($scalar, $i), ' ');
|
Chris@0
|
298 if (!in_array($tmp[0], $delimiters)) {
|
Chris@0
|
299 throw new ParseException(sprintf('Unexpected characters (%s).', substr($scalar, $i)));
|
Chris@0
|
300 }
|
Chris@0
|
301 }
|
Chris@0
|
302 } else {
|
Chris@0
|
303 // "normal" string
|
Chris@0
|
304 if (!$delimiters) {
|
Chris@0
|
305 $output = substr($scalar, $i);
|
Chris@0
|
306 $i += strlen($output);
|
Chris@0
|
307
|
Chris@0
|
308 // remove comments
|
Chris@0
|
309 if (Parser::preg_match('/[ \t]+#/', $output, $match, PREG_OFFSET_CAPTURE)) {
|
Chris@0
|
310 $output = substr($output, 0, $match[0][1]);
|
Chris@0
|
311 }
|
Chris@0
|
312 } elseif (Parser::preg_match('/^(.+?)('.implode('|', $delimiters).')/', substr($scalar, $i), $match)) {
|
Chris@0
|
313 $output = $match[1];
|
Chris@0
|
314 $i += strlen($output);
|
Chris@0
|
315 } else {
|
Chris@0
|
316 throw new ParseException(sprintf('Malformed inline YAML string: %s.', $scalar));
|
Chris@0
|
317 }
|
Chris@0
|
318
|
Chris@0
|
319 // a non-quoted string cannot start with @ or ` (reserved) nor with a scalar indicator (| or >)
|
Chris@0
|
320 if ($output && ('@' === $output[0] || '`' === $output[0] || '|' === $output[0] || '>' === $output[0])) {
|
Chris@0
|
321 throw new ParseException(sprintf('The reserved indicator "%s" cannot start a plain scalar; you need to quote the scalar.', $output[0]));
|
Chris@0
|
322 }
|
Chris@0
|
323
|
Chris@0
|
324 if ($output && '%' === $output[0]) {
|
Chris@0
|
325 @trigger_error(sprintf('Not quoting the scalar "%s" starting with the "%%" indicator character is deprecated since Symfony 3.1 and will throw a ParseException in 4.0.', $output), E_USER_DEPRECATED);
|
Chris@0
|
326 }
|
Chris@0
|
327
|
Chris@0
|
328 if ($evaluate) {
|
Chris@0
|
329 $output = self::evaluateScalar($output, $flags, $references);
|
Chris@0
|
330 }
|
Chris@0
|
331 }
|
Chris@0
|
332
|
Chris@0
|
333 return $output;
|
Chris@0
|
334 }
|
Chris@0
|
335
|
Chris@0
|
336 /**
|
Chris@0
|
337 * Parses a YAML quoted scalar.
|
Chris@0
|
338 *
|
Chris@0
|
339 * @param string $scalar
|
Chris@0
|
340 * @param int &$i
|
Chris@0
|
341 *
|
Chris@0
|
342 * @return string
|
Chris@0
|
343 *
|
Chris@0
|
344 * @throws ParseException When malformed inline YAML string is parsed
|
Chris@0
|
345 */
|
Chris@0
|
346 private static function parseQuotedScalar($scalar, &$i)
|
Chris@0
|
347 {
|
Chris@0
|
348 if (!Parser::preg_match('/'.self::REGEX_QUOTED_STRING.'/Au', substr($scalar, $i), $match)) {
|
Chris@0
|
349 throw new ParseException(sprintf('Malformed inline YAML string: %s.', substr($scalar, $i)));
|
Chris@0
|
350 }
|
Chris@0
|
351
|
Chris@0
|
352 $output = substr($match[0], 1, strlen($match[0]) - 2);
|
Chris@0
|
353
|
Chris@0
|
354 $unescaper = new Unescaper();
|
Chris@0
|
355 if ('"' == $scalar[$i]) {
|
Chris@0
|
356 $output = $unescaper->unescapeDoubleQuotedString($output);
|
Chris@0
|
357 } else {
|
Chris@0
|
358 $output = $unescaper->unescapeSingleQuotedString($output);
|
Chris@0
|
359 }
|
Chris@0
|
360
|
Chris@0
|
361 $i += strlen($match[0]);
|
Chris@0
|
362
|
Chris@0
|
363 return $output;
|
Chris@0
|
364 }
|
Chris@0
|
365
|
Chris@0
|
366 /**
|
Chris@0
|
367 * Parses a YAML sequence.
|
Chris@0
|
368 *
|
Chris@0
|
369 * @param string $sequence
|
Chris@0
|
370 * @param int $flags
|
Chris@0
|
371 * @param int &$i
|
Chris@0
|
372 * @param array $references
|
Chris@0
|
373 *
|
Chris@0
|
374 * @return array
|
Chris@0
|
375 *
|
Chris@0
|
376 * @throws ParseException When malformed inline YAML string is parsed
|
Chris@0
|
377 */
|
Chris@0
|
378 private static function parseSequence($sequence, $flags, &$i = 0, $references = array())
|
Chris@0
|
379 {
|
Chris@0
|
380 $output = array();
|
Chris@0
|
381 $len = strlen($sequence);
|
Chris@0
|
382 ++$i;
|
Chris@0
|
383
|
Chris@0
|
384 // [foo, bar, ...]
|
Chris@0
|
385 while ($i < $len) {
|
Chris@0
|
386 switch ($sequence[$i]) {
|
Chris@0
|
387 case '[':
|
Chris@0
|
388 // nested sequence
|
Chris@0
|
389 $output[] = self::parseSequence($sequence, $flags, $i, $references);
|
Chris@0
|
390 break;
|
Chris@0
|
391 case '{':
|
Chris@0
|
392 // nested mapping
|
Chris@0
|
393 $output[] = self::parseMapping($sequence, $flags, $i, $references);
|
Chris@0
|
394 break;
|
Chris@0
|
395 case ']':
|
Chris@0
|
396 return $output;
|
Chris@0
|
397 case ',':
|
Chris@0
|
398 case ' ':
|
Chris@0
|
399 break;
|
Chris@0
|
400 default:
|
Chris@0
|
401 $isQuoted = in_array($sequence[$i], array('"', "'"));
|
Chris@0
|
402 $value = self::parseScalar($sequence, $flags, array(',', ']'), array('"', "'"), $i, true, $references);
|
Chris@0
|
403
|
Chris@0
|
404 // the value can be an array if a reference has been resolved to an array var
|
Chris@0
|
405 if (is_string($value) && !$isQuoted && false !== strpos($value, ': ')) {
|
Chris@0
|
406 // embedded mapping?
|
Chris@0
|
407 try {
|
Chris@0
|
408 $pos = 0;
|
Chris@0
|
409 $value = self::parseMapping('{'.$value.'}', $flags, $pos, $references);
|
Chris@0
|
410 } catch (\InvalidArgumentException $e) {
|
Chris@0
|
411 // no, it's not
|
Chris@0
|
412 }
|
Chris@0
|
413 }
|
Chris@0
|
414
|
Chris@0
|
415 $output[] = $value;
|
Chris@0
|
416
|
Chris@0
|
417 --$i;
|
Chris@0
|
418 }
|
Chris@0
|
419
|
Chris@0
|
420 ++$i;
|
Chris@0
|
421 }
|
Chris@0
|
422
|
Chris@0
|
423 throw new ParseException(sprintf('Malformed inline YAML string: %s.', $sequence));
|
Chris@0
|
424 }
|
Chris@0
|
425
|
Chris@0
|
426 /**
|
Chris@0
|
427 * Parses a YAML mapping.
|
Chris@0
|
428 *
|
Chris@0
|
429 * @param string $mapping
|
Chris@0
|
430 * @param int $flags
|
Chris@0
|
431 * @param int &$i
|
Chris@0
|
432 * @param array $references
|
Chris@0
|
433 *
|
Chris@0
|
434 * @return array|\stdClass
|
Chris@0
|
435 *
|
Chris@0
|
436 * @throws ParseException When malformed inline YAML string is parsed
|
Chris@0
|
437 */
|
Chris@0
|
438 private static function parseMapping($mapping, $flags, &$i = 0, $references = array())
|
Chris@0
|
439 {
|
Chris@0
|
440 $output = array();
|
Chris@0
|
441 $len = strlen($mapping);
|
Chris@0
|
442 ++$i;
|
Chris@0
|
443
|
Chris@0
|
444 // {foo: bar, bar:foo, ...}
|
Chris@0
|
445 while ($i < $len) {
|
Chris@0
|
446 switch ($mapping[$i]) {
|
Chris@0
|
447 case ' ':
|
Chris@0
|
448 case ',':
|
Chris@0
|
449 ++$i;
|
Chris@0
|
450 continue 2;
|
Chris@0
|
451 case '}':
|
Chris@0
|
452 if (self::$objectForMap) {
|
Chris@0
|
453 return (object) $output;
|
Chris@0
|
454 }
|
Chris@0
|
455
|
Chris@0
|
456 return $output;
|
Chris@0
|
457 }
|
Chris@0
|
458
|
Chris@0
|
459 // key
|
Chris@0
|
460 $key = self::parseScalar($mapping, $flags, array(':', ' '), array('"', "'"), $i, false);
|
Chris@0
|
461
|
Chris@0
|
462 if (':' !== $key && false === $i = strpos($mapping, ':', $i)) {
|
Chris@0
|
463 break;
|
Chris@0
|
464 }
|
Chris@0
|
465
|
Chris@0
|
466 if (':' !== $key && (!isset($mapping[$i + 1]) || !in_array($mapping[$i + 1], array(' ', ',', '[', ']', '{', '}'), true))) {
|
Chris@0
|
467 @trigger_error('Using a colon that is not followed by an indication character (i.e. " ", ",", "[", "]", "{", "}" is deprecated since version 3.2 and will throw a ParseException in 4.0.', E_USER_DEPRECATED);
|
Chris@0
|
468 }
|
Chris@0
|
469
|
Chris@0
|
470 // value
|
Chris@0
|
471 $done = false;
|
Chris@0
|
472
|
Chris@0
|
473 while ($i < $len) {
|
Chris@0
|
474 switch ($mapping[$i]) {
|
Chris@0
|
475 case '[':
|
Chris@0
|
476 // nested sequence
|
Chris@0
|
477 $value = self::parseSequence($mapping, $flags, $i, $references);
|
Chris@0
|
478 // Spec: Keys MUST be unique; first one wins.
|
Chris@0
|
479 // Parser cannot abort this mapping earlier, since lines
|
Chris@0
|
480 // are processed sequentially.
|
Chris@0
|
481 if (!isset($output[$key])) {
|
Chris@0
|
482 $output[$key] = $value;
|
Chris@0
|
483 } else {
|
Chris@0
|
484 @trigger_error(sprintf('Duplicate key "%s" detected on line %d whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $key, self::$parsedLineNumber + 1), E_USER_DEPRECATED);
|
Chris@0
|
485 }
|
Chris@0
|
486 $done = true;
|
Chris@0
|
487 break;
|
Chris@0
|
488 case '{':
|
Chris@0
|
489 // nested mapping
|
Chris@0
|
490 $value = self::parseMapping($mapping, $flags, $i, $references);
|
Chris@0
|
491 // Spec: Keys MUST be unique; first one wins.
|
Chris@0
|
492 // Parser cannot abort this mapping earlier, since lines
|
Chris@0
|
493 // are processed sequentially.
|
Chris@0
|
494 if (!isset($output[$key])) {
|
Chris@0
|
495 $output[$key] = $value;
|
Chris@0
|
496 } else {
|
Chris@0
|
497 @trigger_error(sprintf('Duplicate key "%s" detected on line %d whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $key, self::$parsedLineNumber + 1), E_USER_DEPRECATED);
|
Chris@0
|
498 }
|
Chris@0
|
499 $done = true;
|
Chris@0
|
500 break;
|
Chris@0
|
501 case ':':
|
Chris@0
|
502 case ' ':
|
Chris@0
|
503 break;
|
Chris@0
|
504 default:
|
Chris@0
|
505 $value = self::parseScalar($mapping, $flags, array(',', '}'), array('"', "'"), $i, true, $references);
|
Chris@0
|
506 // Spec: Keys MUST be unique; first one wins.
|
Chris@0
|
507 // Parser cannot abort this mapping earlier, since lines
|
Chris@0
|
508 // are processed sequentially.
|
Chris@0
|
509 if (!isset($output[$key])) {
|
Chris@0
|
510 $output[$key] = $value;
|
Chris@0
|
511 } else {
|
Chris@0
|
512 @trigger_error(sprintf('Duplicate key "%s" detected on line %d whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $key, self::$parsedLineNumber + 1), E_USER_DEPRECATED);
|
Chris@0
|
513 }
|
Chris@0
|
514 $done = true;
|
Chris@0
|
515 --$i;
|
Chris@0
|
516 }
|
Chris@0
|
517
|
Chris@0
|
518 ++$i;
|
Chris@0
|
519
|
Chris@0
|
520 if ($done) {
|
Chris@0
|
521 continue 2;
|
Chris@0
|
522 }
|
Chris@0
|
523 }
|
Chris@0
|
524 }
|
Chris@0
|
525
|
Chris@0
|
526 throw new ParseException(sprintf('Malformed inline YAML string: %s.', $mapping));
|
Chris@0
|
527 }
|
Chris@0
|
528
|
Chris@0
|
529 /**
|
Chris@0
|
530 * Evaluates scalars and replaces magic values.
|
Chris@0
|
531 *
|
Chris@0
|
532 * @param string $scalar
|
Chris@0
|
533 * @param int $flags
|
Chris@0
|
534 * @param array $references
|
Chris@0
|
535 *
|
Chris@0
|
536 * @return mixed The evaluated YAML string
|
Chris@0
|
537 *
|
Chris@0
|
538 * @throws ParseException when object parsing support was disabled and the parser detected a PHP object or when a reference could not be resolved
|
Chris@0
|
539 */
|
Chris@0
|
540 private static function evaluateScalar($scalar, $flags, $references = array())
|
Chris@0
|
541 {
|
Chris@0
|
542 $scalar = trim($scalar);
|
Chris@0
|
543 $scalarLower = strtolower($scalar);
|
Chris@0
|
544
|
Chris@0
|
545 if (0 === strpos($scalar, '*')) {
|
Chris@0
|
546 if (false !== $pos = strpos($scalar, '#')) {
|
Chris@0
|
547 $value = substr($scalar, 1, $pos - 2);
|
Chris@0
|
548 } else {
|
Chris@0
|
549 $value = substr($scalar, 1);
|
Chris@0
|
550 }
|
Chris@0
|
551
|
Chris@0
|
552 // an unquoted *
|
Chris@0
|
553 if (false === $value || '' === $value) {
|
Chris@0
|
554 throw new ParseException('A reference must contain at least one character.');
|
Chris@0
|
555 }
|
Chris@0
|
556
|
Chris@0
|
557 if (!array_key_exists($value, $references)) {
|
Chris@0
|
558 throw new ParseException(sprintf('Reference "%s" does not exist.', $value));
|
Chris@0
|
559 }
|
Chris@0
|
560
|
Chris@0
|
561 return $references[$value];
|
Chris@0
|
562 }
|
Chris@0
|
563
|
Chris@0
|
564 switch (true) {
|
Chris@0
|
565 case 'null' === $scalarLower:
|
Chris@0
|
566 case '' === $scalar:
|
Chris@0
|
567 case '~' === $scalar:
|
Chris@0
|
568 return;
|
Chris@0
|
569 case 'true' === $scalarLower:
|
Chris@0
|
570 return true;
|
Chris@0
|
571 case 'false' === $scalarLower:
|
Chris@0
|
572 return false;
|
Chris@0
|
573 // Optimise for returning strings.
|
Chris@0
|
574 case $scalar[0] === '+' || $scalar[0] === '-' || $scalar[0] === '.' || $scalar[0] === '!' || is_numeric($scalar[0]):
|
Chris@0
|
575 switch (true) {
|
Chris@0
|
576 case 0 === strpos($scalar, '!str'):
|
Chris@0
|
577 return (string) substr($scalar, 5);
|
Chris@0
|
578 case 0 === strpos($scalar, '! '):
|
Chris@0
|
579 return (int) self::parseScalar(substr($scalar, 2), $flags);
|
Chris@0
|
580 case 0 === strpos($scalar, '!php/object:'):
|
Chris@0
|
581 if (self::$objectSupport) {
|
Chris@0
|
582 return unserialize(substr($scalar, 12));
|
Chris@0
|
583 }
|
Chris@0
|
584
|
Chris@0
|
585 if (self::$exceptionOnInvalidType) {
|
Chris@0
|
586 throw new ParseException('Object support when parsing a YAML file has been disabled.');
|
Chris@0
|
587 }
|
Chris@0
|
588
|
Chris@0
|
589 return;
|
Chris@0
|
590 case 0 === strpos($scalar, '!!php/object:'):
|
Chris@0
|
591 if (self::$objectSupport) {
|
Chris@0
|
592 @trigger_error('The !!php/object tag to indicate dumped PHP objects is deprecated since version 3.1 and will be removed in 4.0. Use the !php/object tag instead.', E_USER_DEPRECATED);
|
Chris@0
|
593
|
Chris@0
|
594 return unserialize(substr($scalar, 13));
|
Chris@0
|
595 }
|
Chris@0
|
596
|
Chris@0
|
597 if (self::$exceptionOnInvalidType) {
|
Chris@0
|
598 throw new ParseException('Object support when parsing a YAML file has been disabled.');
|
Chris@0
|
599 }
|
Chris@0
|
600
|
Chris@0
|
601 return;
|
Chris@0
|
602 case 0 === strpos($scalar, '!php/const:'):
|
Chris@0
|
603 if (self::$constantSupport) {
|
Chris@0
|
604 if (defined($const = substr($scalar, 11))) {
|
Chris@0
|
605 return constant($const);
|
Chris@0
|
606 }
|
Chris@0
|
607
|
Chris@0
|
608 throw new ParseException(sprintf('The constant "%s" is not defined.', $const));
|
Chris@0
|
609 }
|
Chris@0
|
610 if (self::$exceptionOnInvalidType) {
|
Chris@0
|
611 throw new ParseException(sprintf('The string "%s" could not be parsed as a constant. Have you forgotten to pass the "Yaml::PARSE_CONSTANT" flag to the parser?', $scalar));
|
Chris@0
|
612 }
|
Chris@0
|
613
|
Chris@0
|
614 return;
|
Chris@0
|
615 case 0 === strpos($scalar, '!!float '):
|
Chris@0
|
616 return (float) substr($scalar, 8);
|
Chris@0
|
617 case Parser::preg_match('{^[+-]?[0-9][0-9_]*$}', $scalar):
|
Chris@0
|
618 $scalar = str_replace('_', '', (string) $scalar);
|
Chris@0
|
619 // omitting the break / return as integers are handled in the next case
|
Chris@0
|
620 case ctype_digit($scalar):
|
Chris@0
|
621 $raw = $scalar;
|
Chris@0
|
622 $cast = (int) $scalar;
|
Chris@0
|
623
|
Chris@0
|
624 return '0' == $scalar[0] ? octdec($scalar) : (((string) $raw == (string) $cast) ? $cast : $raw);
|
Chris@0
|
625 case '-' === $scalar[0] && ctype_digit(substr($scalar, 1)):
|
Chris@0
|
626 $raw = $scalar;
|
Chris@0
|
627 $cast = (int) $scalar;
|
Chris@0
|
628
|
Chris@0
|
629 return '0' == $scalar[1] ? octdec($scalar) : (((string) $raw === (string) $cast) ? $cast : $raw);
|
Chris@0
|
630 case is_numeric($scalar):
|
Chris@0
|
631 case Parser::preg_match(self::getHexRegex(), $scalar):
|
Chris@0
|
632 $scalar = str_replace('_', '', $scalar);
|
Chris@0
|
633
|
Chris@0
|
634 return '0x' === $scalar[0].$scalar[1] ? hexdec($scalar) : (float) $scalar;
|
Chris@0
|
635 case '.inf' === $scalarLower:
|
Chris@0
|
636 case '.nan' === $scalarLower:
|
Chris@0
|
637 return -log(0);
|
Chris@0
|
638 case '-.inf' === $scalarLower:
|
Chris@0
|
639 return log(0);
|
Chris@0
|
640 case 0 === strpos($scalar, '!!binary '):
|
Chris@0
|
641 return self::evaluateBinaryScalar(substr($scalar, 9));
|
Chris@0
|
642 case Parser::preg_match('/^(-|\+)?[0-9][0-9,]*(\.[0-9_]+)?$/', $scalar):
|
Chris@0
|
643 case Parser::preg_match('/^(-|\+)?[0-9][0-9_]*(\.[0-9_]+)?$/', $scalar):
|
Chris@0
|
644 if (false !== strpos($scalar, ',')) {
|
Chris@0
|
645 @trigger_error('Using the comma as a group separator for floats is deprecated since version 3.2 and will be removed in 4.0.', E_USER_DEPRECATED);
|
Chris@0
|
646 }
|
Chris@0
|
647
|
Chris@0
|
648 return (float) str_replace(array(',', '_'), '', $scalar);
|
Chris@0
|
649 case Parser::preg_match(self::getTimestampRegex(), $scalar):
|
Chris@0
|
650 if (Yaml::PARSE_DATETIME & $flags) {
|
Chris@0
|
651 // When no timezone is provided in the parsed date, YAML spec says we must assume UTC.
|
Chris@0
|
652 return new \DateTime($scalar, new \DateTimeZone('UTC'));
|
Chris@0
|
653 }
|
Chris@0
|
654
|
Chris@0
|
655 $timeZone = date_default_timezone_get();
|
Chris@0
|
656 date_default_timezone_set('UTC');
|
Chris@0
|
657 $time = strtotime($scalar);
|
Chris@0
|
658 date_default_timezone_set($timeZone);
|
Chris@0
|
659
|
Chris@0
|
660 return $time;
|
Chris@0
|
661 }
|
Chris@0
|
662 default:
|
Chris@0
|
663 return (string) $scalar;
|
Chris@0
|
664 }
|
Chris@0
|
665 }
|
Chris@0
|
666
|
Chris@0
|
667 /**
|
Chris@0
|
668 * @param string $scalar
|
Chris@0
|
669 *
|
Chris@0
|
670 * @return string
|
Chris@0
|
671 *
|
Chris@0
|
672 * @internal
|
Chris@0
|
673 */
|
Chris@0
|
674 public static function evaluateBinaryScalar($scalar)
|
Chris@0
|
675 {
|
Chris@0
|
676 $parsedBinaryData = self::parseScalar(preg_replace('/\s/', '', $scalar));
|
Chris@0
|
677
|
Chris@0
|
678 if (0 !== (strlen($parsedBinaryData) % 4)) {
|
Chris@0
|
679 throw new ParseException(sprintf('The normalized base64 encoded data (data without whitespace characters) length must be a multiple of four (%d bytes given).', strlen($parsedBinaryData)));
|
Chris@0
|
680 }
|
Chris@0
|
681
|
Chris@0
|
682 if (!Parser::preg_match('#^[A-Z0-9+/]+={0,2}$#i', $parsedBinaryData)) {
|
Chris@0
|
683 throw new ParseException(sprintf('The base64 encoded data (%s) contains invalid characters.', $parsedBinaryData));
|
Chris@0
|
684 }
|
Chris@0
|
685
|
Chris@0
|
686 return base64_decode($parsedBinaryData, true);
|
Chris@0
|
687 }
|
Chris@0
|
688
|
Chris@0
|
689 private static function isBinaryString($value)
|
Chris@0
|
690 {
|
Chris@0
|
691 return !preg_match('//u', $value) || preg_match('/[^\x00\x07-\x0d\x1B\x20-\xff]/', $value);
|
Chris@0
|
692 }
|
Chris@0
|
693
|
Chris@0
|
694 /**
|
Chris@0
|
695 * Gets a regex that matches a YAML date.
|
Chris@0
|
696 *
|
Chris@0
|
697 * @return string The regular expression
|
Chris@0
|
698 *
|
Chris@0
|
699 * @see http://www.yaml.org/spec/1.2/spec.html#id2761573
|
Chris@0
|
700 */
|
Chris@0
|
701 private static function getTimestampRegex()
|
Chris@0
|
702 {
|
Chris@0
|
703 return <<<EOF
|
Chris@0
|
704 ~^
|
Chris@0
|
705 (?P<year>[0-9][0-9][0-9][0-9])
|
Chris@0
|
706 -(?P<month>[0-9][0-9]?)
|
Chris@0
|
707 -(?P<day>[0-9][0-9]?)
|
Chris@0
|
708 (?:(?:[Tt]|[ \t]+)
|
Chris@0
|
709 (?P<hour>[0-9][0-9]?)
|
Chris@0
|
710 :(?P<minute>[0-9][0-9])
|
Chris@0
|
711 :(?P<second>[0-9][0-9])
|
Chris@0
|
712 (?:\.(?P<fraction>[0-9]*))?
|
Chris@0
|
713 (?:[ \t]*(?P<tz>Z|(?P<tz_sign>[-+])(?P<tz_hour>[0-9][0-9]?)
|
Chris@0
|
714 (?::(?P<tz_minute>[0-9][0-9]))?))?)?
|
Chris@0
|
715 $~x
|
Chris@0
|
716 EOF;
|
Chris@0
|
717 }
|
Chris@0
|
718
|
Chris@0
|
719 /**
|
Chris@0
|
720 * Gets a regex that matches a YAML number in hexadecimal notation.
|
Chris@0
|
721 *
|
Chris@0
|
722 * @return string
|
Chris@0
|
723 */
|
Chris@0
|
724 private static function getHexRegex()
|
Chris@0
|
725 {
|
Chris@0
|
726 return '~^0x[0-9a-f_]++$~i';
|
Chris@0
|
727 }
|
Chris@0
|
728 }
|