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

Skip to content

Commit 86c4586

Browse files
committed
Fall back to default configuration in debug:config
1 parent 2cee75b commit 86c4586

File tree

8 files changed

+143
-14
lines changed

8 files changed

+143
-14
lines changed

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

Lines changed: 37 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,10 @@ 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($this->getConfigForExtension($extension, $container));
9687

9788
if (null === $path = $input->getArgument('path')) {
9889
$io->title(
@@ -153,4 +144,36 @@ private function getConfigForPath(array $config, string $path, string $alias)
153144

154145
return $config;
155146
}
147+
148+
/**
149+
* @return mixed
150+
*/
151+
private function getConfigForExtension(ExtensionInterface $extension, ContainerBuilder $container)
152+
{
153+
$extensionAlias = $extension->getAlias();
154+
155+
$extensionConfig = [];
156+
foreach ($container->getCompilerPassConfig()->getPasses() as $pass) {
157+
if ($pass instanceof ValidateEnvPlaceholdersPass) {
158+
$extensionConfig = $pass->getExtensionConfig();
159+
break;
160+
}
161+
}
162+
163+
if (isset($extensionConfig[$extensionAlias])) {
164+
return $extensionConfig[$extensionAlias];
165+
}
166+
167+
// Fall back to default config if the extension has one
168+
169+
if (!$extension instanceof ConfigurationExtensionInterface) {
170+
throw new \LogicException(sprintf('The extension with alias "%s" does not have configuration.', $extensionAlias));
171+
}
172+
173+
$configs = $container->getExtensionConfig($extensionAlias);
174+
$configuration = $extension->getConfiguration($configs, $container);
175+
$this->validateConfiguration($extension, $configuration);
176+
177+
return (new Processor())->processConfiguration($configuration, $configs);
178+
}
156179
}
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,21 @@
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('bar')->end()
17+
->end();
18+
19+
return $treeBuilder;
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}
17+
}
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
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;
@@ -74,6 +75,23 @@ public function testDumpWithPrefixedEnv()
7475
$this->assertStringContainsString("cookie_httponly: '%env(bool:COOKIE_HTTPONLY)%'", $tester->getDisplay());
7576
}
7677

78+
public function testDumpFallsBackToBundleDefaultConfig()
79+
{
80+
$tester = $this->createCommandTester();
81+
$tester->execute(['name' => 'DefaultConfigTestBundle']);
82+
83+
$this->assertStringContainsString('foo: bar', $tester->getDisplay());
84+
}
85+
86+
public function testDumpThrowsExceptionWhenDefaultConfigFallbackIsImpossible()
87+
{
88+
$this->expectException(LogicException::class);
89+
$this->expectExceptionMessage('The extension with alias "extension_without_config_test" does not have configuration.');
90+
91+
$tester = $this->createCommandTester();
92+
$tester->execute(['name' => 'ExtensionWithoutConfigTestBundle']);
93+
}
94+
7795
private function createCommandTester(): CommandTester
7896
{
7997
$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
];

0 commit comments

Comments
 (0)