Skip to content

[DependencyInjection] Accessing processed extension configs #48408

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

Closed
wants to merge 1 commit into from
Closed
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 @@ -2183,6 +2183,7 @@ protected function createContainer(array $data = [])
'kernel.project_dir' => __DIR__,
'kernel.debug' => false,
'kernel.environment' => 'test',
'kernel.runtime_environment' => 'test',
'kernel.name' => 'kernel',
'kernel.container_class' => 'testContainer',
'container.build_hash' => 'Abc1234',
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.3
---

* Add build parameter `.extension.processed_configs` to get any extension configs (already processed) during compiler time

6.2
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ public function freezeAfterProcessing(Extension $extension, ContainerBuilder $co
}
$this->processedEnvPlaceholders = [];

$processedConfigs = $this->has('.extension.processed_configs') ? $this->get('.extension.processed_configs') : [];
$processedConfigs[$extension->getAlias()] = $config;
$this->set('.extension.processed_configs', $processedConfigs);

// serialize config and container to catch env vars nested in object graphs
$config = serialize($config).serialize($container->getDefinitions()).serialize($container->getAliases()).serialize($container->getParameterBag()->all());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\Compiler\MergeExtensionConfigurationContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\MergeExtensionConfigurationPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Extension\AbstractExtension;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;

Expand Down Expand Up @@ -144,6 +147,41 @@ public function testReuseEnvPlaceholderGeneratedByPreviousExtension()

$this->addToAssertionCount(1);
}

public function testGetExtensionConfigAfterProcessing()
{
$container = new ContainerBuilder(new EnvPlaceholderParameterBag([
'kernel.environment' => 'test',
'kernel.build_dir' => 'test',
]));

$container->registerExtension(new class() extends AbstractExtension {
public function configure(DefinitionConfigurator $definition): void
{
$definition->rootNode()
->children()
->scalarNode('bar')
->beforeNormalization()
->ifNull()
->then(static fn () => '')
->end()
->end()
->end()
;
}

public function getAlias(): string
{
return 'foo';
}
});

$container->loadFromExtension('foo', ['bar' => null]);

(new MergeExtensionConfigurationPass())->process($container);

self::assertSame(['foo' => [['bar' => '']]], $container->getParameter('.extension.processed_configs'));
}
}

class FooConfiguration implements ConfigurationInterface
Expand Down