-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Yaml] support parsing files #24253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Yaml] support parsing files #24253
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(improving deprecation message planned in a latter PR, isn't it?)
@@ -92,6 +92,18 @@ public function parse($value, $flags = 0) | |||
@trigger_error('Using the Yaml::PARSE_KEYS_AS_STRINGS flag is deprecated since version 3.4 as it will be removed in 4.0. Quote your keys when they are evaluable instead.', E_USER_DEPRECATED); | |||
} | |||
|
|||
if ((bool) (Yaml::PARSE_FILE & $flags)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (Yaml::PARSE_FILE & $flags) {
src/Symfony/Component/Yaml/Yaml.php
Outdated
@@ -33,6 +33,7 @@ class Yaml | |||
const PARSE_CONSTANT = 256; | |||
const PARSE_CUSTOM_TAGS = 512; | |||
const DUMP_EMPTY_ARRAY_AS_SEQUENCE = 1024; | |||
const PARSE_FILE = 4096; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2048
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nope, we have a deprecated flag below that uses 2048 :)
c7e8dce
to
daf9fcf
Compare
throw new ParseException(sprintf('File "%s" cannot be read.', $value)); | ||
} | ||
|
||
$value = file_get_contents($value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, we should ensure that ParseException
have the file set in them when throwing them (either through the constructor or through setParsedFile
). Otherwise, we don't benefit from the better error reporting.
This feels strange to use a flag to switch from filename to yaml content based on an flag. Why not creating a |
daf9fcf
to
bc20917
Compare
@jvasseur i think we prefer a single API, e.g. |
@ro0NL but this flag is fundamentally different from the others, the others modify the parsing behavior, this one change what the method expect as an argument. Let's look at the |
I tend to agree with @jvasseur here. Having a flag in the bitmask changing totally the meaning of the argument is dangerous. |
The example given by @jvasseur is indeed something we should consider and having a method like |
bc20917
to
ae1379d
Compare
@@ -26,6 +26,7 @@ class Parser | |||
const TAG_PATTERN = '(?P<tag>![\w!.\/:-]+)'; | |||
const BLOCK_SCALAR_HEADER_PATTERN = '(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)?'; | |||
|
|||
private $filename; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This property must be reset when doing a public call to parse()
, otherwise subsequent usages of the parser would still report the previous filename.
The other alternative is to reset it to null
in a finally
clause in parseFile
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
@@ -690,7 +705,7 @@ public function getInvalidBinaryData() | |||
|
|||
/** | |||
* @expectedException \Symfony\Component\Yaml\Exception\ParseException | |||
* @expectedExceptionMessage Malformed inline YAML string: {this, is not, supported}. | |||
* @expectedExceptionMessage Malformed inline YAML string: {this, is not, supported} (near "{this, is not, supported}"). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this message is weird. Should it really include the snippet here ?
61fb0c9
to
349120a
Compare
@@ -111,6 +137,8 @@ public function parse($value, $flags = 0) | |||
$data = $this->doParse($value, $flags); | |||
} catch (\Exception $e) { | |||
} catch (\Throwable $e) { | |||
} finally { | |||
$this->filename = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's strange to set the property in parseFile
and reset it here. Why not adding a try finally in the parseFile
method instead ?
|
||
$this->filename = $filename; | ||
|
||
return $this->parse(file_get_contents($filename, $flags)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The parenthesis are at the wrong place, $flags
is an argument of the parse
method not the file_get_contents
function.
349120a
to
f8950c3
Compare
@stof OK for you? Still 👍 on my side with |
@@ -51,6 +52,35 @@ public function __construct() | |||
} | |||
|
|||
/** | |||
* Parses a YAML file into a PHP value. | |||
* | |||
* @param string $filename A string containing YAML |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please update this:
@param string $filename A string containing YAML
to something like this:
@param string $filename A relative or absolute path to the YAML file
Same for the Yaml:: parseFile()
method below.
By the way ... should we replace $filename
by $filepath
everywhere in the code of this PR?
Other components should be updated to use it |
f8950c3
to
9becb8a
Compare
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
…[Validator] use the parseFile() method of the YAML parser (xabbuh) This PR was merged into the 3.4 branch. Discussion ---------- [DependencyInjection][Routing][Serializer][Translations][Validator] use the parseFile() method of the YAML parser | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #24253 (comment) | License | MIT | Doc PR | Commits ------- 8bf465f use the parseFile() method of the YAML parser
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).