8000 [DependencyInjection] Process PHP configs using the ContainerConfigurator by MatTheCat · Pull Request #54970 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection] Process PHP configs using the ContainerConfigurator #54970

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,8 @@ class_exists(ContainerConfigurator::class);

$callback(...$arguments);

$this->loadFromExtensions($configBuilders);
}

/**
* @param iterable<ConfigBuilderInterface> $configBuilders
*/
private function loadFromExtensions(iterable $configBuilders): void
{
foreach ($configBuilders as $configBuilder) {
$this->loadExtensionConfig($configBuilder->getExtensionAlias(), $configBuilder->toArray());
$containerConfigurator->extension($configBuilder->getExtensionAlias(), $configBuilder->toArray(), $this->prepend);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still need to do some config prepending tests, but I feel that we've missed something here as the previous $this->loadExtensionConfig() is currently doing more things when prepend argument is true, e.g. the nested importing might be broken...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw you made the ContainerConfigurator able to prepend in #52636 so I assumed it was okay but it is indeed missing this part:

if ($this->importing) {
if (!isset($this->extensionConfigs[$namespace])) {
$this->extensionConfigs[$namespace] = [];
}
array_unshift($this->extensionConfigs[$namespace], $config);
return;
}

Don’t know if it is an issue at all though 😓

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it'll be an issue when want to prepend nested configs using imports

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After some quick tests, the root issue should be mitigated by processing only the config value this way:

$this->loadExtensionConfig($configBuilder->getExtensionAlias(), ContainerConfigurator::processValue($configBuilder->toArray()));

that will keep the prepending strategy working again. Would you mind creating another PR to update it? I can do it otherwise

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you already have the tests I think you’re more reading than I am; go ahead 😁

Would ContainerConfiguration::extension serve a purpose then?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would ContainerConfiguration::extension serve a purpose then?

It should be avoided internally where recursive importing is involved, as now importing from prependExtension() is supported (which requires an specific holding strategy to keep the importing order). However, you can still use it on your end for trivial array config importing (either in the prependExtension() or loadExtension() methods).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okaaay thanks; glad I asked your review 😄

}

$this->loadExtensionConfigs();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

use Symfony\Component\DependencyInjection\Tests\Fixtures\AcmeConfig;
use function Symfony\Component\DependencyInjection\Loader\Configurator\env;

return function (AcmeConfig $config) {
$config->color(env('COLOR'));
};
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,14 @@ public function testServiceWithServiceLocatorArgument()
$values = ['foo' => new Definition(\stdClass::class), 'bar' => new Definition(\stdClass::class)];
$this->assertEquals([new ServiceLocatorArgument($values)], $container->getDefinition('locator_dependent_inline_service')->getArguments());
}

public function testConfigBuilderEnvConfigurator()
{
$container = new ContainerBuilder();
$container->registerExtension(new \AcmeExtension());
$loader = new PhpFileLoader($container, new FileLocator(\dirname(__DIR__).'/Fixtures'), 'prod', new ConfigBuilderGenerator(sys_get_temp_dir()), true);
$loader->load('config/config_builder_env_configurator.php');

$this->assertIsString($container->getExtensionConfig('acme')[0]['color']);
}
}
Loading
0