8000 expose references detected in inline notation structures · symfony/symfony@09a07b9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 09a07b9

Browse files
committed
expose references detected in inline notation structures
1 parent f8518ca commit 09a07b9

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

src/Symfony/Component/Yaml/Inline.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static function initialize(int $flags, int $parsedLineNumber = null, stri
5858
*
5959
* @throws ParseException
6060
*/
61-
public static function parse(string $value = null, int $flags = 0, array $references = [])
61+
public static function parse(string $value = null, int $flags = 0, array &$references = [])
6262
{
6363
self::initialize($flags);
6464

@@ -267,7 +267,7 @@ private static function dumpNull(int $flags): string
267267
*
268268
* @throws ParseException When malformed inline YAML string is parsed
269269
*/
270-
public static function parseScalar(string $scalar, int $flags = 0, array $delimiters = null, int &$i = 0, bool $evaluate = true, array $references = [])
270+
public static function parseScalar(string $scalar, int $flags = 0, array $delimiters = null, int &$i = 0, bool $evaluate = true, array &$references = [])
271271
{
272272
if (\in_array($scalar[$i], ['"', "'"])) {
273273
// quoted scalar
@@ -343,7 +343,7 @@ private static function parseQuotedScalar(string $scalar, int &$i): string
343343
*
344344
* @throws ParseException When malformed inline YAML string is parsed
345345
*/
346-
private static function parseSequence(string $sequence, int $flags, int &$i = 0, array $references = []): array
346+
private static function parseSequence(string $sequence, int $flags, int &$i = 0, array &$references = []): array
347347
{
348348
$output = [];
349349
$len = \strlen($sequence);
@@ -385,6 +385,11 @@ private static function parseSequence(string $sequence, int $flags, int &$i = 0,
385385
}
386386
}
387387

388+
if (\is_string($value) && '' !== $value && '&' === $value[0] && Parser::preg_match(Parser::REFERENCE_PATTERN, $value, $matches)) {
389+
$references[$matches['ref']] = $matches['value'];
390+
$value = $matches['value'];
391+
}
392+
388393
--$i;
389394
}
390395

@@ -407,7 +412,7 @@ private static function parseSequence(string $sequence, int $flags, int &$i = 0,
407412
*
408413
* @throws ParseException When malformed inline YAML string is parsed
409414
*/
410-
private static function parseMapping(string $mapping, int $flags, int &$i = 0, array $references = [])
415+
private static function parseMapping(string $mapping, int $flags, int &$i = 0, array &$references = [])
411416
{
412417
$output = [];
413418
$len = \strlen($mapping);
@@ -433,14 +438,14 @@ private static function parseMapping(string $mapping, int $flags, int &$i = 0, a
433438
// key
434439
$offsetBeforeKeyParsing = $i;
435440
$isKeyQuoted = \in_array($mapping[$i], ['"', "'"], true);
436-
$key = self::parseScalar($mapping, $flags, [':', ' '], $i, false, []);
441+
$key = self::parseScalar($mapping, $flags, [':', ' '], $i, false);
437442

438443
if ($offsetBeforeKeyParsing === $i) {
439444
throw new ParseException('Missing mapping key.', self::$parsedLineNumber + 1, $mapping);
440445
}
441446

442447
if ('!php/const' === $key) {
443-
$key .= ' '.self::parseScalar($mapping, $flags, [':'], $i, false, []);
448+
$key .= ' '.self::parseScalar($mapping, $flags, [':'], $i, false);
444449
$key = sel 6D4E f::evaluateScalar($key, $flags);
445450
}
446451

@@ -522,6 +527,11 @@ private static function parseMapping(string $mapping, int $flags, int &$i = 0, a
522527
if ('<<' === $key) {
523528
$output += $value;
524529
} elseif ($allowOverwrite || !isset($output[$key])) {
530+
if (\is_string($value) && '' !== $value && '&' === $value[0] && Parser::preg_match(Parser::REFERENCE_PATTERN, $value, $matches)) {
531+
$references[$matches['ref']] = $matches['value'];
532+
$value = $matches['value'];
533+
}
534+
525535
if (null !== $tag) {
526536
$output[$key] = new TaggedValue($tag, $value);
527537
} else {
@@ -548,7 +558,7 @@ private static function parseMapping(string $mapping, int $flags, int &$i = 0, a
548558
*
549559
* @throws ParseException when object parsing support was disabled and the parser detected a PHP object or when a reference could not be resolved
550560
*/
551-
private static function evaluateScalar(string $scalar, int $flags, array $references = [])
561+
private static function evaluateScalar(string $scalar, int $flags, array &$references = [])
552562
{
553563
$scalar = trim($scalar);
554564
$scalarLower = strtolower($scalar);

src/Symfony/Component/Yaml/Parser.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Parser
2525
{
2626
public const TAG_PATTERN = '(?P<tag>![\w!.\/:-]+)';
2727
public const BLOCK_SCALAR_HEADER_PATTERN = '(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)?';
28+
public const REFERENCE_PATTERN = '#^&(?P<ref>[^ ]++) *+(?P<value>.*)#u';
2829

2930
private $filename;
3031
private $offset = 0;
@@ -158,7 +159,7 @@ private function doParse(string $value, int $flags)
158159
}
159160
$context = 'sequence';
160161

161-
if (isset($values['value']) && '&' === $values['value'][0] && self::preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
162+
if (isset($values['value']) && '&' === $values['value'][0] && self::preg_match(self::REFERENCE_PATTERN, $values['value'], $matches)) {
162163
$isRef = $matches['ref'];
163164
$this->refsBeingParsed[] = $isRef;
164165
$values['value'] = $matches['value'];
@@ -296,7 +297,7 @@ private function doParse(string $value, int $flags)
296297
$data += $parsed; // array union
297298
}
298299
}
299-
} elseif ('<<' !== $key && isset($values['value']) && '&' === $values['value'][0] && self::preg_match('#^&(?P<ref>[^ ]++) *+(?P<value>.*)#u', $values['value'], $matches)) {
300+
} elseif ('<<' !== $key && isset($values['value']) && '&' === $values['value'][0] && self::preg_match(self::REFERENCE_PATTERN, $values['value'], $matches)) {
300301
$isRef = $matches['ref'];
301302
$this->refsBeingParsed[] = $isRef;
302303
$values['value'] = $matches['value'];

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ public function testParseScalarWithCorrectlyQuotedStringShouldReturnString()
187187
*/
188188
public function testParseReferences($yaml, $expected)
189189
{
190-
$this->assertSame($expected, Inline::parse($yaml, 0, ['var' => 'var-value']));
190+
$references = ['var' => 'var-value'];
191+
$this->assertSame($expected, Inline::parse($yaml, 0, $references));
191192
}
192193

193194
public function getDataForParseReferences()
@@ -211,7 +212,8 @@ public function testParseMapReferenceInSequence()
211212
'b' => 'Clark',
212213
'c' => 'Brian',
213214
];
214-
$this->assertSame([$foo], Inline::parse('[*foo]', 0, ['foo' => $foo]));
215+
$references = ['foo' => $foo];
216+
$this->assertSame([$foo], Inline::parse('[*foo]', 0, $references));
215217
}
216218

217219
public function testParseUnquotedAsterisk()

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,10 @@ public function testReferenceResolvingInInlineStrings()
10321032
'map' => ['key' => 'var-value'],
10331033
'list_in_map' => ['key' => ['var-value']],
10341034
'map_in_map' => ['foo' => ['bar' => 'var-value']],
1035+
'foo' => ['bar' => 'baz'],
1036+
'bar' => ['foo' => 'baz'],
1037+
'baz' => ['foo'],
1038+
'foobar' => ['foo'],
10351039
], Yaml::parse(<<<'EOF'
10361040
var: &var var-value
10371041
scalar: *var
@@ -1042,6 +1046,10 @@ public function testReferenceResolvingInInlineStrings()
10421046
map: { key: *var }
10431047
list_in_map: { key: [*var] }
10441048
map_in_map: { foo: { bar: *var } }
1049+
foo: { bar: &baz baz }
1050+
bar: { foo: *baz }
1051+
baz: [ &foo foo ]
1052+
foobar: [ *foo ]
10451053
EOF
10461054
));
10471055
}

0 commit comments

Comments
 (0)
0