Skip to content

Commit c998faa

Browse files
feature #40600 [Config][DependencyInjection] Add configuration builder for writing PHP config (Nyholm)
This PR was squashed before being merged into the 5.3-dev branch. Discussion ---------- [Config][DependencyInjection] Add configuration builder for writing PHP config | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | | License | MIT | Doc PR | symfony/symfony-docs#15181 I've spent most part of today to generate this PR. It is far from complete but it is ready for review. This PR will build classes and store them in the build_dir. The classes will help you write PHP config. It will basically generate an array for you. ### Before ```php // config/packages/security.php <?php use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; return static function (ContainerConfigurator $container) { $array = [ 'firewalls' => [ 'main' => [ 'pattern' => '^/*', 'lazy' => true, 'anonymous' => [], ], ], 'access_control' => [ [ 'path' => '^/user', 'roles' => [ 0 => 'ROLE_USER', ], ], [ 'path' => '^/admin', 'roles' => 'ROLE_ADMIN', ], ], 'role_hierarchy' => [ 'ROLE_ADMIN' => ['ROLE_USER'], 'ROLE_SUPER_ADMIN' => ['ROLE_ADMIN', 'ROLE_ALLOWED_TO_SWITCH', ], ], ]; $container->extension('security', $array); } ``` ### After ```php // config/packages/security.php <?php use Symfony\Config\SecurityConfig; return static function (SecurityConfig $security) { $security ->roleHierarchy('ROLE_ADMIN', ['ROLE_USER']) ->roleHierarchy('ROLE_SUPER_ADMIN', ['ROLE_ADMIN', 'ROLE_ALLOWED_TO_SWITCH']) ->accessControl() ->path('^/user') ->role('ROLE_USER'); $security->accessControl(['path' => '^/admin', 'roles' => 'ROLE_ADMIN']); $security->firewall('main') ->pattern('^/*') ->lazy(true) ->anonymous(); }; ``` ### About autogeneration This PR is generating the extension's `ConfigBuilder`s when they are first used. Since the PR is already very large, I prefer to follow up with additional PRs to include a cache warmer and command to rebuild the `ConfigBuilder`s. The generated `ConfigBuilder` uses a "ucfirst() camelCased" extension alias. If the alias is `acme_foo` the root `ConfigBuilder` will be `Symfony\Config\AcmeFooConfig`. The recommended way of using this class is: ```php // config/packages/acme_foo.php use Symfony\Config\AcmeFooConfig; return static function (AcmeFooConfig $foo) { // ... // No need to return } ``` One may also init the class directly, But this will not help you with generation or autoloading ```php // config/packages/acme_foo.php use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; return static function (ContainerConfigurator $container) { $foo = new \Symfony\Config\AcmeFooConfig(); // ... $container->extension('acme_foo', $foo->toArray()); } ``` **I do think we should only talk about the first way** If a third party bundle like this idea and want to provide their own `ConfigBuilder`, they have two options: 1) Create the class `Symfony\Config\TheBundleConfig` themselves and make sure they configure composer to autoload that file and that the class implements `ConfigBuilderInterface`. We will never regenerate a file that already exists. 2) Create any class implementing `ConfigBuilderInterface` and ask their users to use that class in their config in the same way they would use `Symfony\Config\TheBundleConfig`. The first way is obviously the smoothest way of doing things. ### BC There is a great discussion about backwards compatibility for the generated files. We can assure that the class generator don't introduce a BC break with our tests. However, if the bundle changes their configuration definition it may break BC. Things like renaming, changing type or changing a single value to array is obvious BC breaks, however, these can be fixed in the config definition with normalisation. The generator does not support normalisation. It is way way more complicated to reverse engineer that. I think a future update could fix this in one of two ways: 1) Add extra definition rules to help the class generator 2) Allow the bundle to patch part of the generated code I hate BC breaks as much as the next person, but all the BC breaks in the generated classes will be caught when building the container (not at runtime), so I am fine with not having a 100% complete solution for this issue in the initial PR. ### Other limitations If a bundle is using a custom extension alias, then we cannot guess it.. so a user have to use a cache warmer because we cannot generate the `ConfigBuilder` on the fly. ### TODO - [x] Add tests - [x] Update changelog - [x] Write documentation ------------- The generated code can be found in this example app: https://github.com/Nyholm/sf-issue-40600/tree/main/var/cache/dev/Symfony/Config Commits ------- 460b46f730 [Config][DependencyInjection] Add configuration builder for writing PHP config
2 parents 0905c3e + 28aec45 commit c998faa

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

Kernel.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Bridge\ProxyManager\LazyProxy\Instantiator\RuntimeInstantiator;
1515
use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper;
16+
use Symfony\Component\Config\Builder\ConfigBuilderGenerator;
1617
use Symfony\Component\Config\ConfigCache;
1718
use Symfony\Component\Config\Loader\DelegatingLoader;
1819
use Symfony\Component\Config\Loader\LoaderResolver;
@@ -758,7 +759,7 @@ protected function getContainerLoader(ContainerInterface $container)
758759
new XmlFileLoader($container, $locator, $env),
759760
new YamlFileLoader($container, $locator, $env),
760761
new IniFileLoader($container, $locator, $env),
761-
new PhpFileLoader($container, $locator, $env),
762+
new PhpFileLoader($container, $locator, $env, class_exists(ConfigBuilderGenerator::class) ? new ConfigBuilderGenerator($this->getBuildDir()) : null),
762763
new GlobFileLoader($container, $locator, $env),
763764
new DirectoryLoader($container, $locator, $env),
764765
new ClosureLoader($container, $env),

0 commit comments

Comments
 (0)