diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index 52cd1867efcfd..42f61af9de0a4 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -208,7 +208,7 @@ private function doParse($value, $flags) $this->refs[$isRef] = end($data); } } elseif ( - self::preg_match('#^(?P'.Inline::REGEX_QUOTED_STRING.'|(?:![^\s]++\s++)?[^ \'"\[\{!].*?) *\:(\s++(?P.+))?$#u', rtrim($this->currentLine), $values) + self::preg_match('#^(?P'.Inline::REGEX_QUOTED_STRING.'|(?:!?!php/const:)?(?:![^\s]++\s++)?[^ \'"\[\{!].*?) *\:(\s++(?P.+))?$#u', rtrim($this->currentLine), $values) && (false === strpos($values['key'], ' #') || in_array($values['key'][0], array('"', "'"))) ) { if ($context && 'sequence' == $context) { @@ -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); + + // 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); diff --git a/src/Symfony/Component/Yaml/Tests/ParserTest.php b/src/Symfony/Component/Yaml/Tests/ParserTest.php index c8f4a7d40e35e..a0bef9d528c81 100644 --- a/src/Symfony/Component/Yaml/Tests/ParserTest.php +++ b/src/Symfony/Component/Yaml/Tests/ParserTest.php @@ -1819,9 +1819,59 @@ public function testParserCleansUpReferencesBetweenRuns() YAML; $this->parser->parse($yaml); } + + public function testPhpConstantTagMappingKey() + { + $yaml = << array( + 'foo' => array( + 'from' => array( + 'bar', + ), + 'to' => 'baz', + ), + ), + ); + + $this->assertSame($expected, $this->parser->parse($yaml, Yaml::PARSE_CONSTANT)); + } + + public function testPhpConstantTagMappingKeyWithKeysCastToStrings() + { + $yaml = << 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'; }