Skip to content

Commit db2e2c8

Browse files
committed
feature #19570 [Config] Fix YamlReferenceDumper prototyped array support (ogizanagi)
This PR was merged into the 3.2-dev branch. Discussion ---------- [Config] Fix YamlReferenceDumper prototyped array support | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Also related to #19480 which fixes another prototype issue, but cannot be tested properly on Travis because marked as skipped by this missing implementation. Previous output was: ```yaml [...] parameters: # Prototype name: ~ connections: user: ~ pass: ~ ``` instead of: ```yaml [...] parameters: # Prototype name: ~ connections: # Prototype - user: ~ pass: ~ ``` Commits ------- 063a980 [Config] Fix YamlReferenceDumper prototyped array support
2 parents 7136247 + 063a980 commit db2e2c8

File tree

5 files changed

+56
-29
lines changed

5 files changed

+56
-29
lines changed

src/Symfony/Component/Config/Definition/Dumper/YamlReferenceDumper.php

+41-26
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Config\Definition\ArrayNode;
1717
use Symfony\Component\Config\Definition\EnumNode;
1818
use Symfony\Component\Config\Definition\PrototypedArrayNode;
19+
use Symfony\Component\Config\Definition\ScalarNode;
1920
use Symfony\Component\Yaml\Inline;
2021

2122
/**
@@ -45,8 +46,9 @@ public function dumpNode(NodeInterface $node)
4546
/**
4647
* @param NodeInterface $node
4748
* @param int $depth
49+
* @param bool $prototypedArray
4850
*/
49-
private function writeNode(NodeInterface $node, $depth = 0)
51+
private function writeNode(NodeInterface $node, $depth = 0, $prototypedArray = false)
5052
{
5153
$comments = array();
5254
$default = '';
@@ -59,29 +61,7 @@ private function writeNode(NodeInterface $node, $depth = 0)
5961
$children = $node->getChildren();
6062

6163
if ($node instanceof PrototypedArrayNode) {
62-
$prototype = $node->getPrototype();
63-
64-
if ($prototype instanceof ArrayNode) {
65-
$children = $prototype->getChildren();
66-
}
67-
68-
// check for attribute as key
69-
if ($key = $node->getKeyAttribute()) {
70-
$keyNodeClass = 'Symfony\Component\Config\Definition\\'.($prototype instanceof ArrayNode ? 'ArrayNode' : 'ScalarNode');
71-
$keyNode = new $keyNodeClass($key, $node);
72-
73-
$info = 'Prototype';
74-
if (null !== $prototype->getInfo()) {
75-
$info .= ': '.$prototype->getInfo();
76-
}
77-
$keyNode->setInfo($info);
78-
79-
// add children
80-
foreach ($children as $childNode) {
81-
$keyNode->addChild($childNode);
82-
}
83-
$children = array($key => $keyNode);
84-
}
64+
$children = $this->getPrototypeChildren($node);
8565
}
8666

8767
if (!$children) {
@@ -125,7 +105,8 @@ private function writeNode(NodeInterface $node, $depth = 0)
125105
$default = (string) $default != '' ? ' '.$default : '';
126106
$comments = count($comments) ? '# '.implode(', ', $comments) : '';
127107

128-
$text = rtrim(sprintf('%-20s %s %s', $node->getName().':', $default, $comments), ' ');
108+
$key = $prototypedArray ? '-' : $node->getName().':';
109+
$text = rtrim(sprintf('%-20s %s %s', $key, $default, $comments), ' ');
129110

130111
if ($info = $node->getInfo()) {
131112
$this->writeLine('');
@@ -159,7 +140,7 @@ private function writeNode(NodeInterface $node, $depth = 0)
159140

160141
if ($children) {
161142
foreach ($children as $childNode) {
162-
$this->writeNode($childNode, $depth + 1);
143+
$this->writeNode($childNode, $depth + 1, $node instanceof PrototypedArrayNode && !$node->getKeyAttribute());
163144
}
164145
}
165146
}
@@ -200,4 +181,38 @@ private function writeArray(array $array, $depth)
200181
}
201182
}
202183
}
184+
185+
/**
186+
* @param PrototypedArrayNode $node
187+
*
188+
* @return array
189+
*/
190+
private function getPrototypeChildren(PrototypedArrayNode $node)
191+
{
192+
$prototype = $node->getPrototype();
193+
$key = $node->getKeyAttribute();
194+
195+
// Do not expand prototype if it isn't an array node nor uses attribute as key
196+
if (!$key && !$prototype instanceof ArrayNode) {
197+
return $node->getChildren();
198+
}
199+
200+
if ($prototype instanceof ArrayNode) {
201+
$keyNode = new ArrayNode($key, $node);
202+
// add children
203+
foreach ($prototype->getChildren() as $childNode) {
204+
$keyNode->addChild($childNode);
205+
}
206+
} else {
207+
$keyNode = new ScalarNode($key, $node);
208+
}
209+
210+
$info = 'Prototype';
211+
if (null !== $prototype->getInfo()) {
212+
$info .= ': '.$prototype->getInfo();
213+
}
214+
$keyNode->setInfo($info);
215+
216+
return array($key => $keyNode);
217+
}
203218
}

src/Symfony/Component/Config/Tests/Definition/Dumper/XmlReferenceDumperTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ enum=""
6666
child3=""
6767
/>
6868
69+
<!-- prototype -->
70+
<scalar-prototyped>scalar value</scalar-prototyped>
71+
6972
<!-- prototype: Parameter name -->
7073
<parameter name="parameter name">scalar value</parameter>
7174

src/Symfony/Component/Config/Tests/Definition/Dumper/YamlReferenceDumperTest.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public function testDumper()
2222

2323
$dumper = new YamlReferenceDumper();
2424

25-
$this->markTestIncomplete('The Yaml Dumper currently does not support prototyped arrays');
2625
$this->assertEquals($this->getConfigurationAsString(), $dumper->dump($configuration));
2726
}
2827

@@ -32,7 +31,7 @@ private function getConfigurationAsString()
3231
acme_root:
3332
boolean: true
3433
scalar_empty: ~
35-
scalar_null: ~
34+
scalar_null: null
3635
scalar_true: true
3736
scalar_false: false
3837
scalar_default: default
@@ -55,13 +54,17 @@ enum: ~ # One of "this"; "that"
5554
# multi-line info text
5655
# which should be indented
5756
child3: ~ # Example: example setting
57+
scalar_prototyped: []
5858
parameters:
5959
6060
# Prototype: Parameter name
6161
name: ~
6262
connections:
63+
6364
# Prototype
64-
- { user: ~, pass: ~ }
65+
-
66+
user: ~
67+
pass: ~
6568

6669
EOL;
6770
}

src/Symfony/Component/Config/Tests/Fixtures/Configuration/ExampleConfiguration.php

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public function getConfigTreeBuilder()
5252
->end()
5353
->end()
5454
->end()
55+
->arrayNode('scalar_prototyped')
56+
->prototype('scalar')->end()
57+
->end()
5558
->arrayNode('parameters')
5659
->useAttributeAsKey('name')
5760
->prototype('scalar')->info('Parameter name')->end()

src/Symfony/Component/Config/composer.json

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
"php": ">=5.5.9",
2020
"symfony/filesystem": "~2.8|~3.0"
2121
},
22+
"require-dev": {
23+
"symfony/yaml": "~3.0"
24+
},
2225
"suggest": {
2326
"symfony/yaml": "To use the yaml reference dumper"
2427
},

0 commit comments

Comments
 (0)