16
16
use Symfony \Component \Config \Definition \ArrayNode ;
17
17
use Symfony \Component \Config \Definition \EnumNode ;
18
18
use Symfony \Component \Config \Definition \PrototypedArrayNode ;
19
+ use Symfony \Component \Config \Definition \ScalarNode ;
19
20
use Symfony \Component \Yaml \Inline ;
20
21
21
22
/**
@@ -45,8 +46,9 @@ public function dumpNode(NodeInterface $node)
45
46
/**
46
47
* @param NodeInterface $node
47
48
* @param int $depth
49
+ * @param bool $prototypedArray
48
50
*/
49
- private function writeNode (NodeInterface $ node , $ depth = 0 )
51
+ private function writeNode (NodeInterface $ node , $ depth = 0 , $ prototypedArray = false )
50
52
{
51
53
$ comments = array ();
52
54
$ default = '' ;
@@ -59,29 +61,7 @@ private function writeNode(NodeInterface $node, $depth = 0)
59
61
$ children = $ node ->getChildren ();
60
62
61
63
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 );
85
65
}
86
66
87
67
if (!$ children ) {
@@ -125,7 +105,8 @@ private function writeNode(NodeInterface $node, $depth = 0)
125
105
$ default = (string ) $ default != '' ? ' ' .$ default : '' ;
126
106
$ comments = count ($ comments ) ? '# ' .implode (', ' , $ comments ) : '' ;
127
107
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 ), ' ' );
129
110
130
111
if ($ info = $ node ->getInfo ()) {
131
112
$ this ->writeLine ('' );
@@ -159,7 +140,7 @@ private function writeNode(NodeInterface $node, $depth = 0)
159
140
160
141
if ($ children ) {
161
142
foreach ($ children as $ childNode ) {
162
- $ this ->writeNode ($ childNode , $ depth + 1 );
143
+ $ this ->writeNode ($ childNode , $ depth + 1 , $ node instanceof PrototypedArrayNode && ! $ node -> getKeyAttribute () );
163
144
}
164
145
}
165
146
}
@@ -200,4 +181,38 @@ private function writeArray(array $array, $depth)
200
181
}
201
182
}
202
183
}
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
+ }
203
218
}
0 commit comments