8000 [FrameworkBundle] Fall back to default configuration in debug:config and consistently resolve parameter values by herndlm · Pull Request #42368 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] Fall back to default configuration in debug:config and consistently resolve parameter values #42368

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
Aug 26, 2021
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
[FrameworkBundle] Fall back to default configuration in debug:config …
…and consistently resolve parameter values
  • Loading branch information
herndlm committed Aug 25, 2021
commit 9b6110bfaad5abfe4ec4f90506e8616cfbf48874
52 changes: 38 additions & 14 deletions src/Symfony/Bundle/FrameworkBundle/Command/ConfigDebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@

namespace Symfony\Bundle\FrameworkBundle\Command;

use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\Compiler\ValidateEnvPlaceholdersPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\Yaml\Yaml;

/**
Expand Down Expand Up @@ -77,22 +80,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$extension = $this->findExtension($name);
$container = $this->compileContainer();

$extensionAlias = $extension->getAlias();
$extensionConfig = [];
foreach ($container->getCompilerPassConfig()->getPasses() as $pass) {
if ($pass instanceof ValidateEnvPlaceholdersPass) {
$extensionConfig = $pass->getExtensionConfig();
break;
}
}

if (!isset($extensionConfig[$extensionAlias])) {
throw new \LogicException(sprintf('The extension with alias "%s" does not have configuration.', $extensionAlias));
}
$container = $this->compileContainer();

$config = $container->resolveEnvPlaceholders($extensionConfig[$extensionAlias]);
$config = $container->resolveEnvPlaceholders(
$container->getParameterBag()->resolveValue(
$this->getConfigForExtension($extension, $container)
)
);

if (null === $path = $input->getArgument('path')) {
$io->title(
Expand Down Expand Up @@ -153,4 +148,33 @@ private function getConfigForPath(array $config, string $path, string $alias)

return $config;
}

private function getConfigForExtension(ExtensionInterface $extension, ContainerBuilder $container): array
{
$extensionAlias = $extension->getAlias();

$extensionConfig = [];
foreach ($container->getCompilerPassConfig()->getPasses() as $pass) {
if ($pass instanceof ValidateEnvPlaceholdersPass) {
$extensionConfig = $pass->getExtensionConfig();
break;
}
}

if (isset($extensionConfig[$extensionAlias])) {
return $extensionConfig[$extensionAlias];
}

// Fall back to default config if the extension has one

if (!$extension instanceof ConfigurationExtensionInterface) {
throw new \LogicException(sprintf('The extension with alias "%s" does not have configuration.', $extensionAlias));
}

$configs = $container->getExtensionConfig($extensionAlias);
$configuration = $extension->getConfiguration($configs, $container);
$this->validateConfiguration($extension, $configuration);

return (new Processor())->processConfiguration($configuration, $configs);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\DefaultConfigTestBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class DefaultConfigTestBundle extends Bundle
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\DefaultConfigTestBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('default_config_test');

$treeBuilder->getRootNode()
->children()
->scalarNode('foo')->defaultValue('%default_config_test_foo%')->end()
->scalarNode('baz')->defaultValue('%env(BAZ)%')->end()
->end();

return $treeBuilder;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\DefaultConfigTestBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;

class DefaultConfigTestExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$container->setParameter('default_config_test', $config['foo']);
$container->setParameter('default_config_test', $config['baz']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\ExtensionWithoutConfigTestBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;

class ExtensionWithoutConfigTestExtension implements ExtensionInterface
{
public function load(array $configs, ContainerBuilder $container)
{
}

public function getNamespace()
{
return '';
}

public function getXsdValidationBasePath()
{
return false;
}

public function getAlias()
{
return 'extension_without_config_test';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\ExtensionWithoutConfigTestBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class ExtensionWithoutConfigTestBundle extends Bundle
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ public function testParametersValuesAreResolved()
$this->assertStringContainsString('secret: test', $tester->getDisplay());
}

public function testDefaultParameterValueIsResolvedIfConfigIsExisting()
{
$tester = $this->createCommandTester();
$ret = $tester->execute(['name' => 'framework']);

$this->assertSame(0, $ret, 'Returns 0 in case of success');
$kernelCacheDir = $this->application->getKernel()->getContainer()->getParameter('kernel.cache_dir');
$this->assertStringContainsString(sprintf("dsn: 'file:%s/profiler'", $kernelCacheDir), $tester->getDisplay());
}

public function testDumpUndefinedBundleOption()
{
$tester = $this->createCommandTester();
Expand All @@ -74,6 +84,33 @@ public function testDumpWithPrefixedEnv()
$this->assertStringContainsString("cookie_httponly: '%env(bool:COOKIE_HTTPONLY)%'", $tester->getDisplay());
}

public function testDumpFallsBackToDefaultConfigAndResolvesParameterValue()
{
$tester = $this->createCommandTester();
$ret = $tester->execute(['name' => 'DefaultConfigTestBundle']);

$this->assertSame(0, $ret, 'Returns 0 in case of success');
$this->assertStringContainsString('foo: bar', $tester->getDisplay());
}

public function testDumpFallsBackToDefaultConfigAndResolvesEnvPlaceholder()
{
$tester = $this->createCommandTester();
$ret = $tester->execute(['name' => 'DefaultConfigTestBundle']);

$this->assertSame(0, $ret, 'Returns 0 in case of success');
$this->assertStringContainsString("baz: '%env(BAZ)%'", $tester->getDisplay());
}

public function testDumpThrowsExceptionWhenDefaultConfigFallbackIsImpossible()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The extension with alias "extension_without_config_test" does not have configuration.');

$tester = $this->createCommandTester();
$tester->execute(['name' => 'ExtensionWithoutConfigTestBundle']);
}

private function createCommandTester(): CommandTester
{
$command = $this->application->find('debug:config');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
*/

use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\DefaultConfigTestBundle\DefaultConfigTestBundle;
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\ExtensionWithoutConfigTestBundle\ExtensionWithoutConfigTestBundle;
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle;

return [
new DefaultConfigTestBundle(),
new ExtensionWithoutConfigTestBundle(),
new FrameworkBundle(),
new TestBundle(),
];
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ parameters:
env(LOCALE): en
env(COOKIE_HTTPONLY): '1'
secret: test
default_config_test_foo: bar
0