Skip to content

Commit fc84720

Browse files
committed
Merge branch '2.8' into 3.2
* 2.8: don't keep internal state between parser runs Add \Traversable typehint to phpdoc [ExpressionLanguage] Avoid dependency on ctype [Debug] Fix php notice [VarDumper] Relax tests to adapt for php 7.1rc4
2 parents 62b4cdb + 1c1117c commit fc84720

File tree

2 files changed

+55
-14
lines changed

2 files changed

+55
-14
lines changed

Parser.php

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

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

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-
104131
$data = array();
105132
$context = null;
106133
$allowOverwrite = false;
134+
107135
while ($this->moveToNextLine()) {
108136
if ($this->isCurrentLineEmpty()) {
109137
continue;
@@ -279,21 +307,13 @@ public function parse($value, $flags = 0)
279307
throw $e;
280308
}
281309

282-
if (isset($mbEncoding)) {
283-
mb_internal_encoding($mbEncoding);
284-
}
285-
286310
return $value;
287311
}
288312

289313
throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
290314
}
291315
}
292316

293-
if (isset($mbEncoding)) {
294-
mb_internal_encoding($mbEncoding);
295-
}
296-
297317
if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && !is_object($data) && 'mapping' === $context) {
298318
$object = new \stdClass();
299319

@@ -322,7 +342,7 @@ private function parseBlock($offset, $yaml, $flags)
322342
$parser = new self($offset, $this->totalNumberOfLines, $skippedLineNumbers);
323343
$parser->refs = &$this->refs;
324344

325-
return $parser->parse($yaml, $flags);
345+
return $parser->doParse($yaml, $flags);
326346
}
327347

328348
/**

Tests/ParserTest.php

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

14911491
$this->assertEquals($trickyVal, $arrayFromYaml);
14921492
}
1493+
1494+
/**
1495+
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
1496+
* @expectedExceptionMessage Reference "foo" does not exist at line 2
1497+
*/
1498+
public function testParserCleansUpReferencesBetweenRuns()
1499+
{
1500+
$yaml = <<<YAML
1501+
foo: &foo
1502+
baz: foobar
1503+
bar:
1504+
<<: *foo
1505+
YAML;
1506+
$this->parser->parse($yaml);
1507+
1508+
$yaml = <<<YAML
1509+
bar:
1510+
<<: *foo
1511+
YAML;
1512+
$this->parser->parse($yaml);
1513+
}
14931514
}
14941515

14951516
class B

0 commit comments

Comments
 (0)