8000 feature #24253 [Yaml] support parsing files (xabbuh) · symfony/symfony@1b4d2b2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1b4d2b2

Browse files
committed
feature #24253 [Yaml] support parsing files (xabbuh)
This PR was merged into the 3.4 branch. Discussion ---------- [Yaml] support parsing files | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #24191 (comment) | License | MIT | Doc PR | TODO This PR adds a new flag `PARSE_FILE` which can be passed to the YAML parser. When given, the input will be interpreted as a filename whose contents will then be parsed. We already supported passing filenames in the past. Back then the features was not deterministic as it just relied on the string being an existing file or not. Now that we are able to control the behaviour of the parser by passing flags this can be done in a much cleaner way and allows to properly deal with errors (i.e. non existent or unreadable files). This change will also allow to improve error/deprecation messages to include the filename being parsed and thus showing more descriptive error messages when people are using the YAML parser with a bunch of files (e.g. as part of the DependencyInjection or Routing component). Commits ------- 9becb8a [Yaml] support parsing files
2 parents 8136fa5 + 9becb8a commit 1b4d2b2

File tree

7 files changed

+177
-50
lines changed

7 files changed

+177
-50
lines changed

src/Symfony/Component/Yaml/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ CHANGELOG
44
3.4.0
55
-----
66

7+
* added support for parsing YAML files using the `Yaml::parseFile()` or `Parser::parseFile()` method
8+
79
* the `Dumper`, `Parser`, and `Yaml` classes are marked as final
810

911
* Deprecated the `!php/object:` tag which will be replaced by the

src/Symfony/Component/Yaml/Inline.php

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,27 @@ class Inline
2626
{
2727
const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*+(?:\\\\.[^"\\\\]*+)*+)"|\'([^\']*+(?:\'\'[^\']*+)*+)\')';
2828

29-
public static $parsedLineNumber;
29+
public static $parsedLineNumber = -1;
30+
public static $parsedFilename;
3031

3132
private static $exceptionOnInvalidType = false;
3233
private static $objectSupport = false;
3334
private static $objectForMap = false;
3435
private static $constantSupport = false;
3536

3637
/**
37-
* @param int $flags
38-
* @param int|null $parsedLineNumber
38+
* @param int $flags
39+
* @param int|null $parsedLineNumber
40+
* @param string|null $parsedFilename
3941
*/
40-
public static function initialize($flags, $parsedLineNumber = null)
42+
public static function initialize($flags, $parsedLineNumber = null, $parsedFilename = null)
4143
{
4244
self::$exceptionOnInvalidType = (bool) (Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE & $flags);
4345
self::$objectSupport = (bool) (Yaml::PARSE_OBJECT & $flags);
4446
self::$objectForMap = (bool) (Yaml::PARSE_OBJECT_FOR_MAP & $flags);
4547
self::$constantSupport = (bool) (Yaml::PARSE_CONSTANT & $flags);
46-
47-
if (null !== $parsedLineNumber) {
48-
self::$parsedLineNumber = $parsedLineNumber;
49-
}
48+
self::$parsedFilename = $parsedFilename;
49+
self::$parsedLineNumber = null !== $parsedLineNumber ? $parsedLineNumber : -1;
5050
}
5151

5252
/**
@@ -128,7 +128,7 @@ public static function parse($value, $flags = 0, $references = array())
128128

129129
// some comments are allowed at the end
130130
if (preg_replace('/\s+#.*$/A', '', substr($value, $i))) {
131-
throw new ParseException(sprintf('Unexpected characters near "%s".', substr($value, $i)));
131+
throw new ParseException(sprintf('Unexpected characters near "%s".', substr($value, $i)), self::$parsedLineNumber, $value, self::$parsedFilename);
132132
}
133133

134134
if (isset($mbEncoding)) {
@@ -322,7 +322,7 @@ public static function parseScalar($scalar, $flags = 0, $delimiters = null, &$i
322322
if (null !== $delimiters) {
323323
$tmp = ltrim(substr($scalar, $i), ' ');
324324
if (!in_array($tmp[0], $delimiters)) {
325-
throw new ParseException(sprintf('Unexpected characters (%s).', substr($scalar, $i)));
325+
throw new ParseException(sprintf('Unexpected characters (%s).', substr($scalar, $i)), self::$parsedLineNumber, $scalar, self::$parsedFilename);
326326
}
327327
}
328328
} else {
@@ -339,12 +339,12 @@ public static function parseScalar($scalar, $flags = 0, $delimiters = null, &$i
339339
$output = $match[1];
340340
$i += strlen($output);
341341
} else {
342-
throw new ParseException(sprintf('Malformed inline YAML string: %s.', $scalar));
342+
throw new ParseException(sprintf('Malformed inline YAML string: %s.', $scalar), self::$parsedLineNumber, null, self::$parsedFilename);
343343
}
344344

345345
// a non-quoted string cannot start with @ or ` (reserved) nor with a scalar indicator (| or >)
346346
if ($output && ('@' === $output[0] || '`' === $output[0] || '|' === $output[0] || '>' === $output[0])) {
347-
throw new ParseException(sprintf('The reserved indicator "%s" cannot start a plain scalar; you need to quote the scalar.', $output[0]));
347+
throw new ParseException(sprintf('The reserved indicator "%s" cannot start a plain scalar; you need to quote the scalar.', $output[0]), self::$parsedLineNumber, $output, self::$parsedFilename);
348348
}
349349

350350
if ($output && '%' === $output[0]) {
@@ -372,7 +372,7 @@ public static function parseScalar($scalar, $flags = 0, $delimiters = null, &$i
372372
private static function parseQuotedScalar($scalar, &$i)
373373
{
374374
if (!Parser::preg_match('/'.self::REGEX_QUOTED_STRING.'/Au', substr($scalar, $i), $match)) {
375-
throw new ParseException(sprintf('Malformed inline YAML string: %s.', substr($scalar, $i)));
375+
throw new ParseException(sprintf('Malformed inline YAML string: %s.', substr($scalar, $i)), self::$parsedLineNumber, $scalar, self::$parsedFilename);
376376
}
377377

378378
$output = substr($match[0], 1, strlen($match[0]) - 2);
@@ -455,7 +455,7 @@ private static function parseSequence($sequence, $flags, &$i = 0, $references =
455455
++$i;
456456
}
457457

458-
throw new ParseException(sprintf('Malformed inline YAML string: %s.', $sequence));
458+
throw new ParseException(sprintf('Malformed inline YAML string: %s.', $sequence), self::$parsedLineNumber, null, self::$parsedFilename);
459459
}
460460

461461
/**
@@ -572,7 +572,7 @@ private static function parseMapping($mapping, $flags, &$i = 0, $references = ar
572572
}
573573
}
574574

575-
throw new ParseException(sprintf('Malformed inline YAML string: %s.', $mapping));
575+
throw new ParseException(sprintf('Malformed inline YAML string: %s.', $mapping), self::$parsedLineNumber, null, self::$parsedFilename);
576576
}
577577

578578
/**
@@ -600,11 +600,11 @@ private static function evaluateScalar($scalar, $flags, $references = array())
600600

601601
// an unquoted *
602602
if (false === $value || '' === $value) {
603-
throw new ParseException('A reference must contain at least one character.');
603+
throw new ParseException('A reference must contain at least one character.', self::$parsedLineNumber, $value, self::$parsedFilename);
604604
}
605605

606606
if (!array_key_exists($value, $references)) {
607-
throw new ParseException(sprintf('Reference "%s" does not exist.', $value));
607+
throw new ParseException(sprintf('Reference "%s" does not exist.', $value), self::$parsedLineNumber, $value, self::$parsedFilename);
608608
}
609609

610610
return $references[$value];
@@ -639,7 +639,7 @@ private static function evaluateScalar($scalar, $flags, $references = array())
639639
}
640640

641641
if (self::$exceptionOnInvalidType) {
642-
throw new ParseException('Object support when parsing a YAML file has been disabled.');
642+
throw new ParseException('Object support when parsing a YAML file has been disabled.', self::$parsedLineNumber, $scalar, self::$parsedFilename);
643643
}
644644

645645
return;
@@ -651,7 +651,7 @@ private static function evaluateScalar($scalar, $flags, $references = array())
651651
}
652652

653653
if (self::$exceptionOnInvalidType) {
654-
throw new ParseException('Object support when parsing a YAML file has been disabled.');
654+
throw new ParseException('Object support when parsing a YAML file has been disabled.', self::$parsedLineNumber, $scalar, self::$parsedFilename);
655655
}
656656

657657
return;
@@ -661,7 +661,7 @@ private static function evaluateScalar($scalar, $flags, $references = array())
661661
}
662662

663663
if (self::$exceptionOnInvalidType) {
664-
throw new ParseException('Object support when parsing a YAML file has been disabled.');
664+
throw new ParseException('Object support when parsing a YAML file has been disabled.', self::$parsedLineNumber, $scalar, self::$parsedFilename);
665665
}
666666

667667
return;
@@ -673,10 +673,10 @@ private static function evaluateScalar($scalar, $flags, $references = array())
673673
return constant($const);
674674
}
675675

676-
throw new ParseException(sprintf('The constant "%s" is not defined.', $const));
676+
throw new ParseException(sprintf('The constant "%s" is not defined.', $const), self::$parsedLineNumber, $scalar, self::$parsedFilename);
677677
}
678678
if (self::$exceptionOnInvalidType) {
679-
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));
679+
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), self::$parsedLineNumber, $scalar, self::$parsedFilename);
680680
}
681681

682682
return;
@@ -686,10 +686,10 @@ private static function evaluateScalar($scalar, $flags, $references = array())
686686
return constant($const);
687687
}
688688

689-
throw new ParseException(sprintf('The constant "%s" is not defined.', $const));
689+
throw new ParseException(sprintf('The constant "%s" is not defined.', $const), self::$parsedLineNumber, $scalar, self::$parsedFilename);
690690
}
691691
if (self::$exceptionOnInvalidType) {
692-
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));
692+
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), self::$parsedLineNumber, $scalar, self::$parsedFilename);
693693
}
694694

695695
return;
@@ -781,7 +781,7 @@ private static function parseTag($value, &$i, $flags)
781781

782782
// Built-in tags
783783
if ($tag && '!' === $tag[0]) {
784-
throw new ParseException(sprintf('The built-in tag "!%s" is not implemented.', $tag));
784+
throw new ParseException(sprintf('The built-in tag "!%s" is not implemented.', $tag), self::$parsedLineNumber, $value, self::$parsedFilename);
785785
}
786786

787787
if (Yaml::PARSE_CUSTOM_TAGS & $flags) {
@@ -790,7 +790,7 @@ private static function parseTag($value, &$i, $flags)
790790
return $tag;
791791
}
792792

793-
throw new ParseException(sprintf('Tags support is not enabled. Enable the `Yaml::PARSE_CUSTOM_TAGS` flag to use "!%s".', $tag));
793+
throw new ParseException(sprintf('Tags support is not enabled. Enable the `Yaml::PARSE_CUSTOM_TAGS` flag to use "!%s".', $tag), self::$parsedLineNumber, $value, self::$parsedFilename);
794794
}
795795

796796
/**
@@ -805,11 +805,11 @@ public static function evaluateBinaryScalar($scalar)
805805
$parsedBinaryData = self::parseScalar(preg_replace('/\s/', '', $scalar));
806806

807807
if (0 !== (strlen($parsedBinaryData) % 4)) {
808-
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)));
808+
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)), self::$parsedLineNumber, $scalar, self::$parsedFilename);
809809
}
810810

811811
if (!Parser::preg_match('#^[A-Z0-9+/]+={0,2}$#i', $parsedBinaryData)) {
812-
throw new ParseException(sprintf('The base64 encoded data (%s) contains invalid characters.', $parsedBinaryData));
812+
throw new ParseException(sprintf('The base64 encoded data (%s) contains invalid characters.', $parsedBinaryData), self::$parsedLineNumber, $scalar, self::$parsedFilename);
813813
}
814814

815815
return base64_decode($parsedBinaryData, true);

0 commit comments

Comments
 (0)
0