Skip to content

Commit ed9f4e9

Browse files
bug #62436 [FrameworkBundle] Dump all registered extensions’ configuration reference (MatTheCat)
This PR was squashed before being merged into the 7.4 branch. Discussion ---------- [FrameworkBundle] Dump all registered extensions’ configuration reference | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #62428 | License | MIT Commits ------- badc255c143 [FrameworkBundle] Dump all registered extensions’ configuration reference
2 parents e3f0f2f + db3100e commit ed9f4e9

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

DependencyInjection/Compiler/PhpConfigReferenceDumpPass.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ public function process(ContainerBuilder $container): void
9595
$appTypes = '';
9696

9797
$anyEnvExtensions = [];
98+
foreach ($container->getExtensions() as $alias => $extension) {
99+
if (!$configuration = $this->getConfiguration($extension, $container)) {
100+
continue;
101+
}
102+
103+
$anyEnvExtensions[$alias] = $extension;
104+
$type = $this->camelCase($alias).'Config';
105+
$appTypes .= \sprintf("\n * @psalm-type %s = %s", $type, ArrayShapeGenerator::generate($configuration->getConfigTreeBuilder()->buildTree()));
106+
}
98107
foreach ($this->bundlesDefinition as $bundle => $envs) {
99108
if (!is_subclass_of($bundle, BundleInterface::class)) {
100109
continue;
@@ -105,15 +114,21 @@ public function process(ContainerBuilder $container): void
105114
if (!$configuration = $this->getConfiguration($extension, $container)) {
106115
continue;
107116
}
108-
$anyEnvExtensions[$bundle] = $extension;
109-
$type = $this->camelCase($extension->getAlias()).'Config';
110-
$appTypes .= \sprintf("\n * @psalm-type %s = %s", $type, ArrayShapeGenerator::generate($configuration->getConfigTreeBuilder()->buildTree()));
117+
118+
$extensionAlias = $extension->getAlias();
119+
if (isset($anyEnvExtensions[$extensionAlias])) {
120+
$extension = $anyEnvExtensions[$extensionAlias];
121+
} else {
122+
$anyEnvExtensions[$extensionAlias] = $extension;
123+
$type = $this->camelCase($extensionAlias).'Config';
124+
$appTypes .= \sprintf("\n * @psalm-type %s = %s", $type, ArrayShapeGenerator::generate($configuration->getConfigTreeBuilder()->buildTree()));
125+
}
111126

112127
foreach ($knownEnvs as $env) {
113128
if ($envs[$env] ?? $envs['all'] ?? false) {
114129
$extensionsPerEnv[$env][] = $extension;
115130
} else {
116-
unset($anyEnvExtensions[$bundle]);
131+
unset($anyEnvExtensions[$extensionAlias]);
117132
}
118133
}
119134
}

Tests/DependencyInjection/Compiler/PhpConfigReferenceDumpPassTest.php

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ public function testProcessGeneratesExpectedReferenceFile()
9595
$container = new ContainerBuilder();
9696
$container->setParameter('.container.known_envs', ['dev', 'prod', 'test']);
9797

98-
$extension = new TestExtension();
99-
$container->registerExtension($extension);
98+
$container->registerExtension(new TestExtension(false));
99+
$container->registerExtension(new AppExtension());
100100

101101
$pass = new PhpConfigReferenceDumpPass($this->tempDir.'/reference.php', [
102102
TestBundle::class => ['all' => true],
@@ -133,12 +133,16 @@ class TestBundle extends Bundle
133133
{
134134
public function getContainerExtension(): ?ExtensionInterface
135135
{
136-
return new TestExtension();
136+
return new TestExtension(true);
137137
}
138138
}
139139

140140
class TestExtension extends Extension
141141
{
142+
public function __construct(private bool $fromBundle)
143+
{
144+
}
145+
142146
public function load(array $configs, ContainerBuilder $container): void
143147
{
144148
}
@@ -160,12 +164,16 @@ public function getAlias(): string
160164

161165
public function getConfiguration(array $config, ContainerBuilder $container): ConfigurationInterface
162166
{
163-
return new TestConfiguration();
167+
return new TestConfiguration($this->fromBundle);
164168
}
165169
}
166170

167171
class TestConfiguration implements ConfigurationInterface
168172
{
173+
public function __construct(private bool $fromBundle)
174+
{
175+
}
176+
169177
public function getConfigTreeBuilder(): TreeBuilder
170178
{
171179
$treeBuilder = new TreeBuilder('test');
@@ -181,9 +189,30 @@ public function getConfigTreeBuilder(): TreeBuilder
181189
->integerNode('count')->end()
182190
->end()
183191
->end()
192+
->booleanNode('fromBundle')->defaultValue($this->fromBundle)->end()
184193
->end();
185194
}
186195

187196
return $treeBuilder;
188197
}
189198
}
199+
200+
class AppExtension extends Extension
201+
{
202+
public function load(array $configs, ContainerBuilder $container): void
203+
{
204+
}
205+
206+
public function getConfiguration(array $config, ContainerBuilder $container): ConfigurationInterface
207+
{
208+
return new AppConfiguration();
209+
}
210+
}
211+
212+
class AppConfiguration implements ConfigurationInterface
213+
{
214+
public function getConfigTreeBuilder(): TreeBuilder
215+
{
216+
return new TreeBuilder('app', 'boolean');
217+
}
218+
}

Tests/Fixtures/reference.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,15 @@
129129
* name?: scalar|null,
130130
* count?: int,
131131
* },
132+
* fromBundle?: bool, // Default: false
132133
* }
134+
* @psalm-type AppConfig = bool
133135
* @psalm-type ConfigType = array{
134136
* imports?: ImportsConfig,
135137
* parameters?: ParametersConfig,
136138
* services?: ServicesConfig,
137139
* test?: TestConfig,
140+
* app?: AppConfig,
138141
* "when@dev"?: array{
139142
* imports?: ImportsConfig,
140143
* parameters?: ParametersConfig,

0 commit comments

Comments
 (0)