8000 bug #13886 [FrameworkBundle][debug:config] added support for dynamic … · symfony/symfony@39da732 · GitHub
[go: up one dir, main page]

Skip to content

Commit 39da732

Browse files
committed
bug #13886 [FrameworkBundle][debug:config] added support for dynamic configurations... (aitboudad)
This PR was merged into the 2.6 branch. Discussion ---------- [FrameworkBundle][debug:config] added support for dynamic configurations... | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Fixed tickets | #12438 | Tests pass? | yes | License | MIT Commits ------- 0f9eb7a [FrameworkBundle][debug:config] added support for dynamic configurations.
2 parents eda1ab7 + 0f9eb7a commit 39da732

File tree

6 files changed

+97
-38
lines changed

6 files changed

+97
-38
lines changed

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

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
use Symfony\Component\Config\Definition\ConfigurationInterface;
1515
use Symfony\Component\Console\Output\OutputInterface;
1616
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
17-
use Symfony\Component\DependencyInjection\ContainerBuilder;
18-
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
1917

2018
/**
2119
* A console command for dumping available configuration reference.
@@ -43,37 +41,24 @@ protected function listBundles(OutputInterface $output)
4341
protected function findExtension($name)
4442
{
4543
$extension = null;
44+
$bundles = $this->initializeBundles();
45+
foreach ($bundles as $bundle) {
46+
$extension = $bundle->getContainerExtension();
4647

47-
$bundles = $this->getContainer()->get('kernel')->getBundles();
48-
49-
if (preg_match('/Bundle$/', $name)) {
50-
// input is bundle name
51-
52-
if (isset($bundles[$name])) {
53-
$extension = $bundles[$name]->getContainerExtension();
54-
}
55-
56-
if (!$extension) {
57-
throw new \LogicException(sprintf('No extensions with configuration available for "%s"', $name));
48+
if ($extension && ($name === $extension->getAlias() || $name === $bundle->getName())) {
49+
break;
5850
}
59-
} else {
60-
foreach ($bundles as $bundle) {
61-
$extension = $bundle->getContainerExtension();
62-
63-
if ($extension && $name === $extension->getAlias()) {
64-
break;
65-
}
51+
}
6652

67-
$extension = null;
53+
if (!$extension) {
54+
$message = sprintf('No extension with alias "%s" is enabled', $name);
55+
if (preg_match('/Bundle$/', $name)) {
56+
$message = sprintf('No extensions with configuration available for "%s"', $name);
6857
}
6958

70-
if (!$extension) {
71-
throw new \LogicException(sprintf('No extension with alias "%s" is enabled', $name));
72-
}
59+
throw new \LogicException($message);
7360
}
7461

75-
$this->initializeExtensions($bundles);
76-
7762
return $extension;
7863
}
7964

@@ -88,21 +73,22 @@ public function validateConfiguration(ExtensionInterface $extension, $configurat
8873
}
8974
}
9075

91-
private function initializeExtensions($bundles)
76+
private function initializeBundles()
9277
{
9378
// Re-build bundle manually to initialize DI extensions that can be extended by other bundles in their build() method
9479
// as this method is not called when the container is loaded from the cache.
95-
$parameters = $this->getContainer()->getParameterBag()->all();
96-
$container = new ContainerBuilder(new ParameterBag($parameters));
80+
$container = $this->getContainerBuilder();
81+
$bundles = $this->getContainer()->get('kernel')->registerBundles();
9782
foreach ($bundles as $bundle) {
9883
if ($extension = $bundle->getContainerExtension()) {
9984
$container->registerExtension($extension);
10085
}
10186
}
10287

10388
foreach ($bundles as $bundle) {
104-
$bundle = clone $bundle;
10589
$bundle->build($container);
10690
}
91+
92+
return $bundles;
10793
}
10894
}

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
6666
}
6767

6868
$extension = $this->findExtension($name);
69-
70-
$kernel = $this->getContainer()->get('kernel');
71-
$method = new \ReflectionMethod($kernel, 'buildContainer');
72-
$method->setAccessible(true);
73-
$container = $method->invoke($kernel);
69+
$container = $this->compileContainer();
7470

7571
$configs = $container->getExtensionConfig($extension->getAlias());
7672
$configuration = $extension->getConfiguration($configs, $container);
@@ -90,4 +86,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
9086

9187
$output->writeln(Yaml::dump(array($extension->getAlias() => $config), 3));
9288
}
89+
90+
private function compileContainer()
91+
{
92+
$kernel = clone $this->getContainer()->get('kernel');
93+
$kernel->boot();
94+
95+
$method = new \ReflectionMethod($kernel, 'buildContainer');
96+
$method->setAccessible(true);
97+
$container = $method->invoke($kernel);
98+
$container->getCompiler()->compile($container);
99+
100+
return $container;
101+
}
93102
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ protected function validateInput(InputInterface $input)
162162
*/
163163
protected function getContainerBuilder()
164164
{
165+
if ($this->containerBuilder) {
166+
return $this->containerBuilder;
167+
}
168+
165169
if (!$this->getApplication()->getKernel()->isDebug()) {
166170
throw new \LogicException(sprintf('Debug information about the container is only available in debug mode.'));
167171
}
@@ -175,7 +179,7 @@ protected function getContainerBuilder()
175179
$loader = new XmlFileLoader($container, new FileLocator());
176180
$loader->load($cachedFile);
177181

178-
return $container;
182+
return $this->containerBuilder = $container;
179183
}
180184

181185
private function findProperServiceName(InputInterface $input, OutputInterface $output, ContainerBuilder $builder, $name)

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/DependencyInjection/TestExtension.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313

1414
use Symfony\Component\DependencyInjection\ContainerBuilder;
1515
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
16+
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
1617

17-
class TestExtension extends Extension
18+
class TestExtension extends Extension implements PrependExtensionInterface
1819
{
1920
private $customConfig;
2021

@@ -27,6 +28,14 @@ public function load(array $configs, ContainerBuilder $container)
2728
$config = $this->processConfiguration($configuration, $configs);
2829
}
2930

31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public function prepend(ContainerBuilder $container)
35+
{
36+
$container->prependExtensionConfig('test', array('custom' => 'foo'));
37+
}
38+
3039
/**
3140
* {@inheritdoc}
3241
*/
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
13+
14+
use Symfony\Bundle\FrameworkBundle\Console\Application;
15+
use Symfony\Component\Console\Input\ArrayInput;
16+
use Symfony\Component\Console\Output\NullOutput;
17+
use Symfony\Component\Console\Tester\CommandTester;
18+
19+
/**
20+
* @group functional
21+
*/
22+
class ConfigDebugCommandTest extends WebTestCase
23+
{
24+
private $application;
25+
26+
protected function setUp()
27+
{
28+
$kernel = static::createKernel(array('test_case' => 'ConfigDump', 'root_config' => 'config.yml'));
29+
$this->application = new Application($kernel);
30+
$this->application->doRun(new ArrayInput(array()), new NullOutput());
31+
}
32+
33+
public function testDumpBundleName()
34+
{
35+
$tester = $this->createCommandTester();
36+
$ret = $tester->execute(array('name' => 'TestBundle'));
37+
38+
$this->assertSame(0, $ret, 'Returns 0 in case of success');
39+
$this->assertContains('custom: foo', $tester->getDisplay());
40+
}
41+
42+
/**
43+
* @return CommandTester
44+
*/
45+
private function createCommandTester()
46+
{
47+
$command = $this->application->find('debug:config');
48+
49+
return new CommandTester($command);
50+
}
51+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function testDumpBundleName()
3535
$tester = $this->createCommandTester();
3636
$ret = $tester->execute(array('name' => 'TestBundle'));
3737

38-
$this->assertEquals(0, $ret, 'Returns 0 in case of success');
38+
$this->assertSame(0, $ret, 'Returns 0 in case of success');
3939
$this->assertContains('test:', $tester->getDisplay());
4040
$this->assertContains(' custom:', $tester->getDisplay());
4141
}

0 commit comments

Comments
 (0)
0