[DependencyInjection] extensions should only load if called during configuration
This commit is contained in:
parent
6790e45911
commit
bf0553c23a
@ -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);
|
||||
}
|
||||
|
@ -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.
|
||||
*
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user