8000 Fall back to default configuration in debug:config and consistently r… · symfony/symfony@2affabe · GitHub
[go: up one dir, main page]

Skip to content

Commit 2affabe

Browse files
committed
Fall back to default configuration in debug:config and consistently resolve parameter values
1 parent e6c9155 commit 2affabe

File tree

9 files changed

+170
-16
lines changed

9 files changed

+170
-16
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/ConfigDebugCommand.php

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Command;
1313

14+
use Symfony\Component\Config\Definition\Processor;
1415
use Symfony\Component\Console\Exception\LogicException;
1516
use Symfony\Component\Console\Input\InputArgument;
1617
use Symfony\Component\Console\Input\InputInterface;
1718
use Symfony\Component\Console\Output\OutputInterface;
1819
use Symfony\Component\Console\Style\SymfonyStyle;
1920
use Symfony\Component\DependencyInjection\Compiler\ValidateEnvPlaceholdersPass;
2021
use Symfony\Component\DependencyInjection\ContainerBuilder;
22+
use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface;
23+
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
2124
use Symfony\Component\Yaml\Yaml;
2225

2326
/**
@@ -77,22 +80,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7780
}
7881

7982
$extension = $this->findExtension($name);
80-
$container = $this->compileContainer();
81-
8283
$extensionAlias = $extension->getAlias();
83-
$extensionConfig = [];
84-
foreach ($container->getCompilerPassConfig()->getPasses() as $pass) {
85-
if ($pass instanceof ValidateEnvPlaceholdersPass) {
86-
$extensionConfig = $pass->getExtensionConfig();
87-
break;
88-
}
89-
}
90-
91-
if (!isset($extensionConfig[$extensionAlias])) {
92-
throw new \LogicException(sprintf('The extension with alias "%s" does not have configuration.', $extensionAlias));
93-
}
84+
$container = $this->compileContainer();
9485

95-
$config = $container->resolveEnvPlaceholders($extensionConfig[$extensionAlias]);
86+
$config = $container->resolveEnvPlaceholders(
87+
$container->getParameterBag()->resolveValue(
88+
$this->getConfigForExtension($extension, $container)
89+
)
90+
);
9691

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

154149
return $config;
155150
}
151+
152+
private function getConfigForExtension(ExtensionInterface $extension, ContainerBuilder $container): array
153+
{
154+
$extensionAlias = $extension->getAlias();
155+
156+
$extensionConfig = [];
157+
foreach ($container->getCompilerPassConfig()->getPasses() as $pass) {
158+
if ($pass instanceof ValidateEnvPlaceholdersPass) {
159+
$extensionConfig = $pass->getExtensionConfig();
160+
break;
161+
}
162+
}
163+
164+
if (isset($extensionConfig[$extensionAlias])) {
165+
return $extensionConfig[$extensionAlias];
166+
}
167+
168+
// Fall back to default config if the extension has one
169+
170+
if (!$extension instanceof ConfigurationExtensionInterface) {
171+
throw new \LogicException(sprintf('The extension with alias "%s" does not have configuration.', $extensionAlias));
172+
}
173+
174+
$configs = $container->getExtensionConfig($extensionAlias);
175+
$configuration = $extension->getConfiguration($configs, $container);
176+
$this->validateConfiguration($extension, $configuration);
177+
178+
return (new Processor())->processConfiguration($configuration, $configs);
179+
}
156180
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\DefaultConfigTestBundle;
4+
5+
use Symfony\Component\HttpKernel\Bundle\Bundle;
6+
7+
class DefaultConfigTestBundle extends Bundle
8+
{
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\DefaultConfigTestBundle\DependencyInjection;
4+
5+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
6+
use Symfony\Component\Config\Definition\ConfigurationInterface;
7+
8+
class Configuration implements ConfigurationInterface
9+
{
10+
public function getConfigTreeBuilder(): TreeBuilder
11+
{
12+
$treeBuilder = new TreeBuilder('default_config_test');
13+
14+
$treeBuilder->getRootNode()
15+
->children()
16+
->scalarNode('foo')->defaultValue('%default_config_test_foo%')->end()
17+
->scalarNode('baz')->defaultValue('%env(BAZ)%')->end()
18+
->end();
19+
20+
return $treeBuilder;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\DefaultConfigTestBundle\DependencyInjection;
4+
5+
use Symfony\Component\DependencyInjection\ContainerBuilder;
6+
use Symfony\Component\DependencyInjection\Extension\Extension;
7+
8+
class DefaultConfigTestExtension extends Extension
9+
{
10+
public function load(array $configs, ContainerBuilder $container)
11+
{
12+
$configuration = new Configuration();
13+
$config = $this->processConfiguration($configuration, $configs);
14+
15+
$container->setParameter('default_config_test', $config['foo']);
16+
$container->setParameter('default_config_test', $config['baz']);
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\ExtensionWithoutConfigTestBundle\DependencyInjection;
4+
5+
use Symfony\Component\DependencyInjection\ContainerBuilder;
6+
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
7+
8+
class ExtensionWithoutConfigTestExtension implements ExtensionInterface
9+
{
10+
public function load(array $configs, ContainerBuilder $container)
11+
{
12+
}
13+
14+
public function getNamespace()
15+
{
16+
return '';
17+
}
18+
19+
public function getXsdValidationBasePath()
20+
{
21+
return false;
22+
}
23+
24+
public function getAlias()
25+
{
26+
return 'extension_without_config_test';
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\ExtensionWithoutConfigTestBundle;
4+
5+
use Symfony\Component\HttpKernel\Bundle\Bundle;
6+
7+
class ExtensionWithoutConfigTestBundle extends Bundle
8+
{
9+
}

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ConfigDebugCommandTest.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
1313

14+
use LogicException;
1415
use Symfony\Bundle\FrameworkBundle\Console\Application;
1516
use Symfony\Component\Console\Input\ArrayInput;
1617
use Symfony\Component\Console\Output\NullOutput;
1718
use Symfony\Component\Console\Tester\CommandTester;
19+
use Symfony\Component\DependencyInjection\ContainerInterface;
1820

1921
/**
2022
* @group functional
@@ -25,8 +27,8 @@ class ConfigDebugCommandTest extends AbstractWebTestCase
2527

2628
protected function setUp(): void
2729
{
28-
$kernel = static::createKernel(['test_case' => 'ConfigDump', 'root_config' => 'config.yml']);
29-
$this->application = new Application($kernel);
30+
self::$kernel = static::createKernel(['test_case' => 'ConfigDump', 'root_config' => 'config.yml']);
31+
$this->application = new Application(self::$kernel);
3032
$this->application->doRun(new ArrayInput([]), new NullOutput());
3133
}
3234

@@ -58,6 +60,16 @@ public function testParametersValuesAreResolved()
5860
$this->assertStringContainsString('secret: test', $tester->getDisplay());
5961
}
6062

63+
public function testDefaultParameterValueIsResolved()
64+
{
65+
$tester = $this->createCommandTester();
66+
$ret = $tester->execute(['name' => 'framework']);
67+
68+
$this->assertSame(0, $ret, 'Returns 0 in case of success');
69+
$kernelCacheDir = self::$kernel->getContainer()->getParameter('kernel.cache_dir');
70+
$this->assertStringContainsString(sprintf('save_path: %s/sessions', $kernelCacheDir), $tester->getDisplay());
71+
}
72+
6173
public function testDumpUndefinedBundleOption()
6274
{
6375
$tester = $this->createCommandTester();
@@ -74,6 +86,33 @@ public function testDumpWithPrefixedEnv()
7486
$this->assertStringContainsString("cookie_httponly: '%env(bool:COOKIE_HTTPONLY)%'", $tester->getDisplay());
7587
}
7688

89+
public function testDumpFallsBackToDefaultConfigAndResolvesParameterValue()
90+
{
91+
$tester = $this->createCommandTester();
92+
$ret = $tester->execute(['name' => 'DefaultConfigTestBundle']);
93+
94+
$this->assertSame(0, $ret, 'Returns 0 in case of success');
95+
$this->assertStringContainsString('foo: bar', $tester->getDisplay());
96+
}
97+
98+
public function testDumpFallsBackToDefaultConfigAndResolvesEnvPlaceholder()
99+
{
100+
$tester = $this->createCommandTester();
101+
$ret = $tester->execute(['name' => 'DefaultConfigTestBundle']);
102+
103+
$this->assertSame(0, $ret, 'Returns 0 in case of success');
104+
$this->assertStringContainsString("baz: '%env(BAZ)%'", $tester->getDisplay());
105+
}
106+
107+
public function testDumpThrowsExceptionWhenDefaultConfigFallbackIsImpossible()
108+
{
109+
$this->expectException(LogicException::class);
110+
$this->expectExceptionMessage('The extension with alias "extension_without_config_test" does not have configuration.');
111+
112+
$tester = $this->createCommandTester();
113+
$tester->execute(['name' => 'ExtensionWithoutConfigTestBundle']);
114+
}
115+
77116
private function createCommandTester(): CommandTester
78117
{
79118
$command = $this->application->find('debug:config');

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/ConfigDump/bundles.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010
*/
1111

1212
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
13+
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\DefaultConfigTestBundle\DefaultConfigTestBundle;
14+
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\ExtensionWithoutConfigTestBundle\ExtensionWithoutConfigTestBundle;
1315
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle;
1416

1517
return [
18+
new DefaultConfigTestBundle(),
19+
new ExtensionWithoutConfigTestBundle(),
1620
new FrameworkBundle(),
1721
new TestBundle(),
1822
];

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/ConfigDump/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ parameters:
1111
env(LOCALE): en
1212
env(COOKIE_HTTPONLY): '1'
1313
secret: test
14+
default_config_test_foo: bar

0 commit comments

Comments
 (0)
0