-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Yaml] Added support for parsing PHP constants #18626
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
use Symfony\Component\Config\Resource\FileResource; | ||
use Symfony\Component\Yaml\Exception\ParseException; | ||
use Symfony\Component\Yaml\Parser as YamlParser; | ||
use Symfony\Component\Yaml\Yaml; | ||
use Symfony\Component\ExpressionLanguage\Expression; | ||
|
||
/** | ||
|
@@ -366,7 +367,7 @@ protected function loadFile($file) | |
} | ||
|
||
try { | ||
$configuration = $this->yamlParser->parse(file_get_contents($file)); | ||
$configuration = $this->yamlParser->parse(file_get_contents($file), Yaml::PARSE_CONSTANT); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this means that the DI component now can't work with Yaml < 3.2, isn't it? So the composer.json file should get a conflict rule. Or: would it make sense to enable const parsing by default (and thus not require any change here?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} catch (ParseException $e) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
throw new InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ | |
"php": ">=5.5.9" | ||
}, | ||
"require-dev": { | ||
"symfony/yaml": "~2.8|~3.0", | ||
"symfony/yaml": "~3.2", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Isn't this line enough? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this line does apply only when testing the component, it has no effect when used as a dep of something else |
||
"symfony/config": "~2.8|~3.0", | ||
"symfony/expression-language": "~2.8|~3.0" | ||
}, | ||
|
@@ -29,6 +29,9 @@ | |
"symfony/expression-language": "For using expressions in service container configuration", | ||
"symfony/proxy-manager-bridge": "Generate service proxies to lazy load them" | ||
}, | ||
"conflict": { | ||
"symfony/yaml": "<3.2" | ||
}, | ||
"autoload": { | ||
"psr-4": { "Symfony\\Component\\DependencyInjection\\": "" }, | ||
"exclude-from-classmap": [ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ class Inline | |
private static $exceptionOnInvalidType = false; | ||
private static $objectSupport = false; | ||
private static $objectForMap = false; | ||
private static $constantSupport = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we sure we need a flag here? Couldn't we unconditionally enable this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @xabbuh what do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Depends on whether we want to throw exceptions when a constant is not defined. Imo the flag makes sense if we then throw an exception when a constant does not exist (which would make debugging a lot easier than when we just returned There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Throwing the exception already depends on the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok then, they are many options.
For now I vote for option 2, since php objects are not parsed by default. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My vote is for a flag too because this component should be as close to the Yaml spec as possible. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with that. It will consistent with how we already treat PHP object support. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fine by me, then my comment above applies. |
||
|
||
/** | ||
* Converts a YAML string to a PHP array. | ||
|
@@ -77,6 +78,7 @@ public static function parse($value, $flags = 0, $references = array()) | |
self::$exceptionOnInvalidType = (bool) (Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE & $flags); | ||
self::$objectSupport = (bool) (Yaml::PARSE_OBJECT & $flags); | ||
self::$objectForMap = (bool) (Yaml::PARSE_OBJECT_FOR_MAP & $flags); | ||
self::$constantSupport = (bool) (Yaml::PARSE_CONSTANT & $flags); | ||
|
||
$value = trim($value); | ||
|
||
|
@@ -580,6 +582,19 @@ private static function evaluateScalar($scalar, $flags, $references = array()) | |
throw new ParseException('Object support when parsing a YAML file has been disabled.'); | ||
} | ||
|
||
return; | ||
case 0 === strpos($scalar, '!php/const:'): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about: case 0 === strpos($scalar, '!php/const:'):
if (self::$constantSupport && defined($const = substr($scalar, 11))) {
return constant($const);
}
if (self::$exceptionOnInvalidType) {
throw new ParseException(sprintf('The "%s" YAML string could not be parsed because the %s.', $scalar, self::$constantSupport ? ' "Yaml::PARSE_CONSTANT" flag was not set' : 'corresponding PHP constant was not defined'));
}
return; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nicolas-grekas Working on it I think we should throw an exception when the constant is not defined even if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In that case maybe I should only change the YAML There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think throwing an exception when a constant is not defined is a good idea. |
||
if (self::$constantSupport) { | ||
if (defined($const = substr($scalar, 11))) { | ||
return constant($const); | ||
} | ||
|
||
throw new ParseException(sprintf('The constant "%s" is not defined.', $const)); | ||
} | ||
if (self::$exceptionOnInvalidType) { | ||
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)); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This must throw an exception when constant support is disabled and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alright! |
||
|
||
return; | ||
case 0 === strpos($scalar, '!!float '): | ||
return (float) substr($scalar, 8); | ||
|
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.
can you please move it one line up in the yaml group?
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.
Are you sure about this? The "good way" would be to move up the
Expression
line but as a CS fix it shouldn't be done in master, right?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.
let's group yaml lines now that we can (but we don't move existing lines to reduce merge conflicts)