Skip to content

Commit 192c211

Browse files
committed
Merge branch '3.2'
* 3.2: don't keep internal state between parser runs Add \Traversable typehint to phpdoc [ExpressionLanguage] Avoid dependency on ctype Moved ->setDate() before the deprecation handling. [Debug] Fix php notice [VarDumper] Relax tests to adapt for php 7.1rc4
2 parents 6f50dd7 + fc84720 commit 192c211

File tree

2 files changed

+54
-15
lines changed

2 files changed

+54
-15
lines changed

Parser.php

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,47 @@ public function parse($value, $flags = 0)
8989
if (false === preg_match('//u', $value)) {
9090
throw new ParseException('The YAML value does not appear to be valid UTF-8.');
9191
}
92+
93+
$this->refs = array();
94+
95+
$mbEncoding = null;
96+
$e = null;
97+
$data = null;
98+
99+
if (2 /* MB_OVERLOAD_STRING */ & (int) ini_get('mbstring.func_overload')) {
100+
$mbEncoding = mb_internal_encoding();
101+
mb_internal_encoding('UTF-8');
102+
}
103+
104+
try {
105+
$data = $this->doParse($value, $flags);
106+
} catch (\Exception $e) {
107+
} catch (\Throwable $e) {
108+
}
109+
110+
if (null !== $mbEncoding) {
111+
mb_internal_encoding($mbEncoding);
112+
}
113+
114+
if (null !== $e) {
115+
throw $e;
116+
}
117+
118+
return $data;
119+
}
120+
121+
private function doParse($value, $flags)
122+
{
92123
$this->currentLineNb = -1;
93124
$this->currentLine = '';
94125
$value = $this->cleanup($value);
95126
$this->lines = explode("\n", $value);
127+
$this->locallySkippedLineNumbers = array();
96128

97129
if (null === $this->totalNumberOfLines) {
98130
$this->totalNumberOfLines = count($this->lines);
99131
}
100132

101-
if (2 /* MB_OVERLOAD_STRING */ & (int) ini_get('mbstring.func_overload')) {
102-
$mbEncoding = mb_internal_encoding();
103-
mb_internal_encoding('UTF-8');
104-
}
105-
106133
if (!$this->moveToNextLine()) {
107134
return null;
108135
}
@@ -324,10 +351,6 @@ public function parse($value, $flags = 0)
324351
throw $e;
325352
}
326353

327-
if (isset($mbEncoding)) {
328-
mb_internal_encoding($mbEncoding);
329-
}
330-
331354
return $value;
332355
}
333356

@@ -375,10 +398,6 @@ public function parse($value, $flags = 0)
375398
$data = new TaggedValue($tag, $data);
376399
}
377400

378-
if (isset($mbEncoding)) {
379-
mb_internal_encoding($mbEncoding);
380-
}
381-
382401
if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && !is_object($data) && 'mapping' === $context) {
383402
$object = new \stdClass();
384403

@@ -408,10 +427,9 @@ private function parseBlock($offset, $yaml, $flags)
408427
$parser->offset = $offset;
409428
$parser->totalNumberOfLines = $this->totalNumberOfLines;
410429
$parser->skippedLineNumbers = $skippedLineNumbers;
411-
412430
$parser->refs = &$this->refs;
413431

414-
return $parser->parse($yaml, $flags);
432+
return $parser->doParse($yaml, $flags);
415433
}
416434

417435
/**

Tests/ParserTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,6 +1752,27 @@ public function testCanParseVeryLongValue()
17521752

17531753
$this->assertEquals($trickyVal, $arrayFromYaml);
17541754
}
1755+
1756+
/**
1757+
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
1758+
* @expectedExceptionMessage Reference "foo" does not exist at line 2
1759+
*/
1760+
public function testParserCleansUpReferencesBetweenRuns()
1761+
{
1762+
$yaml = <<<YAML
1763+
foo: &foo
1764+
baz: foobar
1765+
bar:
1766+
<<: *foo
1767+
YAML;
1768+
$this->parser->parse($yaml);
1769+
1770+
$yaml = <<<YAML
1771+
bar:
1772+
<<: *foo
1773+
YAML;
1774+
$this->parser->parse($yaml);
1775+
}
17551776
}
17561777

17571778
class B

0 commit comments

Comments
 (0)