Skip to content

Commit 5190891

Browse files
committed
Merge branch '2.7' into 2.8
* 2.7: [Yaml] fix exception contexts People - person singularization [Yaml] properly handle unindented collections [Serializer] Add test for ignored attributes during denormalization chomp newlines only at the end of YAML documents Fixed server status command when port has been omitted Update UPGRADE FROM 2.x to 3.0 Catch \Throwable Use levenshtein level for better Bundle matching [WebProfilerBundle] Fix CORS ajax security issues
2 parents e4fbcc6 + 507901b commit 5190891

File tree

2 files changed

+56
-13
lines changed

2 files changed

+56
-13
lines changed

Parser.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Parser
2525
const FOLDED_SCALAR_PATTERN = self::BLOCK_SCALAR_HEADER_PATTERN;
2626

2727
private $offset = 0;
28+
private $totalNumberOfLines;
2829
private $lines = array();
2930
private $currentLineNb = -1;
3031
private $currentLine = '';
@@ -33,11 +34,13 @@ class Parser
3334
/**
3435
* Constructor.
3536
*
36-
* @param int $offset The offset of YAML document (used for line numbers in error messages)
37+
* @param int $offset The offset of YAML document (used for line numbers in error messages)
38+
* @param int|null $totalNumberOfLines The overall number of lines being parsed
3739
*/
38-
public function __construct($offset = 0)
40+
public function __construct($offset = 0, $totalNumberOfLines = null)
3941
{
4042
$this->offset = $offset;
43+
$this->totalNumberOfLines = $totalNumberOfLines;
4144
}
4245

4346
/**
@@ -62,6 +65,10 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
6265
$value = $this->cleanup($value);
6366
$this->lines = explode("\n", $value);
6467

68+
if (null === $this->totalNumberOfLines) {
69+
$this->totalNumberOfLines = count($this->lines);
70+
}
71+
6572
if (2 /* MB_OVERLOAD_STRING */ & (int) ini_get('mbstring.func_overload')) {
6673
$mbEncoding = mb_internal_encoding();
6774
mb_internal_encoding('UTF-8');
@@ -83,7 +90,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
8390
$isRef = $mergeNode = false;
8491
if (preg_match('#^\-((?P<leadspaces>\s+)(?P<value>.+?))?\s*$#u', $this->currentLine, $values)) {
8592
if ($context && 'mapping' == $context) {
86-
throw new ParseException('You cannot define a sequence item when in a mapping');
93+
throw new ParseException('You cannot define a sequence item when in a mapping', $this->getRealCurrentLineNb() + 1, $this->currentLine);
8794
}
8895
$context = 'sequence';
8996

@@ -95,7 +102,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
95102
// array
96103
if (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
97104
$c = $this->getRealCurrentLineNb() + 1;
98-
$parser = new self($c);
105+
$parser = new self($c, $this->totalNumberOfLines);
99106
$parser->refs = &$this->refs;
100107
$data[] = $parser->parse($this->getNextEmbedBlock(null, true), $exceptionOnInvalidType, $objectSupport, $objectForMap);
101108
} else {
@@ -104,7 +111,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
104111
) {
105112
// this is a compact notation element, add to next block and parse
106113
$c = $this->getRealCurrentLineNb();
107-
$parser = new self($c);
114+
$parser = new self($c, $this->totalNumberOfLines);
108115
$parser->refs = &$this->refs;
109116

110117
$block = $values['value'];
@@ -122,7 +129,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
122129
}
123130
} elseif (preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\[\{].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->currentLine, $values) && (false === strpos($values['key'], ' #') || in_array($values['key'][0], array('"', "'")))) {
124131
if ($context && 'sequence' == $context) {
125-
throw new ParseException('You cannot define a mapping item when in a sequence');
132+
throw new ParseException('You cannot define a mapping item when in a sequence', $this->currentLineNb + 1, $this->currentLine);
126133
}
127134
$context = 'mapping';
128135

@@ -169,7 +176,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
169176
$value = $this->getNextEmbedBlock();
170177
}
171178
$c = $this->getRealCurrentLineNb() + 1;
172-
$parser = new self($c);
179+
$parser = new self($c, $this->totalNumberOfLines);
173180
$parser->refs = &$this->refs;
174181
$parsed = $parser->parse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap);
175182

@@ -220,7 +227,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
220227
}
221228
} else {
222229
$c = $this->getRealCurrentLineNb() + 1;
223-
$parser = new self($c);
230+
$parser = new self($c, $this->totalNumberOfLines);
224231
$parser->refs = &$this->refs;
225232
$value = $parser->parse($this->getNextEmbedBlock(), $exceptionOnInvalidType, $objectSupport, $objectForMap);
226233
// Spec: Keys MUST be unique; first one wins.
@@ -243,7 +250,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
243250
} else {
244251
// multiple documents are not supported
245252
if ('---' === $this->currentLine) {
246-
throw new ParseException('Multiple documents are not supported.');
253+
throw new ParseException('Multiple documents are not supported.', $this->currentLineNb + 1, $this->currentLine);
247254
}
248255

249256
// 1-liner optionally followed by newline(s)
@@ -490,7 +497,7 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport, $ob
490497
}
491498

492499
if (!array_key_exists($value, $this->refs)) {
493-
throw new ParseException(sprintf('Reference "%s" does not exist.', $value), $this->currentLine);
500+
throw new ParseException(sprintf('Reference "%s" does not exist.', $value), $this->currentLineNb + 1, $this->currentLine);
494501
}
495502

496503
return $this->refs[$value];
@@ -585,6 +592,8 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
585592
if ($notEOF) {
586593
$blockLines[] = '';
587594
$this->moveToPreviousLine();
595+
} elseif (!$notEOF && !$this->isCurrentLineLastLineInDocument()) {
596+
$blockLines[] = '';
588597
}
589598

590599
// folded style
@@ -691,6 +700,11 @@ private function isCurrentLineComment()
691700
return '' !== $ltrimmedLine && $ltrimmedLine[0] === '#';
692701
}
693702

703+
private function isCurrentLineLastLineInDocument()
704+
{
705+
return ($this->offset + $this->currentLineNb) >= ($this->totalNumberOfLines - 1);
706+
}
707+
694708
/**
695709
* Cleanups a YAML string to be parsed.
696710
*
@@ -768,7 +782,7 @@ private function isNextLineUnIndentedCollection()
768782
*/
769783
private function isStringUnIndentedCollectionItem()
770784
{
771-
return 0 === strpos($this->currentLine, '- ');
785+
return '-' === rtrim($this->currentLine) || 0 === strpos($this->currentLine, '- ');
772786
}
773787

774788
/**

Tests/ParserTest.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ public function testShortcutKeyUnindentedCollectionException()
596596

597597
/**
598598
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
599-
* @expectedExceptionMessage Multiple documents are not supported.
599+
* @expectedExceptionMessageRegExp /^Multiple documents are not supported.+/
600600
*/
601601
public function testMultipleDocumentsNotSupportedException()
602602
{
@@ -628,6 +628,34 @@ public function testSequenceInAMapping()
628628
);
629629
}
630630

631+
public function testSequenceInMappingStartedBySingleDashLine()
632+
{
633+
$yaml = <<<EOT
634+
a:
635+
-
636+
b:
637+
-
638+
bar: baz
639+
- foo
640+
d: e
641+
EOT;
642+
$expected = array(
643+
'a' => array(
644+
array(
645+
'b' => array(
646+
array(
647+
'bar' => 'baz',
648+
),
649+
),
650+
),
651+
'foo',
652+
),
653+
'd' => 'e',
654+
);
655+
656+
$this->assertSame($expected, $this->parser->parse($yaml));
657+
}
658+
631659
/**
632660
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
633661
*/
@@ -993,6 +1021,7 @@ public function getCommentLikeStringInScalarBlockData()
9931021
foo
9941022
# bar
9951023
baz
1024+
9961025
EOT
9971026
,
9981027
),
@@ -1021,7 +1050,7 @@ public function getCommentLikeStringInScalarBlockData()
10211050
$expected = array(
10221051
'foo' => array(
10231052
'bar' => array(
1024-
'scalar-block' => 'line1 line2>',
1053+
'scalar-block' => "line1 line2>\n",
10251054
),
10261055
'baz' => array(
10271056
'foobar' => null,

0 commit comments

Comments
 (0)