Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ae52fe6

Browse files
committed
[Yaml] parse PHP constants in mapping keys
1 parent c9da3d9 commit ae52fe6

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
@@ -208,7 +208,7 @@ private function doParse($value, $flags)
208208
$this->refs[$isRef] = end($data);
209209
}
210210
} elseif (
211-
self::preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|(?:![^\s]++\s++)?[^ \'"\[\{!].*?) *\:(\s++(?P<value>.+))?$#u', rtrim($this->currentLine), $values)
211+
self::preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|(?:!?!php/const:)?(?:![^\s]++\s++)?[^ \'"\[\{!].*?) *\:(\s++(?P<value>.+))?$#u', rtrim($this->currentLine), $values)
212212
&& (false === strpos($values['key'], ' #') || in_array($values['key'][0], array('"', "'")))
213213
) {
214214
if ($context && 'sequence' == $context) {
@@ -221,7 +221,14 @@ private function doParse($value, $flags)
221221
try {
222222
Inline::$parsedLineNumber = $this->getRealCurrentLineNb();
223223
$i = 0;
224-
$key = Inline::parseScalar($values['key'], 0, null, $i, !(Yaml::PARSE_KEYS_AS_STRINGS & $flags));
224+
$evaluateKey = !(Yaml::PARSE_KEYS_AS_STRINGS & $flags);
225+
226+
// constants in key will be evaluated anyway
227+
if (isset($values['key'][0]) && '!' === $values['key'][0] && Yaml::PARSE_CONSTANT & $flags) {
228+
$evaluateKey = true;
229+
}
230+
231+
$key = Inline::parseScalar($values['key'], 0, null, $i, $evaluateKey);
225232
} catch (ParseException $e) {
226233
$e->setParsedLine($this->getRealCurrentLineNb() + 1);
227234
$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
@@ -1819,9 +1819,59 @@ public function testParserCleansUpReferencesBetweenRuns()
18191819
YAML;
18201820
$this->parser->parse($yaml);
18211821
}
1822+
1823+
public function testPhpConstantTagMappingKey()
1824+
{
1825+
$yaml = <<<YAML
1826+
transitions:
1827+
!php/const:Symfony\Component\Yaml\Tests\B::FOO:
1828+
from:
1829+
- !php/const:Symfony\Component\Yaml\Tests\B::BAR
1830+
to: !php/const:Symfony\Component\Yaml\Tests\B::BAZ
1831+
YAML;
1832+
$expected = array(
1833+
'transitions' => array(
1834+
'foo' => array(
1835+
'from' => array(
1836+
'bar',
1837+
),
1838+
'to' => 'baz',
1839+
),
1840+
),
1841+
);
1842+
1843+
$this->assertSame($expected, $this->parser->parse($yaml, Yaml::PARSE_CONSTANT));
1844+
}
1845+
1846+
public function testPhpConstantTagMappingKeyWithKeysCastToStrings()
1847+
{
1848+
$yaml = <<<YAML
1849+
transitions:
1850+
!php/const:Symfony\Component\Yaml\Tests\B::FOO:
1851+
from:
1852+
- !php/const:Symfony\Component\Yaml\Tests\B::BAR
1853+
to: !php/const:Symfony\Component\Yaml\Tests\B::BAZ
1854+
YAML;
1855+
$expected = array(
1856+
'transitions' => array(
1857+
'foo' => array(
1858+
'from' => array(
1859+
'bar',
1860+
),
1861+
'to' => 'baz',
1862+
),
1863+
),
1864+
);
1865+
1866+
$this->assertSame($expected, $this->parser->parse($yaml, Yaml::PARSE_CONSTANT | Yaml::PARSE_KEYS_AS_STRINGS));
1867+
}
18221868
}
18231869

18241870
class B
18251871
{
18261872
public $b = 'foo';
1873+
1874+
const FOO = 'foo';
1875+
const BAR = 'bar';
1876+
const BAZ = 'baz';
18271877
}

0 commit comments

Comments
 (0)