From bf0553c23a2a9ee06468be9c87681eab2f364b0f Mon Sep 17 00:00:00 2001 From: Kris Wallsmith Date: Thu, 3 Mar 2011 13:53:29 +0100 Subject: [PATCH] [DependencyInjection] extensions should only load if called during configuration --- .../MergeExtensionConfigurationPass.php | 7 ++- .../DependencyInjection/ContainerBuilder.php | 47 +++++-------------- .../ContainerBuilderTest.php | 23 +++++++++ 3 files changed, 41 insertions(+), 36 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/MergeExtensionConfigurationPass.php b/src/Symfony/Component/DependencyInjection/Compiler/MergeExtensionConfigurationPass.php index bcaf9aa125..da40583d9b 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/MergeExtensionConfigurationPass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/MergeExtensionConfigurationPass.php @@ -30,10 +30,15 @@ class MergeExtensionConfigurationPass implements CompilerPassInterface $aliases = $container->getAliases(); foreach ($container->getExtensions() as $name => $extension) { + if (!$config = $container->getExtensionConfig($name)) { + // this extension was not called + continue; + } + $tmpContainer = new ContainerBuilder($container->getParameterBag()); $tmpContainer->addObjectResource($extension); - $extension->load($container->getExtensionConfig($name), $tmpContainer); + $extension->load($config, $tmpContainer); $container->merge($tmpContainer); } diff --git a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php index 19f7c23d94..44e66de269 100644 --- a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php +++ b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php @@ -46,6 +46,8 @@ class ContainerBuilder extends Container implements TaggedContainerInterface public function __construct(ParameterBagInterface $parameterBag = null) { parent::__construct($parameterBag); + + $this->compiler = new Compiler(); } /** @@ -166,47 +168,35 @@ class ContainerBuilder extends Container implements TaggedContainerInterface } /** - * Adds a compiler pass at the end of the current passes + * Adds a compiler pass. * - * @param CompilerPassInterface $pass - * @param string $type + * @param CompilerPassInterface $pass A compiler pass + * @param string $type The type of compiler pass */ public function addCompilerPass(CompilerPassInterface $pass, $type = PassConfig::TYPE_BEFORE_OPTIMIZATION) { - if (null === $this->compiler) { - $this->initializeCompiler(); - } - $this->compiler->addPass($pass, $type); $this->addObjectResource($pass); } /** - * Returns the compiler pass config which can then be modified + * Returns the compiler pass config which can then be modified. * - * @return PassConfig + * @return PassConfig The compiler pass config */ public function getCompilerPassConfig() { - if (null === $this->compiler) { - $this->initializeCompiler(); - } - return $this->compiler->getPassConfig(); } /** - * Returns the compiler instance + * Returns the compiler. * - * @return Compiler + * @return Compiler The compiler */ public function getCompiler() { - if (null === $this->compiler) { - $this->initializeCompiler(); - } - return $this->compiler; } @@ -379,7 +369,7 @@ class ContainerBuilder extends Container implements TaggedContainerInterface public function getExtensionConfig($name) { if (!isset($this->extensionConfigs[$name])) { - return array(array()); + $this->extensionConfigs[$name] = array(); } return $this->extensionConfigs[$name]; @@ -401,8 +391,8 @@ class ContainerBuilder extends Container implements TaggedContainerInterface */ public function compile() { - if (null === $this->compiler) { - $this->initializeCompiler(); + foreach ($this->compiler->getPassConfig()->getPasses() as $pass) { + $this->addObjectResource($pass); } $this->compiler->compile($this); @@ -836,19 +826,6 @@ class ContainerBuilder extends Container implements TaggedContainerInterface return $tags; } - /** - * Initializes the compiler - * - * @return void - */ - protected function initializeCompiler() - { - $this->compiler = new Compiler(); - foreach ($this->compiler->getPassConfig()->getPasses() as $pass) { - $this->addObjectResource($pass); - } - } - /** * Returns the Service Conditionals. * diff --git a/tests/Symfony/Tests/Component/DependencyInjection/ContainerBuilderTest.php b/tests/Symfony/Tests/Component/DependencyInjection/ContainerBuilderTest.php index 795c9d8c0e..47a1bd266f 100644 --- a/tests/Symfony/Tests/Component/DependencyInjection/ContainerBuilderTest.php +++ b/tests/Symfony/Tests/Component/DependencyInjection/ContainerBuilderTest.php @@ -447,6 +447,29 @@ class ContainerBuilderTest extends \PHPUnit_Framework_TestCase $container->getExtension('no_registered'); } + public function testRegisteredButNotLoadedExtension() + { + $extension = $this->getMock('Symfony\\Component\\DependencyInjection\\Extension\\ExtensionInterface'); + $extension->expects($this->once())->method('getAlias')->will($this->returnValue('project')); + $extension->expects($this->never())->method('load'); + + $container = new ContainerBuilder(); + $container->registerExtension($extension); + $container->compile(); + } + + public function testRegisteredAndLoadedExtension() + { + $extension = $this->getMock('Symfony\\Component\\DependencyInjection\\Extension\\ExtensionInterface'); + $extension->expects($this->exactly(2))->method('getAlias')->will($this->returnValue('project')); + $extension->expects($this->once())->method('load')->with(array(array('foo' => 'bar'))); + + $container = new ContainerBuilder(); + $container->registerExtension($extension); + $container->loadFromExtension('project', array('foo' => 'bar')); + $container->compile(); + } + /** * @covers Symfony\Component\DependencyInjection\ContainerBuilder::addInterfaceInjector * @covers Symfony\Component\DependencyInjection\ContainerBuilder::addInterfaceInjectors