Skip to content

Commit 7a69935

Browse files
feature #52636 [DependencyInjection] Prepend extension config with ContainerConfigurator (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 ------- 137518de5d6 add argument to prepend extension config
2 parents b47cb73 + 93f8509 commit 7a69935

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.1
5+
---
6+
7+
* Add argument `$prepend` to `ContainerConfigurator::extension()` to prepend the configuration instead of appending it
8+
49
7.0
510
---
611

Loader/Configurator/ContainerConfigurator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,14 @@ public function __construct(ContainerBuilder $container, PhpFileLoader $loader,
4848
$this->env = $env;
4949
}
5050

51-
final public function extension(string $namespace, array $config): void
51+
final public function extension(string $namespace, array $config, bool $prepend = false): void
5252
{
53+
if ($prepend) {
54+
$this->container->prependExtensionConfig($namespace, static::processValue($config));
55+
56+
return;
57+
}
58+
5359
if (!$this->container->hasExtension($namespace)) {
5460
$extensions = array_filter(array_map(fn (ExtensionInterface $ext) => $ext->getAlias(), $this->container->getExtensions()));
5561
throw new InvalidArgumentException(sprintf('There is no extension able to load the configuration for "%s" (in "%s"). Looked for namespace "%s", found "%s".', $namespace, $this->file, $namespace, $extensions ? implode('", "', $extensions) : 'none'));

Tests/Extension/AbstractExtensionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function prependExtension(ContainerConfigurator $container, ContainerBuil
6363
$container->extension('third', ['foo' => 'append']);
6464

6565
// prepend config
66-
$builder->prependExtensionConfig('third', ['foo' => 'prepend']);
66+
$container->extension('third', ['foo' => 'prepend'], true);
6767
}
6868
};
6969

0 commit comments

Comments
 (0)