8000 [FrameworkBundle][config cmd] initialize extension. · guilhermeblanco/symfony@d6ec874 · GitHub
[go: up one dir, main page]

Skip to content

Commit d6ec874

Browse files
aitboudadfabpot
authored andcommitted
[FrameworkBundle][config cmd] initialize extension.
1 parent 2f6d5e4 commit d6ec874

File tree

8 files changed

+199
-0
lines changed

8 files changed

+199
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
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;
1719

1820
/**
1921
* A console command for dumping available configuration reference.
@@ -70,6 +72,8 @@ protected function findExtension($name)
7072
}
7173
}
7274

75+
$this->initializeExtensions($bundles);
76+
7377
return $extension;
7478
}
7579

@@ -83,4 +87,22 @@ public function validateConfiguration(ExtensionInterface $extension, $configurat
8387
throw new \LogicException(sprintf('Configuration class "%s" should implement ConfigurationInterface in order to be dumpable', get_class($configuration)));
8488
}
8589
}
90+
91+
private function initializeExtensions($bundles)
92+
{
93+
// Re-build bundle manually to initialize DI extensions that can be extended by other bundles in their build() method
94+
// 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));
97+
foreach ($bundles as $bundle) {
98+
if ($extension = $bundle->getContainerExtension()) {
99+
$container->registerExtension($extension);
100+
}
101+
}
102+
103+
foreach ($bundles as $bundle) {
104+
$bundle = clone $bundle;
105+
$bundle->build($container);
106+
}
107+
}
86108
}
< ED4F th scope="col">Diff line change
Original file line numberDiff line number
@@ -0,0 +1,24 @@
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\Bundle\TestBundle\DependencyInjection\Config;
13+
14+
class CustomConfig
15+
{
16+
public function addConfiguration($rootNode)
17+
{
18+
$rootNode
19+
->children()
20+
->scalarNode('custom')->end()
21+
->end()
22+
;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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\Bundle\TestBundle\DependencyInjection;
13+
14+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
15+
use Symfony\Component\Config\Definition\ConfigurationInterface;
16+
17+
class Configuration implements ConfigurationInterface
18+
{
19+
private $customConfig;
20+
21+
public function __construct($customConfig = null)
22+
{
23+
$this->customConfig = $customConfig;
24+
}
25+
26+
public function getConfigTreeBuilder()
27+
{
28+
$treeBuilder = new TreeBuilder();
29+
$rootNode = $treeBuilder->root('test');
30+
31+
if ($this->customConfig) {
32+
$this->customConfig->addConfiguration($rootNode);
33+
}
34+
35+
return $treeBuilder;
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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\Bundle\TestBundle\DependencyInjection;
13+
14+
use Symfony\Component\DependencyInjection\ContainerBuilder;
15+
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
16+
17+
class TestExtension extends Extension
18+
{
19+
private $customConfig;
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function load(array $configs, ContainerBuilder $container)
25+
{
26+
$configuration = $this->getConfiguration($configs, $container);
27+
$config = $this->processConfiguration($configuration, $configs);
28+
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
public function getConfiguration(array $config, ContainerBuilder $container)
34+
{
35+
return new Configuration($this->customConfig);
36+
}
37+
38+
public function setCustomConfig($customConfig)
39+
{
40+
$this->customConfig = $customConfig;
41+
}
42+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,18 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle;
1313

1414
use Symfony\Component\HttpKernel\Bundle\Bundle;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\DependencyInjection\Config\CustomConfig;
1517

1618
class TestBundle extends Bundle
1719
{
20+
public function build(ContainerBuilder $container)
21+
{
22+
parent::build($container);
23+
24+
/** @var $extension DependencyInjection\TestExtension */
25+
$extension = $container->getExtension('test');
26+
27+
$extension->setCustomConfig(new CustomConfig());
28+
}
1829
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 ConfigDumpReferenceCommandTest 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->assertEquals(0, $ret, 'Returns 0 in case of success');
39+
$this->assertContains('test:', $tester->getDisplay());
40+
$this->assertContains(' custom:', $tester->getDisplay());
41+
}
42+
43+
/**
44+
* @return CommandTester
45+
*/
46+
private function createCommandTester()
47+
{
48+
$command = $this->application->find('config:dump-reference');
49+
50+
return new CommandTester($command);
51+
}
52+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle;
4+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
5+
6+
return array(
7+
new FrameworkBundle(),
8+
new TestBundle(),
9+
);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
imports:
2+
- { resource: ../config/default.yml }

0 commit comments

Comments
 (0)
0