[DependencyInjection] moved extension loading in the freezing process (opens more possibilities in the loading order of configs)

This commit is contained in:
Fabien Potencier 2010-08-24 16:25:08 +02:00
parent 3c42e0b6ce
commit b1e79963b1
2 changed files with 27 additions and 32 deletions

View File

@ -24,11 +24,11 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
{ {
static protected $extensions = array(); static protected $extensions = array();
protected $definitions = array(); protected $definitions = array();
protected $aliases = array(); protected $aliases = array();
protected $loading = array(); protected $loading = array();
protected $resources = array(); protected $resources = array();
protected $extensionContainers = array(); protected $extensionConfigs = array();
/** /**
* Registers an extension. * Registers an extension.
@ -114,19 +114,13 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
throw new \LogicException('Cannot load from an extension on a frozen container.'); throw new \LogicException('Cannot load from an extension on a frozen container.');
} }
$extension = $this->getExtension($extension); $namespace = $this->getExtension($extension)->getAlias();
$namespace = $extension->getAlias();
$this->addObjectResource($extension); if (!isset($this->extensionConfigs[$namespace.':'.$tag])) {
$this->extensionConfigs[$namespace.':'.$tag] = array();
if (!isset($this->extensionContainers[$namespace])) {
$this->extensionContainers[$namespace] = new self($this->parameterBag);
$r = new \ReflectionObject($extension);
$this->extensionContainers[$namespace]->addResource(new FileResource($r->getFileName()));
} }
$extension->load($tag, $values, $this->extensionContainers[$namespace]); $this->extensionConfigs[$namespace.':'.$tag][] = $values;
return $this; return $this;
} }
@ -235,11 +229,11 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
$this->addResource($resource); $this->addResource($resource);
} }
foreach ($container->getExtensionContainers() as $name => $container) { foreach ($container->getExtensionConfigs() as $name => $configs) {
if (isset($this->extensionContainers[$name])) { if (isset($this->extensionConfigs[$name])) {
$this->extensionContainers[$name]->merge($container); $this->extensionConfigs[$name] = array_merge($this->extensionConfigs[$name], $configs);
} else { } else {
$this->extensionContainers[$name] = $container; $this->extensionConfigs[$name] = $configs;
} }
} }
} }
@ -249,9 +243,9 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
* *
* @return ExtensionInterface[] An array of extension containers * @return ExtensionInterface[] An array of extension containers
*/ */
public function getExtensionContainers() public function getExtensionConfigs()
{ {
return $this->extensionContainers; return $this->extensionConfigs;
} }
/** /**
@ -270,10 +264,20 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
$definitions = $this->definitions; $definitions = $this->definitions;
$aliases = $this->aliases; $aliases = $this->aliases;
foreach ($this->extensionContainers as $container) { foreach ($this->extensionConfigs as $name => $configs) {
list($namespace, $tag) = explode(':', $name);
$extension = $this->getExtension($namespace);
$container = new self($this->parameterBag);
$container->addObjectResource($extension);
foreach ($configs as $config) {
$extension->load($tag, $config, $container);
}
$this->merge($container); $this->merge($container);
} }
$this->extensionContainers = array(); $this->extensionConfigs = array();
$this->addDefinitions($definitions); $this->addDefinitions($definitions);
$this->addAliases($aliases); $this->addAliases($aliases);

View File

@ -231,15 +231,6 @@ class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
$this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the tag is not valid'); $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the tag is not valid');
$this->assertStringStartsWith('There is no extension able to load the configuration for "project:bar" (in', $e->getMessage(), '->load() throws an InvalidArgumentException if the tag is not valid'); $this->assertStringStartsWith('There is no extension able to load the configuration for "project:bar" (in', $e->getMessage(), '->load() throws an InvalidArgumentException if the tag is not valid');
} }
// non-existent tag for a known extension
try {
$loader->load('extensions/services5.xml');
$this->fail('->load() throws an InvalidArgumentException if a tag is not valid for a given extension');
} catch (\Exception $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if a tag is not valid for a given extension');
$this->assertStringStartsWith('The tag "projectwithxsd:foobar" is not defined in the "projectwithxsd" extension.', $e->getMessage(), '->load() throws an InvalidArgumentException if a tag is not valid for a given extension');
}
} }
/** /**