Skip to content

Commit

Permalink
bug #13886 [FrameworkBundle][debug:config] added support for dynamic …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
fabpot committed Mar 23, 2015
2 parents eda1ab7 + 0f9eb7a commit 39da732
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 38 deletions.
Expand Up @@ -14,8 +14,6 @@
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;

/**
* A console command for dumping available configuration reference.
Expand Down Expand Up @@ -43,37 +41,24 @@ protected function listBundles(OutputInterface $output)
protected function findExtension($name)
{
$extension = null;
$bundles = $this->initializeBundles();
foreach ($bundles as $bundle) {
$extension = $bundle->getContainerExtension();

$bundles = $this->getContainer()->get('kernel')->getBundles();

if (preg_match('/Bundle$/', $name)) {
// input is bundle name

if (isset($bundles[$name])) {
$extension = $bundles[$name]->getContainerExtension();
}

if (!$extension) {
throw new \LogicException(sprintf('No extensions with configuration available for "%s"', $name));
if ($extension && ($name === $extension->getAlias() || $name === $bundle->getName())) {
break;
}
} else {
foreach ($bundles as $bundle) {
$extension = $bundle->getContainerExtension();

if ($extension && $name === $extension->getAlias()) {
break;
}
}

$extension = null;
if (!$extension) {
$message = sprintf('No extension with alias "%s" is enabled', $name);
if (preg_match('/Bundle$/', $name)) {
$message = sprintf('No extensions with configuration available for "%s"', $name);
}

if (!$extension) {
throw new \LogicException(sprintf('No extension with alias "%s" is enabled', $name));
}
throw new \LogicException($message);
}

$this->initializeExtensions($bundles);

return $extension;
}

Expand All @@ -88,21 +73,22 @@ public function validateConfiguration(ExtensionInterface $extension, $configurat
}
}

private function initializeExtensions($bundles)
private function initializeBundles()
{
// Re-build bundle manually to initialize DI extensions that can be extended by other bundles in their build() method
// as this method is not called when the container is loaded from the cache.
$parameters = $this->getContainer()->getParameterBag()->all();
$container = new ContainerBuilder(new ParameterBag($parameters));
$container = $this->getContainerBuilder();
$bundles = $this->getContainer()->get('kernel')->registerBundles();
foreach ($bundles as $bundle) {
if ($extension = $bundle->getContainerExtension()) {
$container->registerExtension($extension);
}
}

foreach ($bundles as $bundle) {
$bundle = clone $bundle;
$bundle->build($container);
}

return $bundles;
}
}
19 changes: 14 additions & 5 deletions src/Symfony/Bundle/FrameworkBundle/Command/ConfigDebugCommand.php
Expand Up @@ -66,11 +66,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

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

$kernel = $this->getContainer()->get('kernel');
$method = new \ReflectionMethod($kernel, 'buildContainer');
$method->setAccessible(true);
$container = $method->invoke($kernel);
$container = $this->compileContainer();

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

$output->writeln(Yaml::dump(array($extension->getAlias() => $config), 3));
}

private function compileContainer()
{
$kernel = clone $this->getContainer()->get('kernel');
$kernel->boot();

$method = new \ReflectionMethod($kernel, 'buildContainer');
$method->setAccessible(true);
$container = $method->invoke($kernel);
$container->getCompiler()->compile($container);

return $container;
}
}
Expand Up @@ -162,6 +162,10 @@ protected function validateInput(InputInterface $input)
*/
protected function getContainerBuilder()
{
if ($this->containerBuilder) {
return $this->containerBuilder;
}

if (!$this->getApplication()->getKernel()->isDebug()) {
throw new \LogicException(sprintf('Debug information about the container is only available in debug mode.'));
}
Expand All @@ -175,7 +179,7 @@ protected function getContainerBuilder()
$loader = new XmlFileLoader($container, new FileLocator());
$loader->load($cachedFile);

return $container;
return $this->containerBuilder = $container;
}

private function findProperServiceName(InputInterface $input, OutputInterface $output, ContainerBuilder $builder, $name)
Expand Down
Expand Up @@ -13,8 +13,9 @@

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

class TestExtension extends Extension
class TestExtension extends Extension implements PrependExtensionInterface
{
private $customConfig;

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

/**
* {@inheritdoc}
*/
public function prepend(ContainerBuilder $container)
{
$container->prependExtensionConfig('test', array('custom' => 'foo'));
}

/**
* {@inheritdoc}
*/
Expand Down
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Tester\CommandTester;

/**
* @group functional
*/
class ConfigDebugCommandTest extends WebTestCase
{
private $application;

protected function setUp()
{
$kernel = static::createKernel(array('test_case' => 'ConfigDump', 'root_config' => 'config.yml'));
$this->application = new Application($kernel);
$this->application->doRun(new ArrayInput(array()), new NullOutput());
}

public function testDumpBundleName()
{
$tester = $this->createCommandTester();
$ret = $tester->execute(array('name' => 'TestBundle'));

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

/**
* @return CommandTester
*/
private function createCommandTester()
{
$command = $this->application->find('debug:config');

return new CommandTester($command);
}
}
Expand Up @@ -35,7 +35,7 @@ public function testDumpBundleName()
$tester = $this->createCommandTester();
$ret = $tester->execute(array('name' => 'TestBundle'));

$this->assertEquals(0, $ret, 'Returns 0 in case of success');
$this->assertSame(0, $ret, 'Returns 0 in case of success');
$this->assertContains('test:', $tester->getDisplay());
$this->assertContains(' custom:', $tester->getDisplay());
}
Expand Down

0 comments on commit 39da732

Please sign in to comment.