Skip to content

Commit

Permalink
make it possible for bundles extensions to prepend settings into the …
Browse files Browse the repository at this point in the history
…application configuration of any Bundle
  • Loading branch information
lsmith77 committed Dec 7, 2012
1 parent 425f6f5 commit d7a1154
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
@@ -1,6 +1,12 @@
CHANGELOG
=========

2.2.0
-----

* added PrependExtensionInterface (to be able to allow extensions to prepend
application configuration settings for any Bundle)

2.1.0
-----

Expand Down
Expand Up @@ -29,6 +29,12 @@ public function process(ContainerBuilder $container)
$definitions = $container->getDefinitions();
$aliases = $container->getAliases();

foreach ($container->getExtensions() as $extension) {
if ($extension instanceof PrependExtensionInterface) {
$extension->prepend($container);
}
}

foreach ($container->getExtensions() as $name => $extension) {
if (!$config = $container->getExtensionConfig($name)) {
// this extension was not called
Expand Down
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\ContainerBuilder;

interface PrependExtensionInterface
{
/**
* Allow an extension to prepend the extension configurations.
*
* @param ContainerBuilder $container
*/
public function prepend(ContainerBuilder $container);
}
15 changes: 15 additions & 0 deletions src/Symfony/Component/DependencyInjection/ContainerBuilder.php
Expand Up @@ -465,6 +465,21 @@ public function getExtensionConfig($name)
return $this->extensionConfigs[$name];
}

/**
* Prepends a config array to the configs of the given extension.
*
* @param string $name The name of the extension
* @param array $config The config to set
*/
public function prependExtensionConfig($name, array $config)
{
if (!isset($this->extensionConfigs[$name])) {
$this->extensionConfigs[$name] = array();
}

array_unshift($this->extensionConfigs[$name], $config);
}

/**
* Compiles the container.
*
Expand Down
Expand Up @@ -548,6 +548,28 @@ public function testThrowsExceptionWhenSetDefinitionOnAFrozenContainer()
$container->compile();
$container->setDefinition('a', new Definition());
}

/**
* @covers Symfony\Component\DependencyInjection\ContainerBuilder::getExtensionConfig
* @covers Symfony\Component\DependencyInjection\ContainerBuilder::prependExtensionConfig
*/
public function testExtensionConfig()
{
$container = new ContainerBuilder();

$configs = $container->getExtensionConfig('foo');
$this->assertEmpty($configs);

$first = array('foo' => 'bar');
$container->prependExtensionConfig('foo', $first);
$configs = $container->getExtensionConfig('foo');
$this->assertEquals(array($first), $configs);

$second = array('ding' => 'dong');
$container->prependExtensionConfig('foo', $second);
$configs = $container->getExtensionConfig('foo');
$this->assertEquals(array($second, $first), $configs);
}
}

class FooClass {}
3 changes: 2 additions & 1 deletion src/Symfony/Component/HttpKernel/.gitignore
@@ -1,4 +1,5 @@
vendor/
composer.lock
phpunit.xml

Tests/ProjectContainer.php
Tests/classes.map

0 comments on commit d7a1154

Please sign in to comment.