Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #52636 [DependencyInjection] Prepend extension config with `C…
…ontainerConfigurator` (yceruto) This PR was merged into the 7.1 branch. Discussion ---------- [DependencyInjection] Prepend extension config with `ContainerConfigurator` | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | - | License | MIT I found this by fixing an issue in a bundle that was trying to prepend extension configs using `$container->extension('namespace', [...])` in `AbstractBundle::prependExtension()`, which indeed was appending that config instead of prepending it. Most importantly, the append strategy requires the extension namespace to be loaded beforehand, which is not required when prepend is used. This title DX improvement helps to avoid the confusion between `ContainerConfigurator $container` and `ContainerBuilder $builder` to prepend extension config by allowing `ContainerConfigurator` to do the same now. Example: ```php class AcmeFooBundle extends AbstractBundle { public function prependExtension(ContainerConfigurator $container, ContainerBuilder $builder): void { // Before (only way) $builder->prependExtensionConfig('namespace', ['foo' => 'bar']); // After (also this way) passing "true" as the 3rd parameter $container->extension('namespace', ['foo' => 'bar'], true); } } ``` Instead of adding a new `$prepend` argument to the existing method, I could create a new method, e.g., `ContainerConfigurator::prependExtension()`. What do you prefer? This also helps when you want to prepend several or large configs in your bundle or extension class. Actually, using just `$container->import('...')` doesn't work because internally it will always append the configs, unless you do the following hidden trick below. ```php // acme-bundle/config/packages/configs.php use Symfony\Component\DependencyInjection\ContainerBuilder; return static function (ContainerBuilder $container) { $container->prependExtensionConfig('namespace', ['large' => 'config', ...]); }; ``` If you type `ContainerBuilder` instead of `ContainerConfigurator` in the external PHP config file, the builder instance will be passed instead, allowing you to use the `prependExtensionConfig()` method. But with this proposal, it's simpler as you can keep using `ContainerConfigurator` to prepend extension configs without doing any tactic. Commits ------- 137518d add argument to prepend extension config
- Loading branch information