Skip to content

Commit caa1de3

Browse files
[Config] Generate the array-shape of the current node instead of the whole root node in Config classes
1 parent b666a28 commit caa1de3

File tree

23 files changed

+73
-342
lines changed

23 files changed

+73
-342
lines changed

Builder/ClassBuilder.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Config\Builder;
1313

14+
use Symfony\Component\Config\Definition\NodeInterface;
15+
1416
/**
1517
* Build PHP classes to generate config.
1618
*
@@ -35,6 +37,7 @@ class ClassBuilder
3537
public function __construct(
3638
private string $namespace,
3739
string $name,
40+
private NodeInterface $node,
3841
) {
3942
$this->name = ucfirst($this->camelCase($name)).'Config';
4043
}
@@ -166,4 +169,9 @@ public function shouldAllowExtraKeys(): bool
166169
{
167170
return $this->allowExtraKeys;
168171
}
172+
173+
public function getNode(): NodeInterface
174+
{
175+
return $this->node;
176+
}
169177
}

Builder/ConfigBuilderGenerator.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function build(ConfigurationInterface $configuration): \Closure
5151
$this->classes = [];
5252

5353
$rootNode = $configuration->getConfigTreeBuilder()->buildTree();
54-
$rootClass = new ClassBuilder('Symfony\\Config', $rootNode->getName());
54+
$rootClass = new ClassBuilder('Symfony\\Config', $rootNode->getName(), $rootNode);
5555

5656
$path = $this->getFullPath($rootClass);
5757
if (!is_file($path)) {
@@ -65,7 +65,7 @@ public function NAME(): string
6565
return \'ALIAS\';
6666
}', ['ALIAS' => $rootNode->getPath()]);
6767

68-
$this->writeClasses($rootNode);
68+
$this->writeClasses();
6969
}
7070

7171
return function () use ($path, $rootClass) {
@@ -86,10 +86,10 @@ private function getFullPath(ClassBuilder $class): string
8686
return $directory.\DIRECTORY_SEPARATOR.$class->getFilename();
8787
}
8888

89-
private function writeClasses(NodeInterface $node): void
89+
private function writeClasses(): void
9090
{
9191
foreach ($this->classes as $class) {
92-
$this->buildConstructor($class, $node);
92+
$this->buildConstructor($class, $class->getNode());
9393
$this->buildToArray($class);
9494
if ($class->getProperties()) {
9595
$class->addProperty('_usedProperties', null, '[]');
@@ -121,7 +121,7 @@ private function buildNode(NodeInterface $node, ClassBuilder $class, string $nam
121121

122122
private function handleArrayNode(ArrayNode $node, ClassBuilder $class, string $namespace): void
123123
{
124-
$childClass = new ClassBuilder($namespace, $node->getName());
124+
$childClass = new ClassBuilder($namespace, $node->getName(), $node);
125125
$childClass->setAllowExtraKeys($node->shouldIgnoreExtraKeys());
126126
$class->addRequire($childClass);
127127
$this->classes[] = $childClass;
@@ -271,7 +271,7 @@ public function NAME(string $VAR, TYPE $VALUE): static
271271
return;
272272
}
273273

274-
$childClass = new ClassBuilder($namespace, $name);
274+
$childClass = new ClassBuilder($namespace, $name, $prototype);
275275
if ($prototype instanceof ArrayNode) {
276276
$childClass->setAllowExtraKeys($prototype->shouldIgnoreExtraKeys());
277277
}

Tests/Builder/Fixtures/AddToList/Symfony/Config/AddToList/Messenger/ReceivingConfig.php

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,8 @@ public function color($value): static
4242

4343
/**
4444
* @param array{
45-
* translator?: array{
46-
* fallbacks?: list<scalar|null>,
47-
* sources?: array<string, scalar|null>,
48-
* books?: array{ // Deprecated: The child node "books" at path "add_to_list.translator.books" is deprecated. // looks for translation in old fashion way
49-
* page?: list<array{
50-
* number?: int<min, max>,
51-
* content?: scalar|null,
52-
* }>,
53-
* },
54-
* },
55-
* messenger?: array{
56-
* routing?: array<string, array{
57-
* senders?: list<scalar|null>,
58-
* }>,
59-
* receiving?: list<array{
60-
* priority?: int<min, max>,
61-
* color?: scalar|null,
62-
* }>,
63-
* },
45+
* priority?: int<min, max>,
46+
* color?: scalar|null,
6447
* } $config
6548
*/
6649
public function __construct(array $config = [])

Tests/Builder/Fixtures/AddToList/Symfony/Config/AddToList/Messenger/RoutingConfig.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,7 @@ public function senders(ParamConfigurator|array $value): static
2828

2929
/**
3030
* @param array{
31-
* translator?: array{
32-
* fallbacks?: list<scalar|null>,
33-
* sources?: array<string, scalar|null>,
34-
* books?: array{ // Deprecated: The child node "books" at path "add_to_list.translator.books" is deprecated. // looks for translation in old fashion way
35-
* page?: list<array{
36-
* number?: int<min, max>,
37-
* content?: scalar|null,
38-
* }>,
39-
* },
40-
* },
41-
* messenger?: array{
42-
* routing?: array<string, array{
43-
* senders?: list<scalar|null>,
44-
* }>,
45-
* receiving?: list<array{
46-
* priority?: int<min, max>,
47-
* color?: scalar|null,
48-
* }>,
49-
* },
31+
* senders?: list<scalar|null>,
5032
* } $config
5133
*/
5234
public function __construct(array $config = [])

Tests/Builder/Fixtures/AddToList/Symfony/Config/AddToList/MessengerConfig.php

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,13 @@ public function receiving(array $value = []): \Symfony\Config\AddToList\Messenge
3737

3838
/**
3939
* @param array{
40-
* translator?: array{
41-
* fallbacks?: list<scalar|null>,
42-
* sources?: array<string, scalar|null>,
43-
* books?: array{ // Deprecated: The child node "books" at path "add_to_list.translator.books" is deprecated. // looks for translation in old fashion way
44-
* page?: list<array{
45-
* number?: int<min, max>,
46-
* content?: scalar|null,
47-
* }>,
48-
* },
49-
* },
50-
* messenger?: array{
51-
* routing?: array<string, array{
52-
* senders?: list<scalar|null>,
53-
* }>,
54-
* receiving?: list<array{
55-
* priority?: int<min, max>,
56-
* color?: scalar|null,
57-
* }>,
58-
* },
40+
* routing?: array<string, array{
41+
* senders?: list<scalar|null>,
42+
* }>,
43+
* receiving?: list<array{
44+
* priority?: int<min, max>,
45+
* color?: scalar|null,
46+
* }>,
5947
* } $config
6048
*/
6149
public function __construct(array $config = [])

Tests/Builder/Fixtures/AddToList/Symfony/Config/AddToList/Translator/Books/PageConfig.php

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,8 @@ public function content($value): static
4242

4343
/**
4444
* @param array{
45-
* translator?: array{
46-
* fallbacks?: list<scalar|null>,
47-
* sources?: array<string, scalar|null>,
48-
* books?: array{ // Deprecated: The child node "books" at path "add_to_list.translator.books" is deprecated. // looks for translation in old fashion way
49-
* page?: list<array{
50-
* number?: int<min, max>,
51-
* content?: scalar|null,
52-
* }>,
53-
* },
54-
* },
55-
* messenger?: array{
56-
* routing?: array<string, array{
57-
* senders?: list<scalar|null>,
58-
* }>,
59-
* receiving?: list<array{
60-
* priority?: int<min, max>,
61-
* color?: scalar|null,
62-
* }>,
63-
* },
45+
* number?: int<min, max>,
46+
* content?: scalar|null,
6447
* } $config
6548
*/
6649
public function __construct(array $config = [])

Tests/Builder/Fixtures/AddToList/Symfony/Config/AddToList/Translator/BooksConfig.php

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,11 @@ public function page(array $value = []): \Symfony\Config\AddToList\Translator\Bo
2626
}
2727

2828
/**
29-
* @param array{
30-
* translator?: array{
31-
* fallbacks?: list<scalar|null>,
32-
* sources?: array<string, scalar|null>,
33-
* books?: array{ // Deprecated: The child node "books" at path "add_to_list.translator.books" is deprecated. // looks for translation in old fashion way
34-
* page?: list<array{
35-
* number?: int<min, max>,
36-
* content?: scalar|null,
37-
* }>,
38-
* },
39-
* },
40-
* messenger?: array{
41-
* routing?: array<string, array{
42-
* senders?: list<scalar|null>,
43-
* }>,
44-
* receiving?: list<array{
45-
* priority?: int<min, max>,
46-
* color?: scalar|null,
47-
* }>,
48-
* },
29+
* @param array{ // Deprecated: The child node "books" at path "add_to_list.translator.books" is deprecated. // looks for translation in old fashion way
30+
* page?: list<array{
31+
* number?: int<min, max>,
32+
* content?: scalar|null,
33+
* }>,
4934
* } $config
5035
*/
5136
public function __construct(array $config = [])

Tests/Builder/Fixtures/AddToList/Symfony/Config/AddToList/TranslatorConfig.php

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,12 @@ public function books(array $value = []): \Symfony\Config\AddToList\Translator\B
5959

6060
/**
6161
* @param array{
62-
* translator?: array{
63-
* fallbacks?: list<scalar|null>,
64-
* sources?: array<string, scalar|null>,
65-
* books?: array{ // Deprecated: The child node "books" at path "add_to_list.translator.books" is deprecated. // looks for translation in old fashion way
66-
* page?: list<array{
67-
* number?: int<min, max>,
68-
* content?: scalar|null,
69-
* }>,
70-
* },
71-
* },
72-
* messenger?: array{
73-
* routing?: array<string, array{
74-
* senders?: list<scalar|null>,
75-
* }>,
76-
* receiving?: list<array{
77-
* priority?: int<min, max>,
78-
* color?: scalar|null,
62+
* fallbacks?: list<scalar|null>,
63+
* sources?: array<string, scalar|null>,
64+
* books?: array{ // Deprecated: The child node "books" at path "add_to_list.translator.books" is deprecated. // looks for translation in old fashion way
65+
* page?: list<array{
66+
* number?: int<min, max>,
67+
* content?: scalar|null,
7968
* }>,
8069
* },
8170
* } $config

Tests/Builder/Fixtures/ArrayExtraKeys/Symfony/Config/ArrayExtraKeys/BarConfig.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,9 @@ public function grault($value): static
4242

4343
/**
4444
* @param array{
45-
* foo?: array{
46-
* baz?: scalar|null,
47-
* qux?: scalar|null,
48-
* ...<mixed>
49-
* },
50-
* bar?: list<array{
51-
* corge?: scalar|null,
52-
* grault?: scalar|null,
53-
* ...<mixed>
54-
* }>,
55-
* baz?: array<mixed>,
45+
* corge?: scalar|null,
46+
* grault?: scalar|null,
47+
* ...<mixed>
5648
* } $config
5749
*/
5850
public function __construct(array $config = [])

Tests/Builder/Fixtures/ArrayExtraKeys/Symfony/Config/ArrayExtraKeys/BazConfig.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,7 @@ class BazConfig
1212
private $_extraKeys;
1313

1414
/**
15-
* @param array{
16-
* foo?: array{
17-
* baz?: scalar|null,
18-
* qux?: scalar|null,
19-
* ...<mixed>
20-
* },
21-
* bar?: list<array{
22-
* corge?: scalar|null,
23-
* grault?: scalar|null,
24-
* ...<mixed>
25-
* }>,
26-
* baz?: array<mixed>,
27-
* } $config
15+
* @param array<mixed> $config
2816
*/
2917
public function __construct(array $config = [])
3018
{

0 commit comments

Comments
 (0)