Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1eac150

Browse filesBrowse files
bug #22878 [Yaml] parse PHP constants in mapping keys (xabbuh)
This PR was merged into the 3.3 branch. Discussion ---------- [Yaml] parse PHP constants in mapping keys | Q | A | ------------- | --- | Branch? | 3.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #22854 | License | MIT | Doc PR | Commits ------- ae52fe6 [Yaml] parse PHP constants in mapping keys
2 parents 4e95aac + ae52fe6 commit 1eac150
Copy full SHA for 1eac150

File tree

Expand file treeCollapse file tree

2 files changed

+59
-2
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+59
-2
lines changed

‎src/Symfony/Component/Yaml/Parser.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Parser.php
+9-2Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Tests/ParserTest.php
+50Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.