10000 merged branch gajdaw/2_1_component_yaml_fix_4022 (PR #4126) · symfony/symfony@e54f4e4 · GitHub
[go: up one dir, main page]

Skip to content

Commit e54f4e4

Browse files
committed
merged branch gajdaw/2_1_component_yaml_fix_4022 (PR #4126)
Commits ------- 80a2a92 [2.1][Component][Yaml] fix 4022 Discussion ---------- [2.1][Component][Yaml] fix 4022 Bug fix: yes Feature addition: no Backwards compatibility break: no Symfony2 tests pass: [![Build Status](https://secure.travis-ci.org/gajdaw/symfony.png?branch=2_1_component_yaml_fix_4022)](http://travis-ci. 10000 org/gajdaw/symfony) Fixes the following tickets: #4121, #4022, #4135 Todo: --------------------------------------------------------------------------- by stof at 2012-04-27T13:03:15Z Why is it marked as ``[2.2]`` if it is a bugfix ? @fabpot ping --------------------------------------------------------------------------- by gajdaw at 2012-04-27T14:42:21Z The title should be [2.1] - now it is correct. I marked it 2.0 and PR was for 2.0 originally. Fabien suggested that it should go to master branch: #4121 (comment) --------------------------------------------------------------------------- by fabpot at 2012-05-07T09:17:31Z That does not work when you have something after the unindented collection: collection: key: - a - b - c foo: bar --------------------------------------------------------------------------- by gajdaw at 2012-05-07T11:11:30Z @fabpot Last commit contains test with your yaml: collection: key: - a - b - c foo: bar Everything seems fine. Can you give me a hint: what do you mean, when you say "That does not work"? --------------------------------------------------------------------------- by fabpot at 2012-05-07T12:36:19Z Sorry, the failing test is the following: test: Key/value after unindented collection brief: > Key/value after unindented collection yaml: | collection: key: - a - b - c foo: bar php: | array('collection' => array('key' => array('a', 'b', 'c'), 'foo' => 'bar')) --------------------------------------------------------------------------- by gajdaw at 2012-05-07T15:48:26Z @fabpot Last commit passed your test. --------------------------------------------------------------------------- by fabpot at 2012-05-07T17:28:21Z Can you squash your commits? Thanks. --------------------------------------------------------------------------- by travisbot at 2012-05-08T05:32:58Z This pull request [fails](http://travis-ci.org/symfony/symfony/builds/1273487) (merged 20891c58 into 919604a). --------------------------------------------------------------------------- by gajdaw at 2012-05-08T05:36:51Z Done. --------------------------------------------------------------------------- by travisbot at 2012-05-08T07:23:47Z This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1274162) (merged 80a2a92 into 898ff4e).
2 parents 898ff4e + 80a2a92 commit e54f4e4

File tree

4 files changed

+138
-2
lines changed

4 files changed

+138
-2
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 55 additions & 2 deletions
573
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public function parse($value)
159159
// hash
160160
} elseif (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
161161
// if next line is less indented or equal, then it means that the current value is null
162-
if ($this->isNextLineIndented()) {
162+
if ($this->isNextLineIndented() && !$this->isNextLineUnIndentedCollection()) {
163163
$data[$key] = null;
164164
} else {
165165
$c = $this->getRealCurrentLineNb() + 1;
@@ -275,7 +275,9 @@ private function getNextEmbedBlock($indentation = null)
275275
if (null === $indentation) {
276276
$newIndent = $this->getCurrentLineIndentation();
277277

278-
if (!$this->isCurrentLineEmpty() && 0 == $newIndent) {
278+
$unindentedEmbedBlock = $this->isStringUnIndentedCollectionItem($this->currentLine);
279+
280+
if (!$this->isCurrentLineEmpty() && 0 === $newIndent && !$unindentedEmbedBlock) {
279281
throw new ParseException('Indentation problem.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
280282
}
281283
} else {
@@ -284,7 +286,15 @@ private function getNextEmbedBlock($indentation = null)
284286

285287
$data = array(substr($this->currentLine, $newIndent));
286288

289+
$isItUnindentedCollection = $this->isStringUnIndentedCollectionItem($this->currentLine);
290+
287291
while ($this->moveToNextLine()) {
292+
293+
if ($isItUnindentedCollection && !$this->isStringUnIndentedCollectionItem($this->currentLine)) {
294+
$this->moveToPreviousLine();
295+
break;
296+
}
297+
288298
if ($this->isCurrentLineEmpty()) {
289299
if ($this->isCurrentLineBlank()) {
290300
$data[] = substr($this->currentLine, $newIndent);
@@ -553,4 +563,47 @@ private function cleanup($value)
553563

554564
return $value;
555565
}
566+
567+
/**
568+
* Returns true if the next line starts unindented collection
569+
*
570+
* @return Boolean Returns true if the next line starts unindented collection, false otherwise
571+
*/
572+
private function isNextLineUnIndentedCollection()
+
{
574+
$currentIndentation = $this->getCurrentLineIndentation();
575+
$notEOF = $this->moveToNextLine();
576+
577+
while ($notEOF && $this->isCurrentLineEmpty()) {
578+
$notEOF = $this->moveToNextLine();
579+
}
580+
581+
if (false === $notEOF) {
582+
return false;
583+
}
584+
585+
$ret = false;
586+
if (
587+
$this->getCurrentLineIndentation() == $currentIndentation
588+
&&
589+
$this->isStringUnIndentedCollectionItem($this->currentLine)
590+
) {
591+
$ret = true;
592+
}
593+
594+
$this->moveToPreviousLine();
595+
596+
return $ret;
597+
}
598+
599+
/**
600+
* Returns true if the string is unindented collection item
601+
*
602+
* @return Boolean Returns true if the string is unindented collection item, false otherwise
603+
*/
604+
private function isStringUnIndentedCollectionItem($string)
605+
{
606+
return (0 === strpos($this->currentLine, '- '));
607+
}
608+
556609
}

src/Symfony/Component/Yaml/Tests/Fixtures/index.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
- YtsNullsAndEmpties
1616
- YtsSpecificationExamples
1717
- YtsTypeTransfers
18+
- unindentedCollections
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
--- %YAML:1.0
2+
test: Unindented collection
3+
brief: >
4+
Unindented collection
5+
yaml: |
6+
collection:
7+
- item1
8+
- item2
9+
- item3
10+
php: |
11+
array('collection' => array('item1', 'item2', 'item3'))
12+
---
13+
test: Nested unindented collection (two levels)
14+
brief: >
15+
Nested unindented collection
16+
yaml: |
17+
collection:
18+
key:
19+
- a
20+
- b
21+
- c
22+
php: |
23+
array('collection' => array('key' => array('a', 'b', 'c')))
24+
---
25+
test: Nested unindented collection (three levels)
26+
brief: > 10000
27+
Nested unindented collection
28+
yaml: |
29+
collection:
30+
key:
31+
subkey:
32+
- one
33+
- two
34+
- three
35+
php: |
36+
array('collection' => array('key' => array('subkey' => array('one', 'two', 'three'))))
37+
---
38+
test: Key/value after unindented collection (1)
39+
brief: >
40+
Key/value after unindented collection (1)
41+
yaml: |
42+
collection:
43+
key:
44+
- a
45+
- b
46+
- c
47+
foo: bar
48+
php: |
49+
array('collection' => array('key' => array('a', 'b', 'c')), 'foo' => 'bar')
50+
---
51+
test: Key/value after unindented collection (at the same level)
52+
brief: >
53+
Key/value after unindented collection
54+
yaml: |
55+
collection:
56+
key:
57+
- a
58+
- b
59+
- c
60+
foo: bar
61+
php: |
62+
array('collection' => array('key' => array('a', 'b', 'c'), 'foo' => 'bar'))

src/Symfony/Component/Yaml/Tests/ParserTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,26 @@ public function testNonUtf8Exception()
140140
}
141141
}
142142
}
143+
144+
/**
145+
*
146+
* @expectedException Symfony\Component\Yaml\Exception\ParseException
147+
*
148+
*/
149+
public function testUnindentedCollectionException()
150+
{
151+
$yaml = <<<EOF
152+
153+
collection:
154+
-item1
155+
-item2
156+
-item3
157+
158+
EOF;
159+
160+
$this->parser->parse($yaml);
161+
}
162+
143163
}
144164

145165
class B

0 commit comments

Comments
 (0)
0