1616use Symfony \Component \Config \Definition \ArrayNode ;
1717use Symfony \Component \Config \Definition \EnumNode ;
1818use Symfony \Component \Config \Definition \PrototypedArrayNode ;
19+ use Symfony \Component \Config \Definition \ScalarNode ;
1920use 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}
0 commit comments