15
15
use Symfony \Component \Config \Definition \BooleanNode ;
16
16
use Symfony \Component \Config \Definition \ConfigurationInterface ;
17
17
use Symfony \Component \Config \Definition \EnumNode ;
18
+ use Symfony \Component \Config \Definition \Exception \InvalidConfigurationException ;
18
19
use Symfony \Component \Config \Definition \FloatNode ;
19
20
use Symfony \Component \Config \Definition \IntegerNode ;
20
21
use Symfony \Component \Config \Definition \NodeInterface ;
21
22
use Symfony \Component \Config \Definition \PrototypedArrayNode ;
22
23
use Symfony \Component \Config \Definition \ScalarNode ;
23
24
use Symfony \Component \Config \Definition \VariableNode ;
25
+ use Symfony \Component \Config \Loader \ParamConfigurator ;
24
26
25
27
/**
26
28
* Generate ConfigBuilders to help create valid config.
@@ -83,7 +85,7 @@ private function getFullPath(ClassBuilder $class): string
83
85
return $ directory .\DIRECTORY_SEPARATOR .$ class ->getFilename ();
84
86
}
85
87
86
- private function writeClasses ()
88
+ private function writeClasses (): void
87
89
{
88
90
foreach ($ this ->classes as $ class ) {
89
91
$ this ->buildConstructor ($ class );
@@ -95,7 +97,7 @@ private function writeClasses()
95
97
$ this ->classes = [];
96
98
}
97
99
98
- private function buildNode (NodeInterface $ node , ClassBuilder $ class , string $ namespace )
100
+ private function buildNode (NodeInterface $ node , ClassBuilder $ class , string $ namespace ): void
99
101
{
100
102
if (!$ node instanceof ArrayNode) {
101
103
throw new \LogicException ('The node was expected to be an ArrayNode. This Configuration includes an edge case not supported yet. ' );
@@ -121,7 +123,7 @@ private function buildNode(NodeInterface $node, ClassBuilder $class, string $nam
121
123
}
122
124
}
123
125
124
- private function handleArrayNode (ArrayNode $ node , ClassBuilder $ class , string $ namespace )
126
+ private function handleArrayNode (ArrayNode $ node , ClassBuilder $ class , string $ namespace ): void
125
127
{
126
128
$ childClass = new ClassBuilder ($ namespace , $ node ->getName ());
127
129
$ class ->addRequire ($ childClass );
@@ -134,20 +136,22 @@ public function NAME(array $value = []): CLASS
134
136
if (null === $this->PROPERTY) {
135
137
$this->PROPERTY = new CLASS($value);
136
138
} elseif ([] !== $value) {
137
- throw new \Symfony\Component\Config\Definition\Exception\ InvalidConfigurationException(sprintf( \'The node created by "NAME()" has already been initialized. You cannot pass values the second time you call NAME(). \'));
139
+ throw new InvalidConfigurationException(sprintf( \'The node created by "NAME()" has already been initialized. You cannot pass values the second time you call NAME(). \'));
138
140
}
139
141
140
142
return $this->PROPERTY;
141
143
} ' ;
144
+ $ class ->addUse (InvalidConfigurationException::class);
142
145
$ class ->addMethod ($ node ->getName (), $ body , ['PROPERTY ' => $ property ->getName (), 'CLASS ' => $ childClass ->getFqcn ()]);
143
146
144
147
$ this ->buildNode ($ node , $ childClass , $ this ->getSubNamespace ($ childClass ));
145
148
}
146
149
147
- private function handleVariableNode (VariableNode $ node , ClassBuilder $ class )
150
+ private function handleVariableNode (VariableNode $ node , ClassBuilder $ class ): void
148
151
{
149
152
$ comment = $ this ->getComment ($ node );
150
153
$ property = $ class ->addProperty ($ node ->getName ());
154
+ $ class ->addUse (ParamConfigurator::class);
151
155
152
156
$ body = '
153
157
/**
@@ -162,23 +166,24 @@ public function NAME($valueDEFAULT): self
162
166
$ class ->addMethod ($ node ->getName (), $ body , ['PROPERTY ' => $ property ->getName (), 'COMMENT ' => $ comment , 'DEFAULT ' => $ node ->hasDefaultValue () ? ' = ' .var_export ($ node ->getDefaultValue (), true ) : '' ]);
163
167
}
164
168
165
- private function handlePrototypedArrayNode (PrototypedArrayNode $ node , ClassBuilder $ class , string $ namespace )
169
+ private function handlePrototypedArrayNode (PrototypedArrayNode $ node , ClassBuilder $ class , string $ namespace ): void
166
170
{
167
171
$ name = $ this ->getSingularName ($ node );
168
172
$ prototype = $ node ->getPrototype ();
169
173
$ methodName = $ name ;
170
174
171
175
$ parameterType = $ this ->getParameterType ($ prototype );
172
176
if (null !== $ parameterType || $ prototype instanceof ScalarNode) {
177
+ $ class ->addUse (ParamConfigurator::class);
173
178
$ property = $ class ->addProperty ($ node ->getName ());
174
179
if (null === $ key = $ node ->getKeyAttribute ()) {
175
180
// This is an array of values; don't use singular name
176
181
$ body = '
177
182
/**
178
- * @param list<TYPE> $value
183
+ * @param ParamConfigurator| list<TYPE|ParamConfigurator > $value
179
184
* @return $this
180
185
*/
181
- public function NAME(array $value): self
186
+ public function NAME($value): self
182
187
{
183
188
$this->PROPERTY = $value;
184
189
@@ -189,16 +194,17 @@ public function NAME(array $value): self
189
194
} else {
190
195
$ body = '
191
196
/**
197
+ * @param ParamConfigurator|TYPE $value
192
198
* @return $this
193
199
*/
194
- public function NAME(string $VAR, TYPE $VALUE): self
200
+ public function NAME(string $VAR, $VALUE): self
195
201
{
196
202
$this->PROPERTY[$VAR] = $VALUE;
197
203
198
204
return $this;
199
205
} ' ;
200
206
201
- $ class ->addMethod ($ methodName , $ body , ['PROPERTY ' => $ property ->getName (), 'TYPE ' => '' === $ parameterType ? '' : $ parameterType. ' ' , 'VAR ' => '' === $ key ? 'key ' : $ key , 'VALUE ' => 'value ' === $ key ? 'data ' : 'value ' ]);
207
+ $ class ->addMethod ($ methodName , $ body , ['PROPERTY ' => $ property ->getName (), 'TYPE ' => '' === $ parameterType ? 'mixed ' : $ parameterType , 'VAR ' => '' === $ key ? 'key ' : $ key , 'VALUE ' => 'value ' === $ key ? 'data ' : 'value ' ]);
202
208
}
203
209
204
210
return ;
@@ -227,31 +233,33 @@ public function NAME(string $VAR, array $VALUE = []): CLASS
227
233
return $this->PROPERTY[$VAR];
228
234
}
229
235
230
- throw new \Symfony\Component\Config\Definition\Exception\ InvalidConfigurationException(sprintf( \'The node created by "NAME()" has already been initialized. You cannot pass values the second time you call NAME(). \'));
236
+ throw new InvalidConfigurationException(sprintf( \'The node created by "NAME()" has already been initialized. You cannot pass values the second time you call NAME(). \'));
231
237
} ' ;
238
+ $ class ->addUse (InvalidConfigurationException::class);
232
239
$ class ->addMethod ($ methodName , $ body , ['PROPERTY ' => $ property ->getName (), 'CLASS ' => $ childClass ->getFqcn (), 'VAR ' => '' === $ key ? 'key ' : $ key , 'VALUE ' => 'value ' === $ key ? 'data ' : 'value ' ]);
233
240
}
234
241
235
242
$ this ->buildNode ($ prototype , $ childClass , $ namespace .'\\' .$ childClass ->getName ());
236
243
}
237
244
238
- private function handleScalarNode (ScalarNode $ node , ClassBuilder $ class )
245
+ private function handleScalarNode (ScalarNode $ node , ClassBuilder $ class ): void
239
246
{
240
247
$ comment = $ this ->getComment ($ node );
241
248
$ property = $ class ->addProperty ($ node ->getName ());
249
+ $ class ->addUse (ParamConfigurator::class);
242
250
243
251
$ body = '
244
252
/**
245
253
COMMENT * @return $this
246
254
*/
247
- public function NAME(TYPE $value): self
255
+ public function NAME($value): self
248
256
{
249
257
$this->PROPERTY = $value;
250
258
251
259
return $this;
252
260
} ' ;
253
- $ parameterType = $ this -> getParameterType ( $ node ) ?? '' ;
254
- $ class ->addMethod ($ node ->getName (), $ body , ['PROPERTY ' => $ property ->getName (), 'TYPE ' => '' === $ parameterType ? '' : $ parameterType . ' ' , ' COMMENT ' => $ comment ]);
261
+
262
+ $ class ->addMethod ($ node ->getName (), $ body , ['PROPERTY ' => $ property ->getName (), 'COMMENT ' => $ comment ]);
255
263
}
256
264
257
265
private function getParameterType (NodeInterface $ node ): ?string
@@ -301,9 +309,15 @@ private function getComment(VariableNode $node): string
301
309
}
302
310
303
311
if ($ node instanceof EnumNode) {
304
- $ comment .= sprintf (' * @param %s $value ' , implode ('| ' , array_map (function ($ a ) {
312
+ $ comment .= sprintf (' * @param ParamConfigurator| %s $value ' , implode ('| ' , array_map (function ($ a ) {
305
313
return var_export ($ a , true );
306
314
}, $ node ->getValues ()))).\PHP_EOL ;
315
+ } else {
316
+ $ parameterType = $ this ->getParameterType ($ node );
317
+ if (null === $ parameterType || '' === $ parameterType ) {
318
+ $ parameterType = 'mixed ' ;
319
+ }
320
+ $ comment .= ' * @param ParamConfigurator| ' .$ parameterType .' $value ' .\PHP_EOL ;
307
321
}
308
322
309
323
if ($ node ->isDeprecated ()) {
@@ -387,9 +401,10 @@ private function buildConstructor(ClassBuilder $class): void
387
401
388
402
$ body .= '
389
403
if ($value !== []) {
390
- throw new \Symfony\Component\Config\Definition\Exception\ InvalidConfigurationException(sprintf( \'The following keys are not supported by "%s": \', __CLASS__) . implode( \', \', array_keys($value)));
404
+ throw new InvalidConfigurationException(sprintf( \'The following keys are not supported by "%s": \', __CLASS__) . implode( \', \', array_keys($value)));
391
405
} ' ;
392
406
407
+ $ class ->addUse (InvalidConfigurationException::class);
393
408
$ class ->addMethod ('__construct ' , '
394
409
public function __construct(array $value = [])
395
410
{
0 commit comments