Skip to content

Commit d1f7528

Browse files
bug #61940 [DependencyInjection] Register a custom autoloader to generate *Config classes when they don't exist yet (alexandre-daubois)
This PR was merged into the 7.4 branch. Discussion ---------- [DependencyInjection] Register a custom autoloader to generate `*Config` classes when they don't exist yet | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | - | License | MIT When using classes like `FrameworkConfig` or `WebProfilerConfig` in the config, cache cannot be built as classes don't exist yet. This PRs adds a custom class autoloader that checks if the requested `*Config` class exists under the `Symfony\Config` namespace, and triggers the class generation if necessary. Commits ------- 28b25afc663 [DependencyInjection] Register a custom autoloader to generate `*Config` classes when they don't exist yet
2 parents 3de593a + 9f6b13c commit d1f7528

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

Loader/PhpFileLoader.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
class PhpFileLoader extends FileLoader
3737
{
3838
protected bool $autoRegisterAliasesForSinglyImplementedInterfaces = false;
39+
private ?\Closure $configBuilderAutoloader = null;
3940

4041
public function __construct(
4142
ContainerBuilder $container,
@@ -60,6 +61,14 @@ public function load(mixed $resource, ?string $type = null): mixed
6061
// Force load ContainerConfigurator to make env(), param() etc available.
6162
class_exists(ContainerConfigurator::class);
6263

64+
if ($autoloaderRegistered = !$this->configBuilderAutoloader && $this->generator) {
65+
spl_autoload_register($this->configBuilderAutoloader = function (string $class) {
66+
if (str_starts_with($class, 'Symfony\\Config\\') && str_ends_with($class, 'Config')) {
67+
$this->configBuilder($class);
68+
}
69+
});
70+
}
71+
6372
// the closure forbids access to the private scope in the included file
6473
$load = \Closure::bind(static function ($path, $env) use ($container, $loader, $resource, $type) {
6574
return include $path;
@@ -108,6 +117,11 @@ class_exists(ContainerConfigurator::class);
108117
} finally {
109118
$this->instanceof = [];
110119
$this->registerAliasesForSinglyImplementedInterfaces();
120+
121+
if ($autoloaderRegistered) {
122+
spl_autoload_unregister($this->configBuilderAutoloader);
123+
$this->configBuilderAutoloader = null;
124+
}
111125
}
112126

113127
return null;

0 commit comments

Comments
 (0)