Skip to content

Commit

Permalink
[DependencyInjection] moved extension loading in the freezing process…
Browse files Browse the repository at this point in the history
… (opens more possibilities in the loading order of configs)
  • Loading branch information
fabpot committed Aug 24, 2010
1 parent 3c42e0b commit b1e7996
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 32 deletions.
50 changes: 27 additions & 23 deletions src/Symfony/Component/DependencyInjection/ContainerBuilder.php
Expand Up @@ -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.
Expand Down Expand Up @@ -114,19 +114,13 @@ public function loadFromExtension($extension, $tag, array $values = array())
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;
}
Expand Down Expand Up @@ -235,11 +229,11 @@ public function merge(ContainerBuilder $container)
$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;
}
}
}
Expand All @@ -249,9 +243,9 @@ public function merge(ContainerBuilder $container)
*
* @return ExtensionInterface[] An array of extension containers
*/
public function getExtensionContainers()
public function getExtensionConfigs()
{
return $this->extensionContainers;
return $this->extensionConfigs;
}

/**
Expand All @@ -270,10 +264,20 @@ public function freeze()
$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);
Expand Down
Expand Up @@ -231,15 +231,6 @@ public function testExtensions()
$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');
}
}

/**
Expand Down

0 comments on commit b1e7996

Please sign in to comment.