Verzeichnisstruktur phpBB-3.1.0
- Veröffentlicht
- 27.10.2014
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
Inline.php
001 <?php
002
003 /*
004 * This file is part of the Symfony package.
005 *
006 * (c) Fabien Potencier <fabien@symfony.com>
007 *
008 * For the full copyright and license information, please view the LICENSE
009 * file that was distributed with this source code.
010 */
011
012 namespace Symfony\Component\Yaml;
013
014 use Symfony\Component\Yaml\Exception\ParseException;
015 use Symfony\Component\Yaml\Exception\DumpException;
016
017 /**
018 * Inline implements a YAML parser/dumper for the YAML inline syntax.
019 *
020 * @author Fabien Potencier <fabien@symfony.com>
021 */
022 class Inline
023 {
024 const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\']*(?:\'\'[^\']*)*)\')';
025
026 private static $exceptionOnInvalidType = false;
027 private static $objectSupport = false;
028
029 /**
030 * Converts a YAML string to a PHP array.
031 *
032 * @param string $value A YAML string
033 * @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
034 * @param bool $objectSupport true if object support is enabled, false otherwise
035 * @param array $references Mapping of variable names to values
036 *
037 * @return array A PHP array representing the YAML string
038 *
039 * @throws ParseException
040 */
041 public static function parse($value, $exceptionOnInvalidType = false, $objectSupport = false, $references = array())
042 {
043 self::$exceptionOnInvalidType = $exceptionOnInvalidType;
044 self::$objectSupport = $objectSupport;
045
046 $value = trim($value);
047
048 if (0 == strlen($value)) {
049 return '';
050 }
051
052 if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) {
053 $mbEncoding = mb_internal_encoding();
054 mb_internal_encoding('ASCII');
055 }
056
057 $i = 0;
058 switch ($value[0]) {
059 case '[':
060 $result = self::parseSequence($value, $i, $references);
061 ++$i;
062 break;
063 case '{':
064 $result = self::parseMapping($value, $i, $references);
065 ++$i;
066 break;
067 default:
068 $result = self::parseScalar($value, null, array('"', "'"), $i, true, $references);
069 }
070
071 // some comments are allowed at the end
072 if (preg_replace('/\s+#.*$/A', '', substr($value, $i))) {
073 throw new ParseException(sprintf('Unexpected characters near "%s".', substr($value, $i)));
074 }
075
076 if (isset($mbEncoding)) {
077 mb_internal_encoding($mbEncoding);
078 }
079
080 return $result;
081 }
082
083 /**
084 * Dumps a given PHP variable to a YAML string.
085 *
086 * @param mixed $value The PHP variable to convert
087 * @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
088 * @param bool $objectSupport true if object support is enabled, false otherwise
089 *
090 * @return string The YAML string representing the PHP array
091 *
092 * @throws DumpException When trying to dump PHP resource
093 */
094 public static function dump($value, $exceptionOnInvalidType = false, $objectSupport = false)
095 {
096 switch (true) {
097 case is_resource($value):
098 if ($exceptionOnInvalidType) {
099 throw new DumpException(sprintf('Unable to dump PHP resources in a YAML file ("%s").', get_resource_type($value)));
100 }
101
102 return 'null';
103 case is_object($value):
104 if ($objectSupport) {
105 return '!!php/object:'.serialize($value);
106 }
107
108 if ($exceptionOnInvalidType) {
109 throw new DumpException('Object support when dumping a YAML file has been disabled.');
110 }
111
112 return 'null';
113 case is_array($value):
114 return self::dumpArray($value, $exceptionOnInvalidType, $objectSupport);
115 case null === $value:
116 return 'null';
117 case true === $value:
118 return 'true';
119 case false === $value:
120 return 'false';
121 case ctype_digit($value):
122 return is_string($value) ? "'$value'" : (int) $value;
123 case is_numeric($value):
124 $locale = setlocale(LC_NUMERIC, 0);
125 if (false !== $locale) {
126 setlocale(LC_NUMERIC, 'C');
127 }
128 $repr = is_string($value) ? "'$value'" : (is_infinite($value) ? str_ireplace('INF', '.Inf', strval($value)) : strval($value));
129
130 if (false !== $locale) {
131 setlocale(LC_NUMERIC, $locale);
132 }
133
134 return $repr;
135 case Escaper::requiresDoubleQuoting($value):
136 return Escaper::escapeWithDoubleQuotes($value);
137 case Escaper::requiresSingleQuoting($value):
138 return Escaper::escapeWithSingleQuotes($value);
139 case '' == $value:
140 return "''";
141 case preg_match(self::getTimestampRegex(), $value):
142 case in_array(strtolower($value), array('null', '~', 'true', 'false')):
143 return "'$value'";
144 default:
145 return $value;
146 }
147 }
148
149 /**
150 * Dumps a PHP array to a YAML string.
151 *
152 * @param array $value The PHP array to dump
153 * @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
154 * @param bool $objectSupport true if object support is enabled, false otherwise
155 *
156 * @return string The YAML string representing the PHP array
157 */
158 private static function dumpArray($value, $exceptionOnInvalidType, $objectSupport)
159 {
160 // array
161 $keys = array_keys($value);
162 if ((1 == count($keys) && '0' == $keys[0])
163 || (count($keys) > 1 && array_reduce($keys, function ($v, $w) { return (int) $v + $w; }, 0) == count($keys) * (count($keys) - 1) / 2)
164 ) {
165 $output = array();
166 foreach ($value as $val) {
167 $output[] = self::dump($val, $exceptionOnInvalidType, $objectSupport);
168 }
169
170 return sprintf('[%s]', implode(', ', $output));
171 }
172
173 // mapping
174 $output = array();
175 foreach ($value as $key => $val) {
176 $output[] = sprintf('%s: %s', self::dump($key, $exceptionOnInvalidType, $objectSupport), self::dump($val, $exceptionOnInvalidType, $objectSupport));
177 }
178
179 return sprintf('{ %s }', implode(', ', $output));
180 }
181
182 /**
183 * Parses a scalar to a YAML string.
184 *
185 * @param scalar $scalar
186 * @param string $delimiters
187 * @param array $stringDelimiters
188 * @param int &$i
189 * @param bool $evaluate
190 * @param array $references
191 *
192 * @return string A YAML string
193 *
194 * @throws ParseException When malformed inline YAML string is parsed
195 */
196 public static function parseScalar($scalar, $delimiters = null, $stringDelimiters = array('"', "'"), &$i = 0, $evaluate = true, $references = array())
197 {
198 if (in_array($scalar[$i], $stringDelimiters)) {
199 // quoted scalar
200 $output = self::parseQuotedScalar($scalar, $i);
201
202 if (null !== $delimiters) {
203 $tmp = ltrim(substr($scalar, $i), ' ');
204 if (!in_array($tmp[0], $delimiters)) {
205 throw new ParseException(sprintf('Unexpected characters (%s).', substr($scalar, $i)));
206 }
207 }
208 } else {
209 // "normal" string
210 if (!$delimiters) {
211 $output = substr($scalar, $i);
212 $i += strlen($output);
213
214 // remove comments
215 if (false !== $strpos = strpos($output, ' #')) {
216 $output = rtrim(substr($output, 0, $strpos));
217 }
218 } elseif (preg_match('/^(.+?)('.implode('|', $delimiters).')/', substr($scalar, $i), $match)) {
219 $output = $match[1];
220 $i += strlen($output);
221 } else {
222 throw new ParseException(sprintf('Malformed inline YAML string (%s).', $scalar));
223 }
224
225 if ($evaluate) {
226 $output = self::evaluateScalar($output, $references);
227 }
228 }
229
230 return $output;
231 }
232
233 /**
234 * Parses a quoted scalar to YAML.
235 *
236 * @param string $scalar
237 * @param int &$i
238 *
239 * @return string A YAML string
240 *
241 * @throws ParseException When malformed inline YAML string is parsed
242 */
243 private static function parseQuotedScalar($scalar, &$i)
244 {
245 if (!preg_match('/'.self::REGEX_QUOTED_STRING.'/Au', substr($scalar, $i), $match)) {
246 throw new ParseException(sprintf('Malformed inline YAML string (%s).', substr($scalar, $i)));
247 }
248
249 $output = substr($match[0], 1, strlen($match[0]) - 2);
250
251 $unescaper = new Unescaper();
252 if ('"' == $scalar[$i]) {
253 $output = $unescaper->unescapeDoubleQuotedString($output);
254 } else {
255 $output = $unescaper->unescapeSingleQuotedString($output);
256 }
257
258 $i += strlen($match[0]);
259
260 return $output;
261 }
262
263 /**
264 * Parses a sequence to a YAML string.
265 *
266 * @param string $sequence
267 * @param int &$i
268 * @param array $references
269 *
270 * @return string A YAML string
271 *
272 * @throws ParseException When malformed inline YAML string is parsed
273 */
274 private static function parseSequence($sequence, &$i = 0, $references = array())
275 {
276 $output = array();
277 $len = strlen($sequence);
278 $i += 1;
279
280 // [foo, bar, ...]
281 while ($i < $len) {
282 switch ($sequence[$i]) {
283 case '[':
284 // nested sequence
285 $output[] = self::parseSequence($sequence, $i, $references);
286 break;
287 case '{':
288 // nested mapping
289 $output[] = self::parseMapping($sequence, $i, $references);
290 break;
291 case ']':
292 return $output;
293 case ',':
294 case ' ':
295 break;
296 default:
297 $isQuoted = in_array($sequence[$i], array('"', "'"));
298 $value = self::parseScalar($sequence, array(',', ']'), array('"', "'"), $i, true, $references);
299
300 // the value can be an array if a reference has been resolved to an array var
301 if (!is_array($value) && !$isQuoted && false !== strpos($value, ': ')) {
302 // embedded mapping?
303 try {
304 $pos = 0;
305 $value = self::parseMapping('{'.$value.'}', $pos, $references);
306 } catch (\InvalidArgumentException $e) {
307 // no, it's not
308 }
309 }
310
311 $output[] = $value;
312
313 --$i;
314 }
315
316 ++$i;
317 }
318
319 throw new ParseException(sprintf('Malformed inline YAML string %s', $sequence));
320 }
321
322 /**
323 * Parses a mapping to a YAML string.
324 *
325 * @param string $mapping
326 * @param int &$i
327 * @param array $references
328 *
329 * @return string A YAML string
330 *
331 * @throws ParseException When malformed inline YAML string is parsed
332 */
333 private static function parseMapping($mapping, &$i = 0, $references = array())
334 {
335 $output = array();
336 $len = strlen($mapping);
337 $i += 1;
338
339 // {foo: bar, bar:foo, ...}
340 while ($i < $len) {
341 switch ($mapping[$i]) {
342 case ' ':
343 case ',':
344 ++$i;
345 continue 2;
346 case '}':
347 return $output;
348 }
349
350 // key
351 $key = self::parseScalar($mapping, array(':', ' '), array('"', "'"), $i, false);
352
353 // value
354 $done = false;
355 while ($i < $len) {
356 switch ($mapping[$i]) {
357 case '[':
358 // nested sequence
359 $output[$key] = self::parseSequence($mapping, $i, $references);
360 $done = true;
361 break;
362 case '{':
363 // nested mapping
364 $output[$key] = self::parseMapping($mapping, $i, $references);
365 $done = true;
366 break;
367 case ':':
368 case ' ':
369 break;
370 default:
371 $output[$key] = self::parseScalar($mapping, array(',', '}'), array('"', "'"), $i, true, $references);
372 $done = true;
373 --$i;
374 }
375
376 ++$i;
377
378 if ($done) {
379 continue 2;
380 }
381 }
382 }
383
384 throw new ParseException(sprintf('Malformed inline YAML string %s', $mapping));
385 }
386
387 /**
388 * Evaluates scalars and replaces magic values.
389 *
390 * @param string $scalar
391 * @param array $references
392 *
393 * @return string A YAML string
394 *
395 * @throws ParseException when object parsing support was disabled and the parser detected a PHP object or when a reference could not be resolved
396 */
397 private static function evaluateScalar($scalar, $references = array())
398 {
399 $scalar = trim($scalar);
400 $scalarLower = strtolower($scalar);
401
402 if (0 === strpos($scalar, '*')) {
403 if (false !== $pos = strpos($scalar, '#')) {
404 $value = substr($scalar, 1, $pos - 2);
405 } else {
406 $value = substr($scalar, 1);
407 }
408
409 // an unquoted *
410 if (false === $value || '' === $value) {
411 throw new ParseException('A reference must contain at least one character.');
412 }
413
414 if (!array_key_exists($value, $references)) {
415 throw new ParseException(sprintf('Reference "%s" does not exist.', $value));
416 }
417
418 return $references[$value];
419 }
420
421 switch (true) {
422 case 'null' === $scalarLower:
423 case '' === $scalar:
424 case '~' === $scalar:
425 return;
426 case 'true' === $scalarLower:
427 return true;
428 case 'false' === $scalarLower:
429 return false;
430 // Optimise for returning strings.
431 case $scalar[0] === '+' || $scalar[0] === '-' || $scalar[0] === '.' || $scalar[0] === '!' || is_numeric($scalar[0]):
432 switch (true) {
433 case 0 === strpos($scalar, '!str'):
434 return (string) substr($scalar, 5);
435 case 0 === strpos($scalar, '! '):
436 return intval(self::parseScalar(substr($scalar, 2)));
437 case 0 === strpos($scalar, '!!php/object:'):
438 if (self::$objectSupport) {
439 return unserialize(substr($scalar, 13));
440 }
441
442 if (self::$exceptionOnInvalidType) {
443 throw new ParseException('Object support when parsing a YAML file has been disabled.');
444 }
445
446 return;
447 case ctype_digit($scalar):
448 $raw = $scalar;
449 $cast = intval($scalar);
450
451 return '0' == $scalar[0] ? octdec($scalar) : (((string) $raw == (string) $cast) ? $cast : $raw);
452 case '-' === $scalar[0] && ctype_digit(substr($scalar, 1)):
453 $raw = $scalar;
454 $cast = intval($scalar);
455
456 return '0' == $scalar[1] ? octdec($scalar) : (((string) $raw == (string) $cast) ? $cast : $raw);
457 case is_numeric($scalar):
458 return '0x' == $scalar[0].$scalar[1] ? hexdec($scalar) : floatval($scalar);
459 case '.inf' === $scalarLower:
460 case '.nan' === $scalarLower:
461 return -log(0);
462 case '-.inf' === $scalarLower:
463 return log(0);
464 case preg_match('/^(-|\+)?[0-9,]+(\.[0-9]+)?$/', $scalar):
465 return floatval(str_replace(',', '', $scalar));
466 case preg_match(self::getTimestampRegex(), $scalar):
467 return strtotime($scalar);
468 }
469 default:
470 return (string) $scalar;
471 }
472 }
473
474 /**
475 * Gets a regex that matches a YAML date.
476 *
477 * @return string The regular expression
478 *
479 * @see http://www.yaml.org/spec/1.2/spec.html#id2761573
480 */
481 private static function getTimestampRegex()
482 {
483 return <<<EOF
484 ~^
485 (?P<year>[0-9][0-9][0-9][0-9])
486 -(?P<month>[0-9][0-9]?)
487 -(?P<day>[0-9][0-9]?)
488 (?:(?:[Tt]|[ \t]+)
489 (?P<hour>[0-9][0-9]?)
490 :(?P<minute>[0-9][0-9])
491 :(?P<second>[0-9][0-9])
492 (?:\.(?P<fraction>[0-9]*))?
493 (?:[ \t]*(?P<tz>Z|(?P<tz_sign>[-+])(?P<tz_hour>[0-9][0-9]?)
494 (?::(?P<tz_minute>[0-9][0-9]))?))?)?
495 $~x
496 EOF;
497
498 }
499 }
500