29
29
*/
30
30
class Generator implements GeneratorInterface
31
31
{
32
- private $ classes = [] ;
32
+ private $ classes ;
33
33
34
34
/**
35
35
* @return \Closure that will return the root config class
@@ -39,8 +39,9 @@ public function build(ConfigurationInterface $configuration, string $outputDir):
39
39
$ this ->classes = [];
40
40
41
41
$ rootNode = $ configuration ->getConfigTreeBuilder ()->buildTree ();
42
- $ rootClass = new ClassBuilder ($ namespace = 'Config \\' .ucfirst (preg_replace ('#\W# ' , '' , $ rootNode ->getPath ())), $ rootNode ->getName ());
43
- $ this ->classes [$ rootNode ->getPath ()] = $ rootClass ;
42
+ $ namespace = 'Config \\' .ucfirst (preg_replace ('#\W# ' , '' , $ rootNode ->getPath ()));
43
+ $ rootClass = new ClassBuilder ($ namespace , $ rootNode ->getName ());
44
+ $ this ->classes [] = $ rootClass ;
44
45
45
46
$ this ->buildNode ($ rootNode , $ rootClass , $ namespace );
46
47
@@ -71,7 +72,7 @@ private function writeClasses(string $outputDir)
71
72
private function buildNode (NodeInterface $ node , ClassBuilder $ class , string $ namespace )
72
73
{
73
74
if (!$ node instanceof ArrayNode) {
74
- throw new \LogicException ('The node was expected to be an array node . ' );
75
+ throw new \LogicException ('The node was expected to be an ArrayNode. This Configuration includes an edge case not supported yet . ' );
75
76
}
76
77
77
78
foreach ($ node ->getChildren () as $ child ) {
@@ -98,15 +99,15 @@ private function handleArrayNode(ArrayNode $node, ClassBuilder $class, string $n
98
99
{
99
100
$ childClass = new ClassBuilder ($ namespace , $ node ->getName ());
100
101
$ class ->addRequire ($ childClass );
101
- $ this ->classes [$ node -> getPath () ] = $ childClass ;
102
+ $ this ->classes [] = $ childClass ;
102
103
103
104
$ property = $ class ->addProperty ($ node ->getName (), $ childClass ->getName ());
104
105
$ body = '
105
- public function NAME(array $value = []): OUT
106
+ public function NAME(array $value = []): CLASS
106
107
{
107
108
return $this->PROPERTY = new CLASS($value);
108
109
} ' ;
109
- $ class ->addMethod ('add_ ' .$ node ->getName (), $ body , ['PROPERTY ' => $ property ->getName (), 'CLASS ' => $ childClass ->getFqcn (), ' OUT ' => $ childClass -> getFqcn () ]);
110
+ $ class ->addMethod ('add_ ' .$ node ->getName (), $ body , ['PROPERTY ' => $ property ->getName (), 'CLASS ' => $ childClass ->getFqcn ()]);
110
111
111
112
$ this ->buildNode ($ node , $ childClass , $ namespace .'\\' .$ childClass ->getName ());
112
113
}
@@ -130,20 +131,7 @@ public function NAME($value): self
130
131
131
132
private function handlePrototypedArrayNode (PrototypedArrayNode $ node , ClassBuilder $ class , string $ namespace )
132
133
{
133
- /*
134
- * Pick a good singular name
135
- */
136
- $ name = $ node ->getName ();
137
- if ('s ' === substr ($ name , -1 )) {
138
- $ mappings = $ node ->getParent () instanceof ArrayNode ? $ node ->getParent ()->getXmlRemappings () : [];
139
- foreach ($ mappings as $ map ) {
140
- if ($ map [1 ] === $ name ) {
141
- $ name = $ map [0 ];
142
- break ;
143
- }
144
- }
145
- }
146
-
134
+ $ name = $ this ->getSingularName ($ node );
147
135
$ prototype = $ node ->getPrototype ();
148
136
$ methodName = 'add_ ' .$ name ;
149
137
@@ -175,7 +163,7 @@ public function NAME(string $key, TYPE $value): self
175
163
176
164
$ childClass = new ClassBuilder ($ namespace , $ name );
177
165
$ class ->addRequire ($ childClass );
178
- $ this ->classes [$ node -> getPath () ] = $ childClass ;
166
+ $ this ->classes [] = $ childClass ;
179
167
$ property = $ class ->addProperty ($ node ->getName (), $ childClass ->getName ().'[] ' );
180
168
181
169
if (null === $ node ->getKeyAttribute ()) {
@@ -269,4 +257,26 @@ private function getComment(VariableNode $node): string
269
257
270
258
return $ comment ;
271
259
}
260
+
261
+ /**
262
+ * Pick a good singular name
263
+ */
264
+ private function getSingularName (PrototypedArrayNode $ node ): string
265
+ {
266
+ $ name = $ node ->getName ();
267
+ if ('s ' !== substr ($ name , -1 )) {
268
+ return $ name ;
269
+ }
270
+
271
+ $ parent = $ node ->getParent ();
272
+ $ mappings = $ parent instanceof ArrayNode ? $ parent ->getXmlRemappings () : [];
273
+ foreach ($ mappings as $ map ) {
274
+ if ($ map [1 ] === $ name ) {
275
+ $ name = $ map [0 ];
276
+ break ;
277
+ }
278
+ }
279
+
280
+ return $ name ;
281
+ }
272
282
}
0 commit comments