Navigation Menu

Skip to content

Commit

Permalink
[FrameworkBundle][config cmd] initialize extension.
Browse files Browse the repository at this point in the history
  • Loading branch information
aitboudad authored and fabpot committed Feb 5, 2015
1 parent 2f6d5e4 commit d6ec874
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 0 deletions.
Expand Up @@ -14,6 +14,8 @@
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 @@ -70,6 +72,8 @@ protected function findExtension($name)
}
}

$this->initializeExtensions($bundles);

return $extension;
}

Expand All @@ -83,4 +87,22 @@ public function validateConfiguration(ExtensionInterface $extension, $configurat
throw new \LogicException(sprintf('Configuration class "%s" should implement ConfigurationInterface in order to be dumpable', get_class($configuration)));
}
}

private function initializeExtensions($bundles)
{
// 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));
foreach ($bundles as $bundle) {
if ($extension = $bundle->getContainerExtension()) {
$container->registerExtension($extension);
}
}

foreach ($bundles as $bundle) {
$bundle = clone $bundle;
$bundle->build($container);
}
}
}
@@ -0,0 +1,24 @@
<?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\Bundle\TestBundle\DependencyInjection\Config;

class CustomConfig
{
public function addConfiguration($rootNode)
{
$rootNode
->children()
->scalarNode('custom')->end()
->end()
;
}
}
@@ -0,0 +1,37 @@
<?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\Bundle\TestBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
private $customConfig;

public function __construct($customConfig = null)
{
$this->customConfig = $customConfig;
}

public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('test');

if ($this->customConfig) {
$this->customConfig->addConfiguration($rootNode);
}

return $treeBuilder;
}
}
@@ -0,0 +1,42 @@
<?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\Bundle\TestBundle\DependencyInjection;

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

class TestExtension extends Extension
{
private $customConfig;

/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = $this->getConfiguration($configs, $container);
$config = $this->processConfiguration($configuration, $configs);
}

/**
* {@inheritdoc}
*/
public function getConfiguration(array $config, ContainerBuilder $container)
{
return new Configuration($this->customConfig);
}

public function setCustomConfig($customConfig)
{
$this->customConfig = $customConfig;
}
}
Expand Up @@ -12,7 +12,18 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\DependencyInjection\Config\CustomConfig;

class TestBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);

/** @var $extension DependencyInjection\TestExtension */
$extension = $container->getExtension('test');

$extension->setCustomConfig(new CustomConfig());
}
}
@@ -0,0 +1,52 @@
<?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 ConfigDumpReferenceCommandTest 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->assertEquals(0, $ret, 'Returns 0 in case of success');
$this->assertContains('test:', $tester->getDisplay());
$this->assertContains(' custom:', $tester->getDisplay());
}

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

return new CommandTester($command);
}
}
@@ -0,0 +1,9 @@
<?php

use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;

return array(
new FrameworkBundle(),
new TestBundle(),
);
@@ -0,0 +1,2 @@
imports:
- { resource: ../config/default.yml }

0 comments on commit d6ec874

Please sign in to comment.