12
12
namespace Symfony \Component \Config \Builder ;
13
13
14
14
use Symfony \Component \Config \Definition \ArrayNode ;
15
+ use Symfony \Component \Config \Definition \BaseNode ;
15
16
use Symfony \Component \Config \Definition \BooleanNode ;
16
17
use Symfony \Component \Config \Definition \ConfigurationInterface ;
17
18
use Symfony \Component \Config \Definition \EnumNode ;
@@ -125,14 +126,19 @@ private function buildNode(NodeInterface $node, ClassBuilder $class, string $nam
125
126
126
127
private function handleArrayNode (ArrayNode $ node , ClassBuilder $ class , string $ namespace ): void
127
128
{
129
+ $ comment = $ this ->getComment ($ node );
130
+ if ('' !== $ comment ) {
131
+ $ comment = "/** \n$ comment*/ \n" ;
132
+ }
133
+
128
134
$ childClass = new ClassBuilder ($ namespace , $ node ->getName ());
129
135
$ childClass ->setAllowExtraKeys ($ node ->shouldIgnoreExtraKeys ());
130
136
$ class ->addRequire ($ childClass );
131
137
$ this ->classes [] = $ childClass ;
132
138
133
139
$ property = $ class ->addProperty ($ node ->getName (), $ childClass ->getFqcn ());
134
140
$ body = '
135
- public function NAME(array $value = []): CLASS
141
+ COMMENTpublic function NAME(array $value = []): CLASS
136
142
{
137
143
if (null === $this->PROPERTY) {
138
144
$this->PROPERTY = new CLASS($value);
@@ -143,7 +149,7 @@ public function NAME(array $value = []): CLASS
143
149
return $this->PROPERTY;
144
150
} ' ;
145
151
$ class ->addUse (InvalidConfigurationException::class);
146
- $ class ->addMethod ($ node ->getName (), $ body , ['PROPERTY ' => $ property ->getName (), 'CLASS ' => $ childClass ->getFqcn ()]);
152
+ $ class ->addMethod ($ node ->getName (), $ body , ['COMMENT ' => $ comment , ' PROPERTY ' => $ property ->getName (), 'CLASS ' => $ childClass ->getFqcn ()]);
147
153
148
154
$ this ->buildNode ($ node , $ childClass , $ this ->getSubNamespace ($ childClass ));
149
155
}
@@ -220,16 +226,21 @@ public function NAME(string $VAR, TYPE $VALUE): static
220
226
$ this ->classes [] = $ childClass ;
221
227
$ property = $ class ->addProperty ($ node ->getName (), $ childClass ->getFqcn ().'[] ' );
222
228
229
+ $ comment = $ this ->getComment ($ node );
230
+ if ('' !== $ comment ) {
231
+ $ comment = "/** \n$ comment*/ \n" ;
232
+ }
233
+
223
234
if (null === $ key = $ node ->getKeyAttribute ()) {
224
235
$ body = '
225
- public function NAME(array $value = []): CLASS
236
+ COMMENTpublic function NAME(array $value = []): CLASS
226
237
{
227
238
return $this->PROPERTY[] = new CLASS($value);
228
239
} ' ;
229
- $ class ->addMethod ($ methodName , $ body , ['PROPERTY ' => $ property ->getName (), 'CLASS ' => $ childClass ->getFqcn ()]);
240
+ $ class ->addMethod ($ methodName , $ body , ['COMMENT ' => $ comment , ' PROPERTY ' => $ property ->getName (), 'CLASS ' => $ childClass ->getFqcn ()]);
230
241
} else {
231
242
$ body = '
232
- public function NAME(string $VAR, array $VALUE = []): CLASS
243
+ COMMENTpublic function NAME(string $VAR, array $VALUE = []): CLASS
233
244
{
234
245
if (!isset($this->PROPERTY[$VAR])) {
235
246
return $this->PROPERTY[$VAR] = new CLASS($value);
@@ -241,7 +252,7 @@ public function NAME(string $VAR, array $VALUE = []): CLASS
241
252
throw new InvalidConfigurationException( \'The node created by "NAME()" has already been initialized. You cannot pass values the second time you call NAME(). \');
242
253
} ' ;
243
254
$ class ->addUse (InvalidConfigurationException::class);
244
- $ class ->addMethod ($ methodName , $ body , ['PROPERTY ' => $ property ->getName (), 'CLASS ' => $ childClass ->getFqcn (), 'VAR ' => '' === $ key ? 'key ' : $ key , 'VALUE ' => 'value ' === $ key ? 'data ' : 'value ' ]);
255
+ $ class ->addMethod ($ methodName , $ body , ['COMMENT ' => $ comment , ' PROPERTY ' => $ property ->getName (), 'CLASS ' => $ childClass ->getFqcn (), 'VAR ' => '' === $ key ? 'key ' : $ key , 'VALUE ' => 'value ' === $ key ? 'data ' : 'value ' ]);
245
256
}
246
257
247
258
$ this ->buildNode ($ prototype , $ childClass , $ namespace .'\\' .$ childClass ->getName ());
@@ -298,31 +309,41 @@ private function getParameterType(NodeInterface $node): ?string
298
309
return null ;
299
310
}
300
311
301
- private function getComment (VariableNode $ node ): string
312
+ private function getComment (BaseNode $ node ): string
302
313
{
303
314
$ comment = '' ;
304
315
if ('' !== $ info = (string ) $ node ->getInfo ()) {
305
316
$ comment .= ' * ' .$ info ."\n" ;
306
317
}
307
318
308
- foreach ((array ) ($ node ->getExample () ?? []) as $ example ) {
309
- $ comment .= ' * @example ' .$ example ."\n" ;
310
- }
319
+ if (!$ node instanceof ArrayNode) {
320
+ foreach ((array ) ($ node ->getExample () ?? []) as $ example ) {
321
+ $ comment .= ' * @example ' .$ example ."\n" ;
322
+ }
311
323
312
- if ('' !== $ default = $ node ->getDefaultValue ()) {
313
- $ comment .= ' * @default ' .(null === $ default ? 'null ' : var_export ($ default , true ))."\n" ;
314
- }
324
+ if ('' !== $ default = $ node ->getDefaultValue ()) {
325
+ $ comment .= ' * @default ' .(null === $ default ? 'null ' : var_export ($ default , true ))."\n" ;
326
+ }
315
327
316
- if ($ node instanceof EnumNode) {
317
- $ comment .= sprintf (' * @param ParamConfigurator|%s $value ' , implode ('| ' , array_map (function ($ a ) {
318
- return var_export ($ a , true );
319
- }, $ node ->getValues ())))."\n" ;
328
+ if ($ node instanceof EnumNode) {
329
+ $ comment .= sprintf (' * @param ParamConfigurator|%s $value ' , implode ('| ' , array_map (function ($ a ) {
330
+ return var_export ($ a , true );
331
+ }, $ node ->getValues ())))."\n" ;
332
+ } else {
333
+ $ parameterType = $ this ->getParameterType ($ node );
334
+ if (null === $ parameterType || '' === $ parameterType ) {
335
+ $ parameterType = 'mixed ' ;
336
+ }
337
+ $ comment .= ' * @param ParamConfigurator| ' .$ parameterType .' $value ' ."\n" ;
338
+ }
320
339
} else {
321
- $ parameterType = $ this ->getParameterType ($ node );
322
- if (null === $ parameterType || '' === $ parameterType ) {
323
- $ parameterType = 'mixed ' ;
340
+ foreach ((array ) ($ node ->getExample () ?? []) as $ example ) {
341
+ $ comment .= ' * @example ' .json_encode ($ example )."\n" ;
342
+ }
343
+
344
+ if ($ node ->hasDefaultValue () && [] != $ default = $ node ->getDefaultValue ()) {
345
+ $ comment .= ' * @default ' .json_encode ($ default )."\n" ;
324
346
}
325
- $ comment .= ' * @param ParamConfigurator| ' .$ parameterType .' $value ' ."\n" ;
326
347
}
327
348
328
349
if ($ node ->isDeprecated ()) {
0 commit comments