Skip to content

Commit 0693c5a

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 ------- 137518d add argument to prepend extension config
2 parents 19f5240 + 137518d commit 0693c5a

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

src/Symfony/Component/DependencyInjection/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

src/Symfony/Component/DependencyInjection/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'));

src/Symfony/Component/DependencyInjection/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

src/Symfony/Component/HttpKernel/Tests/Fixtures/AcmeFooBundle/AcmeFooBundle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function configure(DefinitionConfigurator $definition): void
3131

3232
public function prependExtension(ContainerConfigurator $container, ContainerBuilder $builder): void
3333
{
34-
$container->extension('loaded', ['bar' => 'baz']);
34+
$container->extension('loaded', ['bar' => 'baz'], true);
3535
}
3636

3737
public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy