From b1e79963b120e3237a7a82f186ce83a5f568bdcf Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 24 Aug 2010 16:25:08 +0200 Subject: [PATCH] [DependencyInjection] moved extension loading in the freezing process (opens more possibilities in the loading order of configs) --- .../DependencyInjection/ContainerBuilder.php | 50 ++++++++++--------- .../Loader/XmlFileLoaderTest.php | 9 ---- 2 files changed, 27 insertions(+), 32 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php index ffef2846b8..4019fa5908 100644 --- a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php +++ b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php @@ -24,11 +24,11 @@ class ContainerBuilder extends Container implements TaggedContainerInterface { static protected $extensions = array(); - protected $definitions = array(); - protected $aliases = array(); - protected $loading = array(); - protected $resources = array(); - protected $extensionContainers = array(); + protected $definitions = array(); + protected $aliases = array(); + protected $loading = array(); + protected $resources = array(); + protected $extensionConfigs = array(); /** * 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.'); } - $extension = $this->getExtension($extension); - $namespace = $extension->getAlias(); + $namespace = $this->getExtension($extension)->getAlias(); - $this->addObjectResource($extension); - - if (!isset($this->extensionContainers[$namespace])) { - $this->extensionContainers[$namespace] = new self($this->parameterBag); - - $r = new \ReflectionObject($extension); - $this->extensionContainers[$namespace]->addResource(new FileResource($r->getFileName())); + if (!isset($this->extensionConfigs[$namespace.':'.$tag])) { + $this->extensionConfigs[$namespace.':'.$tag] = array(); } - $extension->load($tag, $values, $this->extensionContainers[$namespace]); + $this->extensionConfigs[$namespace.':'.$tag][] = $values; return $this; } @@ -235,11 +229,11 @@ class ContainerBuilder extends Container implements TaggedContainerInterface $this->addResource($resource); } - foreach ($container->getExtensionContainers() as $name => $container) { - if (isset($this->extensionContainers[$name])) { - $this->extensionContainers[$name]->merge($container); + foreach ($container->getExtensionConfigs() as $name => $configs) { + if (isset($this->extensionConfigs[$name])) { + $this->extensionConfigs[$name] = array_merge($this->extensionConfigs[$name], $configs); } 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 */ - 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; $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->extensionContainers = array(); + $this->extensionConfigs = array(); $this->addDefinitions($definitions); $this->addAliases($aliases); diff --git a/tests/Symfony/Tests/Component/DependencyInjection/Loader/XmlFileLoaderTest.php b/tests/Symfony/Tests/Component/DependencyInjection/Loader/XmlFileLoaderTest.php index ba21b8bcb8..7d6b549cb0 100644 --- a/tests/Symfony/Tests/Component/DependencyInjection/Loader/XmlFileLoaderTest.php +++ b/tests/Symfony/Tests/Component/DependencyInjection/Loader/XmlFileLoaderTest.php @@ -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->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'); - } } /**