8000 [Yaml] parse PHP constants in mapping keys by xabbuh · Pull Request #22878 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Yaml] parse PHP constants in mapping keys #22878

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

Merged
merged 1 commit into from
May 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/Symfony/Component/Yaml/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ private function doParse($value, $flags)
$this->refs[$isRef] = end($data);
}
} elseif (
self::preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|(?:![^\s]++\s++)?[^ \'"\[\{!].*?) *\:(\s++(?P<value>.+))?$#u', rtrim($this->currentLine), $values)
self::preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|(?:!?!php/const:)?(?:![^\s]++\s++)?[^ \'"\[\{!].*?) *\:(\s++(?P<value>.+))?$#u', rtrim($this->currentLine), $values)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the !php/const tag is a bit special as it is separated with a colon from its value instead of a space which is used when parsing other tags

&& (false === strpos($values['key'], ' #') || in_array($values['key'][0], array('"', "'")))
) {
if ($context && 'sequence' == $context) {
Expand All @@ -221,7 +221,14 @@ private function doParse($value, $flags)
try {
Inline::$parsedLineNumber = $this->getRealCurrentLineNb();
$i = 0;
$key = Inline::parseScalar($values['key'], 0, null, $i, !(Yaml::PARSE_KEYS_AS_STRINGS & $flags));
$evaluateKey = !(Yaml::PARSE_KEYS_AS_STRINGS & $flags);
Copy link
Contributor
@GuilhemN GuilhemN May 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really don't get why we would ever want to use this flag, this is like saying "I don't want to respect the spec", don't you think we should just deprecate it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need this to deprecate the previous behaviour. Otherwise we have no smooth upgrade path in 3.4.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#21774 introduced a small bc break anyway and this only allows people to go back to the old behavior by using this flag, right?


// constants in key will be evaluated anyway
if (isset($values['key'][0]) && '!' === $values['key'][0] && Yaml::PARSE_CONSTANT & $flags) {
$evaluateKey = true;
}

$key = Inline::parseScalar($values['key'], 0, null, $i, $evaluateKey);
} catch (ParseException $e) {
$e->setParsedLine($this->getRealCurrentLineNb() + 1);
$e->setSnippet($this->currentLine);
Expand Down
50 changes: 50 additions & 0 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1819,9 +1819,59 @@ public function testParserCleansUpReferencesBetweenRuns()
YAML;
$this->parser->parse($yaml);
}

public function testPhpConstantTagMappingKey()
{
$yaml = <<<YAML
transitions:
!php/const:Symfony\Component\Yaml\Tests\B::FOO:
from:
- !php/const:Symfony\Component\Yaml\Tests\B::BAR
to: !php/const:Symfony\Component\Yaml\Tests\B::BAZ
YAML;
$expected = array(
'transitions' => array(
'foo' => array(
'from' => array(
'bar',
),
'to' => 'baz',
),
),
);

$this->assertSame($expected, $this->parser->parse($yaml, Yaml::PARSE_CONSTANT));
}

public function testPhpConstantTagMappingKeyWithKeysCastToStrings()
{
$yaml = <<<YAML
transitions:
!php/const:Symfony\Component\Yaml\Tests\B::FOO:
from:
- !php/const:Symfony\Component\Yaml\Tests\B::BAR
to: !php/const:Symfony\Component\Yaml\Tests\B::BAZ
YAML;
$expected = array(
'transitions' => array(
'foo' => array(
'from' => array(
'bar',
),
'to' => 'baz',
),
),
);

$this->assertSame($expected, $this->parser->parse($yaml, Yaml::PARSE_CONSTANT | Yaml::PARSE_KEYS_AS_STRINGS));
}
}

class B
{
public $b = 'foo';

const FOO = 'foo';
const BAR = 'bar';
const BAZ = 'baz';
}
0