Skip to content

Commit db72005

Browse files
committed
[Yaml] parse PHP constants in mapping keys
1 parent 6da6853 commit db72005

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ private function doParse($value, $flags)
159159
$this->refs[$isRef] = end($data);
160160
}
161161
} elseif (
162-
self::preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|(?:![^\s]++\s++)?[^ \'"\[\{!].*?) *\:(\s++(?P<value>.+))?$#u', rtrim($this->currentLine), $values)
162+
self::preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|(?:!?!php/const:)?(?:![^\s]++\s++)?[^ \'"\[\{!].*?) *\:(\s++(?P<value>.+))?$#u', rtrim($this->currentLine), $values)
163163
&& (false === strpos($values['key'], ' #') || in_array($values['key'][0], array('"', "'")))
164164
) {
165165
if ($context && 'sequence' == $context) {
@@ -172,7 +172,14 @@ private function doParse($value, $flags)
172172
try {
173173
Inline::$parsedLineNumber = $this->getRealCurrentLineNb();
174174
$i = 0;
175-
$key = Inline::parseScalar($values['key'], 0, null, $i, !(Yaml::PARSE_KEYS_AS_STRINGS & $flags));
175+
$evaluateKey = !(Yaml::PARSE_KEYS_AS_STRINGS & $flags);
176+
177+
// constants in key will be evaluated anyway
178+
if (isset($values['key'][0]) && '!' === $values['key'][0] && Yaml::PARSE_CONSTANT & $flags) {
179+
$evaluateKey = true;
180+
}
181+
182+
$key = Inline::parseScalar($values['key'], 0, null, $i, $evaluateKey);
176183
} catch (ParseException $e) {
177184
$e->setParsedLine($this->getRealCurrentLineNb() + 1);
178185
$e->setSnippet($this->currentLine);

src/Symfony/Component/Yaml/Tests/ParserTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,9 +1717,59 @@ public function testParserCleansUpReferencesBetweenRuns()
17171717
YAML;
17181718
$this->parser->parse($yaml);
17191719
}
1720+
1721+
public function testPhpConstantTagMappingKey()
1722+
{
1723+
$yaml = <<<YAML
1724+
transitions:
1725+
!php/const:Symfony\Component\Yaml\Tests\B::FOO:
1726+
from:
1727+
- !php/const:Symfony\Component\Yaml\Tests\B::BAR
1728+
to: !php/const:Symfony\Component\Yaml\Tests\B::BAZ
1729+
YAML;
1730+
$expected = array(
1731+
'transitions' => array(
1732+
'foo' => array(
1733+
'from' => array(
1734+
'bar',
1735+
),
1736+
'to' => 'baz',
1737+
),
1738+
),
1739+
);
1740+
1741+
$this->assertSame($expected, $this->parser->parse($yaml, Yaml::PARSE_CONSTANT));
1742+
}
1743+
1744+
public function testPhpConstantTagMappingKeyWithKeysCastToStrings()
1745+
{
1746+
$yaml = <<<YAML
1747+
transitions:
1748+
!php/const:Symfony\Component\Yaml\Tests\B::FOO:
1749+
from:
1750+
- !php/const:Symfony\Component\Yaml\Tests\B::BAR
1751+
to: !php/const:Symfony\Component\Yaml\Tests\B::BAZ
1752+
YAML;
1753+
$expected = array(
1754+
'transitions' => array(
1755+
'foo' => array(
1756+
'from' => array(
1757+
'bar',
1758+
),
1759+
'to' => 'baz',
1760+
),
1761+
),
1762+
);
1763+
1764+
$this->assertSame($expected, $this->parser->parse($yaml, Yaml::PARSE_CONSTANT | Yaml::PARSE_KEYS_AS_STRINGS));
1765+
}
17201766
}
17211767

17221768
class B
17231769
{
17241770
public $b = 'foo';
1771+
1772+
const FOO = 'foo';
1773+
const BAR = 'bar';
1774+
const BAZ = 'baz';
17251775
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy