8000 feature #10552 [YAML] Added support for object-maps (polyfractal, ni… · symfony/symfony@36d144b · GitHub
[go: up one dir, main page]

Skip to content

Commit 36d144b

Browse files
committed
feature #10552 [YAML] Added support for object-maps (polyfractal, nicolas-grekas)
This PR was merged into the 2.5-dev branch. Discussion ---------- [YAML] Added support for object-maps | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | NA | License | MIT | Doc PR | NA Proposal for #10114 Commits ------- 0cee604 [Yaml] Cleanups for object maps e2d5468 [Yaml] Added support for object maps
2 parents 4c12b7b + 0cee604 commit 36d144b

File tree

3 files changed

+215
-122
lines changed

3 files changed

+215
-122
lines changed

src/Symfony/Component/Yaml/Inline.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,25 @@ class Inline
2525

2626
private static $exceptionOnInvalidType = false;
2727
private static $objectSupport = false;
28+
private static $objectForMap = false;
2829

2930
/**
3031
* Converts a YAML string to a PHP array.
3132
*
32-
* @param string $value A YAML string
33-
* @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
34-
* @param bool $objectSupport true if object support is enabled, false otherwise
33+
* @param string $value A YAML string
34+
* @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
35+
* @param bool $objectSupport true if object support is enabled, false otherwise
36+
* @param bool $objectForMap true if maps should return a stdClass instead of array()
3537
*
3638
* @return array A PHP array representing the YAML string
3739
*
3840
* @throws ParseException
3941
*/
40-
public static function parse($value, $exceptionOnInvalidType = false, $objectSupport = false)
42+
public static function parse($value, $exceptionOnInvalidType = false, $objectSupport = false, $objectForMap = false)
4143< 8000 div class="diff-text-inner"> {
4244
self::$exceptionOnInvalidType = $exceptionOnInvalidType;
4345
self::$objectSupport = $objectSupport;
46+
self::$objectForMap = $objectForMap;
4447

4548
$value = trim($value);
4649

@@ -181,9 +184,9 @@ private static function dumpArray($value, $exceptionOnInvalidType, $objectSuppor
181184
/**
182185
* Parses a scalar to a YAML string.
183186
*
184-
* @param scalar $scalar
185-
* @param string $delimiters
186-
* @param array $stringDelimiters
187+
* @param scalar $scalar
188+
* @param string $delimiters
189+
* @param array $stringDelimiters
187190
* @param int &$i
188191
* @param bool $evaluate
189192
*
@@ -232,7 +235,7 @@ public static function parseScalar($scalar, $delimiters = null, $stringDelimiter
232235
* Parses a quoted scalar to YAML.
233236
*
234237
* @param string $scalar
235-
* @param int &$i
238+
* @param int &$i
236239
*
237240
* @return string A YAML string
238241
*
@@ -261,7 +264,7 @@ private static function parseQuotedScalar($scalar, &$i)
261264
/**
262265
* Parses a sequence to a YAML string.
263266
*
264-
* @param string $sequence
267+
* @param string $sequence
265268
* @param int &$i
266269
*
267270
* @return string A YAML string
@@ -317,7 +320,7 @@ private static function parseSequence($sequence, &$i = 0)
317320
/**
318321
* Parses a mapping to a YAML string.
319322
*
320-
* @param string $mapping
323+
* @param string $mapping
321324
* @param int &$i
322325
*
323326
* @return string A YAML string
@@ -338,6 +341,10 @@ private static function parseMapping($mapping, &$i = 0)
338341
++$i;
339342
continue 2;
340343
case '}':
344+
if (self::$objectForMap) {
345+
return (object) $output;
346+
}
347+
341348
return $output;
342349
}
343350

@@ -346,6 +353,7 @@ private static function parseMapping($mapping, &$i = 0)
346353

347354
// value
348355
$done = false;
356+
349357
while ($i < $len) {
350358
switch ($mapping[$i]) {
351359
case '[':
@@ -399,7 +407,7 @@ private static function parseMapping($mapping, &$i = 0)
399407
/**
400408
* Evaluates scalars and replaces magic values.
401409
*
402-
* @param string $scalar
410+
* @param string $scalar
403411
*
404412
* @return string A YAML string
405413
*/

src/Symfony/Component/Yaml/Parser.php

Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@ public function __construct($offset = 0)
4444
* @param string $value A YAML string
4545
* @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
4646
* @param bool $objectSupport true if object support is enabled, false otherwise
47+
* @param bool $objectForMap true if maps should return a stdClass instead of array()
4748
*
4849
* @return mixed A PHP value
4950
*
5051
* @throws ParseException If the YAML is not valid
5152
*/
52-
public function parse($value, $exceptionOnInvalidType = false, $objectSupport = false)
53+
public function parse($value, $exceptionOnInvalidType = false, $objectSupport = false, $objectForMap = false)
5354
{
5455
$this->currentLineNb = -1;
5556
$this->currentLine = '';
@@ -93,7 +94,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
9394
$c = $this->getRealCurrentLineNb() + 1;
9495
$parser = new Parser($c);
9596
$parser->refs =& $this->refs;
96-
$data[] = $parser->parse($this->getNextEmbedBlock(), $exceptionOnInvalidType, $objectSupport);
97+
$data[] = $parser->parse($this->getNextEmbedBlock(), $exceptionOnInvalidType, $objectSupport, $objectForMap);
9798
} else {
9899
if (isset($values['leadspaces'])
99100
&& ' ' == $values['leadspaces']
@@ -109,9 +110,9 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
109110
$block .= "\n".$this->getNextEmbedBlock($this->getCurrentLineIndentation() + 2);
110111
}
111112

112-
$data[] = $parser->parse($block, $exceptionOnInvalidType, $objectSupport);
113+
$data[] = $parser->parse($block, $exceptionOnInvalidType, $objectSupport, $objectForMap);
113114
} else {
114-
$data[] = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport);
115+
$data[] = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport, $objectForMap);
115116
}
116117
}
117118
} elseif (preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\[\{].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->currentLine, $values) && false === strpos($values['key'],' #')) {
@@ -121,7 +122,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
121122
$context = 'mapping';
122123

123124
// force correct settings
124-
Inline::parse(null, $exceptionOnInvalidType, $objectSupport);
125+
Inline::parse(null, $exceptionOnInvalidType, $objectSupport, $objectForMap);
125126
try {
126127
$key = Inline::parseScalar($values['key']);
127128
} catch (ParseException $e) {
@@ -146,7 +147,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
146147
$c = $this->getRealCurrentLineNb() + 1;
147148
$parser = new Parser($c);
148149
$parser->refs =& $this->refs;
149-
$parsed = $parser->parse($value, $exceptionOnInvalidType, $objectSupport);
150+
$parsed = $parser->parse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap);
150151

151152
$merged = array();
152153
if (!is_array($parsed)) {
@@ -188,7 +189,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
188189
$c = $this->getRealCurrentLineNb() + 1;
189190
$parser = new Parser($c);
190191
$parser->refs =& $this->refs;
191-
$value = $parser->parse($this->getNextEmbedBlock(), $exceptionOnInvalidType, $objectSupport);
192+
$value = $parser->parse($this->getNextEmbedBlock(), $exceptionOnInvalidType, $objectSupport, $objectForMap);
192193
// Spec: Keys MUST be unique; first one wins.
193194
// Parser cannot abort this mapping earlier, since lines
194195
// are processed sequentially.
@@ -200,7 +201,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
200201
if ($isInPlace) {
201202
$data = $this->refs[$isInPlace];
202203
} else {
203-
$value = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport);;
204+
$value = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport, $objectForMap);
204205
// Spec: Keys MUST be unique; first one wins.
205206
// Parser cannot abort this mapping earlier, since lines
206207
// are processed sequentially.
@@ -214,7 +215,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
214215
$lineCount = count($this->lines);
215216
if (1 === $lineCount || (2 === $lineCount && empty($this->lines[1]))) {
216217
try {
217-
$value = Inline::parse($this->lines[0], $exceptionOnInvalidType, $objectSupport);
218+
$value = Inline::parse($this->lines[0], $exceptionOnInvalidType, $objectSupport, $objectForMap);
218219
} catch (ParseException $e) {
219220
$e->setParsedLine($this->getRealCurrentLineNb() + 1);
220221
$e->setSnippet($this->currentLine);
@@ -390,15 +391,16 @@ private function moveToPreviousLine()
390391
/**
391392
* Parses a YAML value.
392393
*
393-
* @param string $value A YAML value
394-
* @param bool $exceptionOnInvalidType True if an exception must be thrown on invalid types false otherwise
395-
* @param bool $objectSupport True if object support is enabled, false otherwise
394+
* @param string $value A YAML value
395+
* @param bool $exceptionOnInvalidType True if an exception must be thrown on invalid types false otherwise
396+
* @param bool $objectSupport True if object support is enabled, false otherwise
397+
* @param bool $objectForMap true if maps should return a stdClass instead of array()
396398
*
397-
* @return mixed A PHP value
399+
* @return mixed A PHP value
398400
*
399401
* @throws ParseException When reference does not exist
400402
*/
401-
private function parseValue($value, $exceptionOnInvalidType, $objectSupport)
403+
private function parseValue($value, $exceptionOnInvalidType, $objectSupport, $objectForMap)
402404
{
403405
if (0 === strpos($value, '*')) {
404406
if (false !== $pos = strpos($value, '#')) {
@@ -421,7 +423,7 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport)
< 1241 /td>
421423
}
422424

423425
try {
424-
return Inline::parse($value, $exceptionOnInvalidType, $objectSupport);
426+
return Inline::parse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap);
425427
} catch (ParseException $e) {
426428
$e->setParsedLine($this->getRealCurrentLineNb() + 1);
427429
$e->setSnippet($this->currentLine);