Skip to content

Commit 1c1117c

Browse files
committed
Merge branch '2.7' into 2.8
* 2.7: don't keep internal state between parser runs Add \Traversable typehint to phpdoc [ExpressionLanguage] Avoid dependency on ctype [Debug] Fix php notice
2 parents 286d848 + 49c2554 commit 1c1117c

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
@@ -64,23 +64,51 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
6464
if (false === preg_match('//u', $value)) {
6565
throw new ParseException('The YAML value does not appear to be valid UTF-8.');
6666
}
67+
68+
$this->refs = array();
69+
70+
$mbEncoding = null;
71+
$e = null;
72+
$data = null;
73+
74+
if (2 /* MB_OVERLOAD_STRING */ & (int) ini_get('mbstring.func_overload')) {
75+
$mbEncoding = mb_internal_encoding();
76+
mb_internal_encoding('UTF-8');
77+
}
78+
79+
try {
80+
$data = $this->doParse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap);
81+
} catch (\Exception $e) {
82+
} catch (\Throwable $e) {
83+
}
84+
85+
if (null !== $mbEncoding) {
86+
mb_internal_encoding($mbEncoding);
87+
}
88+
89+
if (null !== $e) {
90+
throw $e;
91+
}
92+
93+
return $data;
94+
}
95+
96+
private function doParse($value, $exceptionOnInvalidType = false, $objectSupport = false, $objectForMap = false)
97+
{
6798
$this->currentLineNb = -1;
6899
$this->currentLine = '';
69100
$value = $this->cleanup($value);
70101
$this->lines = explode("\n", $value);
102+
$this->locallySkippedLineNumbers = array();
71103

72104
if (null === $this->totalNumberOfLines) {
73105
$this->totalNumberOfLines = count($this->lines);
74106
}
75107

76-
if (2 /* MB_OVERLOAD_STRING */ & (int) ini_get('mbstring.func_overload')) {
77-
$mbEncoding = mb_internal_encoding();
78-
mb_internal_encoding('UTF-8');
79-
}
80-
81108
$data = array();
82109
$context = null;
83110
$allowOverwrite = false;
111+
84112
while ($this->moveToNextLine()) {
85113
if ($this->isCurrentLineEmpty()) {
86114
continue;
@@ -246,21 +274,13 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
246274
throw $e;
247275
}
248276

249-
if (isset($mbEncoding)) {
250-
mb_internal_encoding($mbEncoding);
251-
}
252-
253277
return $value;
254278
}
255279

256280
throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
257281
}
258282
}
259283

260-
if (isset($mbEncoding)) {
261-
mb_internal_encoding($mbEncoding);
262-
}
263-
264284
if ($objectForMap && !is_object($data) && 'mapping' === $context) {
265285
$object = new \stdClass();
266286

@@ -289,7 +309,7 @@ private function parseBlock($offset, $yaml, $exceptionOnInvalidType, $objectSupp
289309
$parser = new self($offset, $this->totalNumberOfLines, $skippedLineNumbers);
290310
$parser->refs = &$this->refs;
291311

292-
return $parser->parse($yaml, $exceptionOnInvalidType, $objectSupport, $objectForMap);
312+
return $parser->doParse($yaml, $exceptionOnInvalidType, $objectSupport, $objectForMap);
293313
}
294314

295315
/**

Tests/ParserTest.php

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

12321232
$this->assertEquals($trickyVal, $arrayFromYaml);
12331233
}
1234+
1235+
/**
1236+
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
1237+
* @expectedExceptionMessage Reference "foo" does not exist at line 2
1238+
*/
1239+
public function testParserCleansUpReferencesBetweenRuns()
1240+
{
1241+
$yaml = <<<YAML
1242+
foo: &foo
1243+
baz: foobar
1244+
bar:
1245+
<<: *foo
1246+
YAML;
1247+
$this->parser->parse($yaml);
1248+
1249+
$yaml = <<<YAML
1250+
bar:
1251+
<<: *foo
1252+
YAML;
1253+
$this->parser->parse($yaml);
1254+
}
12341255
}
12351256

12361257
class B

0 commit comments

Comments
 (0)