Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b93e450

Browse files
committed
prepend extension configs with file loaders
1 parent 3330e55 commit b93e450

File tree

13 files changed

+85
-25
lines changed

13 files changed

+85
-25
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ CHANGELOG
55
---
66

77
* Add argument `$prepend` to `ContainerConfigurator::extension()` to prepend the configuration instead of appending it
8+
* Add argument `$prepend` to `FileLoader::construct()` to prepend loaded configuration instead of appending it
9+
* [BC BREAK] When used in the `prependExtension()` methods, the `ContainerConfigurator::import()` method now prepends the configuration instead of appending it
810

911
7.0
1012
---

src/Symfony/Component/DependencyInjection/Extension/AbstractExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ final public function prepend(ContainerBuilder $container): void
4949
$this->prependExtension($configurator, $container);
5050
};
5151

52-
$this->executeConfiguratorCallback($container, $callback, $this);
52+
$this->executeConfiguratorCallback($container, $callback, $this, true);
5353
}
5454

5555
final public function load(array $configs, ContainerBuilder $container): void

src/Symfony/Component/DependencyInjection/Extension/ExtensionTrait.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030
*/
3131
trait ExtensionTrait
3232
{
33-
private function executeConfiguratorCallback(ContainerBuilder $container, \Closure $callback, ConfigurableExtensionInterface $subject): void
33+
private function executeConfiguratorCallback(ContainerBuilder $container, \Closure $callback, ConfigurableExtensionInterface $subject, bool $prepend = false): void
3434
{
3535
$env = $container->getParameter('kernel.environment');
36-
$loader = $this->createContainerLoader($container, $env);
36+
$loader = $this->createContainerLoader($container, $env, $prepend);
3737
$file = (new \ReflectionObject($subject))->getFileName();
3838
$bundleLoader = $loader->getResolver()->resolve($file);
3939
if (!$bundleLoader instanceof PhpFileLoader) {
@@ -50,15 +50,15 @@ private function executeConfiguratorCallback(ContainerBuilder $container, \Closu
5050
}
5151
}
5252

53-
private function createContainerLoader(ContainerBuilder $container, string $env): DelegatingLoader
53+
private function createContainerLoader(ContainerBuilder $container, string $env, bool $prepend): DelegatingLoader
5454
{
5555
$buildDir = $container->getParameter('kernel.build_dir');
5656
$locator = new FileLocator();
5757
$resolver = new LoaderResolver([
58-
new XmlFileLoader($container, $locator, $env),
59-
new YamlFileLoader($container, $locator, $env),
58+
new XmlFileLoader($container, $locator, $env, $prepend),
59+
new YamlFileLoader($container, $locator, $env, $prepend),
6060
new IniFileLoader($container, $locator, $env),
61-
new PhpFileLoader($container, $locator, $env, new ConfigBuilderGenerator($buildDir)),
61+
new PhpFileLoader($container, $locator, $env, new ConfigBuilderGenerator($buildDir), $prepend),
6262
new GlobFileLoader($container, $locator, $env),
6363
new DirectoryLoader($container, $locator, $env),
6464
new ClosureLoader($container, $env),

src/Symfony/Component/DependencyInjection/Loader/FileLoader.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,15 @@ abstract class FileLoader extends BaseFileLoader
4545
/** @var array<string, Alias> */
4646
protected array $aliases = [];
4747
protected bool $autoRegisterAliasesForSinglyImplementedInterfaces = true;
48+
protected bool $prepend = false;
4849

49-
public function __construct(ContainerBuilder $container, FileLocatorInterface $locator, string $env = null)
50+
/**
51+
* @param bool $prepend Whether to prepend extension config instead of appending them
52+
*/
53+
public function __construct(ContainerBuilder $container, FileLocatorInterface $locator, string $env = null, bool $prepend = false)
5054
{
5155
$this->container = $container;
56+
$this->prepend = $prepend;
5257

5358
parent::__construct($locator, $env);
5459
}
@@ -217,6 +222,15 @@ public function registerAliasesForSinglyImplementedInterfaces(): void
217222
$this->interfaces = $this->singlyImplemented = $this->aliases = [];
218223
}
219224

225+
protected function loadExtensionConfig(string $namespace, array $config): void
226+
{
227+
if ($this->prepend) {
228+
$this->container->prependExtensionConfig($namespace, $config);
229+
} else {
230+
$this->container->loadFromExtension($namespace, $config);
231+
}
232+
}
233+
220234
/**
221235
* Registers a definition in the container with its instanceof-conditionals.
222236
*/

src/Symfony/Component/DependencyInjection/Loader/PhpFileLoader.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ class PhpFileLoader extends FileLoader
3636
protected bool $autoRegisterAliasesForSinglyImplementedInterfaces = false;
3737
private ?ConfigBuilderGeneratorInterface $generator;
3838

39-
public function __construct(ContainerBuilder $container, FileLocatorInterface $locator, string $env = null, ConfigBuilderGeneratorInterface $generator = null)
39+
public function __construct(ContainerBuilder $container, FileLocatorInterface $locator, string $env = null, ConfigBuilderGeneratorInterface $generator = null, bool $prepend = false)
4040
{
41-
parent::__construct($container, $locator, $env);
41+
parent::__construct($container, $locator, $env, $prepend);
4242
$this->generator = $generator;
4343
}
4444

@@ -147,7 +147,7 @@ class_exists(ContainerConfigurator::class);
147147

148148
/** @var ConfigBuilderInterface $configBuilder */
149149
foreach ($configBuilders as $configBuilder) {
150-
$containerConfigurator->extension($configBuilder->getExtensionAlias(), $configBuilder->toArray());
150+
$this->loadExtensionConfig($configBuilder->getExtensionAlias(), $configBuilder->toArray());
151151
}
152152
}
153153

src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ private function validateExtensions(\DOMDocument $dom, string $file): void
808808
}
809809

810810
// can it be handled by an extension?
811-
if (!$this->container->hasExtension($node->namespaceURI)) {
811+
if (!$this->prepend && !$this->container->hasExtension($node->namespaceURI)) {
812812
$extensionNamespaces = array_filter(array_map(fn (ExtensionInterface $ext) => $ext->getNamespace(), $this->container->getExtensions()));
813813
throw new InvalidArgumentException(sprintf('There is no extension able to load the configuration for "%s" (in "%s"). Looked for namespace "%s", found "%s".', $node->tagName, $file, $node->namespaceURI, $extensionNamespaces ? implode('", "', $extensionNamespaces) : 'none'));
814814
}
@@ -830,7 +830,7 @@ private function loadFromExtensions(\DOMDocument $xml): void
830830
$values = [];
831831
}
832832

833-
$this->container->loadFromExtension($node->namespaceURI, $values);
833+
$this->loadExtensionConfig($node->namespaceURI, $values);
834834
}
835835
}
836836

src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ class YamlFileLoader extends FileLoader
116116
private int $anonymousServicesCount;
117117
private string $anonymousServicesSuffix;
118118

119-
120119
public function load(mixed $resource, string $type = null): mixed
121120
{
122121
$path = $this->locator->locate($resource);
@@ -803,7 +802,7 @@ private function validate(mixed $content, string $file): ?array
803802
continue;
804803
}
805804

806-
if (!$this->container->hasExtension($namespace)) {
805+
if (!$this->prepend && !$this->container->hasExtension($namespace)) {
807806
$extensionNamespaces = array_filter(array_map(fn (ExtensionInterface $ext) => $ext->getAlias(), $this->container->getExtensions()));
808807
throw new InvalidArgumentException(sprintf('There is no extension able to load the configuration for "%s" (in "%s"). Looked for namespace "%s", found "%s".', $namespace, $file, $namespace, $extensionNamespaces ? sprintf('"%s"', implode('", "', $extensionNamespaces)) : 'none'));
809808
}
@@ -942,11 +941,11 @@ private function loadFromExtensions(array $content): void
942941
continue;
943942
}
944943

945-
if (!\is_array($values) && null !== $values) {
944+
if (!\is_array($values)) {
946945
$values = [];
947946
}
948947

949-
$this->container->loadFromExtension($namespace, $values);
948+
$this->loadExtensionConfig($namespace, $values);
950949
}
951950
}
952951

src/Symfony/Component/DependencyInjection/Tests/Extension/AbstractExtensionTest.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,25 @@ public function configure(DefinitionConfigurator $definition): void
5454
self::assertSame($expected, $this->processConfiguration($extension));
5555
}
5656

57-
public function testPrependAppendExtensionConfig()
57+
public function testPrependExtensionConfig()
5858
{
5959
$extension = new class() extends AbstractExtension {
6060
public function prependExtension(ContainerConfigurator $container, ContainerBuilder $builder): void
6161
{
62-
// append config
63-
$container->extension('third', ['foo' => 'append']);
62+
// prepend config from plain array
63+
$container->extension('third', ['foo' => 'pong'], true);
6464

65-
// prepend config
66-
$container->extension('third', ['foo' => 'prepend'], true);
65+
// prepend config from external file
66+
$container->import('../Fixtures/config/packages/ping.yaml');
6767
}
6868
};
6969

7070
$container = $this->processPrependExtension($extension);
7171

7272
$expected = [
73-
['foo' => 'prepend'],
73+
['foo' => 'ping'],
74+
['foo' => 'pong'],
7475
['foo' => 'bar'],
75-
['foo' => 'append'],
7676
];
7777

7878
self::assertSame($expected, $container->getExtensionConfig('third'));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
third:
2+
foo: ping

src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ public function testLoad()
4747
$this->assertEquals('foo', $container->getParameter('foo'), '->load() loads a PHP file resource');
4848
}
4949

50+
public function testPrependExtensionConfig()
51+
{
52+
$container = new ContainerBuilder();
53+
$container->registerExtension(new \AcmeExtension());
54+
$container->prependExtensionConfig('acme', ['foo' => 'bar']);
55+
$loader = new PhpFileLoader($container, new FileLocator(\dirname(__DIR__).'/Fixtures'), 'prod', new ConfigBuilderGenerator(sys_get_temp_dir()), true);
56+
$loader->load('config/config_builder.php');
57+
58+
$expected = [
59+
['color' => 'blue'],
60+
['foo' => 'bar'],
61+
];
62+
$this->assertSame($expected, $container->getExtensionConfig('acme'));
63+
}
64+
5065
public function testConfigServices()
5166
{
5267
$fixtures = realpath(__DIR__.'/../Fixtures');

src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,20 @@ public function testExtensions()
604604
}
605605
}
606606

607+
public function testPrependExtensionConfig()
608+
{
609+
$container = new ContainerBuilder();
610+
$container->prependExtensionConfig('http://www.example.com/schema/project', ['foo' => 'bar']);
611+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'), prepend: true);
612+
$loader->load('extensions/services1.xml');
613+
614+
$expected = [
615+
['babar' => 'babar'],
616+
['foo' => 'bar'],
617+
];
618+
$this->assertSame($expected, $container->getExtensionConfig('http://www.example.com/schema/project'));
619+
}
620+
607621
public function testExtensionInPhar()
608622
{
609623
if (\extension_loaded('suhosin') && !str_contains(\ini_get('suhosin.executor.include.whitelist'), 'phar')) {

src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,20 @@ public function testExtensions()
345345
}
346346
}
347347

348+
public function testPrependExtensionConfig()
349+
{
350+
$container = new ContainerBuilder();
351+
$container->prependExtensionConfig('project', ['foo' => 'bar']);
352+
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'), prepend: true);
353+
$loader->load('services10.yml');
354+
355+
$expected = [
356+
['test' => '%project.parameter.foo%'],
357+
['foo' => 'bar'],
358+
];
359+
$this->assertSame($expected, $container->getExtensionConfig('project'));
360+
}
361+
348362
public function testExtensionWithNullConfig()
349363
{
350364
$container = new ContainerBuilder();

src/Symfony/Component/HttpKernel/Bundle/BundleExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function prepend(ContainerBuilder $container): void
5151
$this->subject->prependExtension($configurator, $container);
5252
};
5353

54-
$this->executeConfiguratorCallback($container, $callback, $this->subject);
54+
$this->executeConfiguratorCallback($container, $callback, $this->subject, true);
5555
}
5656

5757
public function load(array $configs, ContainerBuilder $container): void

0 commit comments

Comments
 (0)