[FrameworkBundle][debug:config] added support for dynamic configurations.

This commit is contained in:
Abdellatif Ait boudad 2015-03-10 10:04:39 +00:00
parent 49c60d7383
commit 0f9eb7a79d
6 changed files with 99 additions and 40 deletions

View File

@ -14,8 +14,6 @@ namespace Symfony\Bundle\FrameworkBundle\Command;
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.
@ -43,36 +41,23 @@ abstract class AbstractConfigCommand extends ContainerDebugCommand
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));
}
} else {
foreach ($bundles as $bundle) {
$extension = $bundle->getContainerExtension();
if ($extension && $name === $extension->getAlias()) {
break;
}
$extension = null;
}
if (!$extension) {
throw new \LogicException(sprintf('No extension with alias "%s" is enabled', $name));
if ($extension && ($name === $extension->getAlias() || $name === $bundle->getName())) {
break;
}
}
$this->initializeExtensions($bundles);
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);
}
throw new \LogicException($message);
}
return $extension;
}
@ -88,12 +73,12 @@ abstract class AbstractConfigCommand extends ContainerDebugCommand
}
}
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);
@ -101,8 +86,9 @@ abstract class AbstractConfigCommand extends ContainerDebugCommand
}
foreach ($bundles as $bundle) {
$bundle = clone $bundle;
$bundle->build($container);
}
return $bundles;
}
}

View File

@ -66,11 +66,7 @@ EOF
}
$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);
@ -90,4 +86,17 @@ EOF
$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;
}
}

View File

@ -162,6 +162,10 @@ EOF
*/
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.'));
}
@ -175,7 +179,7 @@ EOF
$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)

View File

@ -13,8 +13,9 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Depe
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;
@ -27,6 +28,14 @@ class TestExtension extends Extension
$config = $this->processConfiguration($configuration, $configs);
}
/**
* {@inheritdoc}
*/
public function prepend(ContainerBuilder $container)
{
$container->prependExtensionConfig('test', array('custom' => 'foo'));
}
/**
* {@inheritdoc}
*/

View File

@ -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);
}
}

View File

@ -35,7 +35,7 @@ class ConfigDumpReferenceCommandTest extends WebTestCase
$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());
}