merged branch fabpot/synthetic-services (PR #6500)

This PR was merged into the master branch.

Commits
-------

3cac604 [DependencyInjection] fixed setting a synthetic service on a frozen container

Discussion
----------

[DependencyInjection] fixed setting a synthetic service on a frozen container

By definition of a synthetic service, setting it on a frozen container should be possible.

---------------------------------------------------------------------------

by stof at 2012-12-28T08:44:44Z

This allows setting services which are not defined

---------------------------------------------------------------------------

by fabpot at 2012-12-28T08:49:29Z

@stof: right, thinking about it more, it is probably not a so good idea.

---------------------------------------------------------------------------

by fabpot at 2012-12-28T08:52:03Z

I've restricted the condition to only accept setting existing synthetic services. That should be ok.

---------------------------------------------------------------------------

by drak at 2012-12-28T19:10:23Z

This is a good enhancement but I would go further. It should still be possible to add parameters and definitions to the container after it's been compiled but not allow modification of existing services or parameters.
This commit is contained in:
Fabien Potencier 2012-12-28 23:53:14 +01:00
commit fc43ae4514
2 changed files with 34 additions and 1 deletions

View File

@ -311,7 +311,10 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
public function set($id, $service, $scope = self::SCOPE_CONTAINER)
{
if ($this->isFrozen()) {
throw new BadMethodCallException('Setting service on a frozen container is not allowed');
// setting a synthetic service on a frozen container is alright
if (!isset($this->definitions[$id]) || !$this->definitions[$id]->isSynthetic()) {
throw new BadMethodCallException('Setting service on a frozen container is not allowed');
}
}
$id = strtolower($id);

View File

@ -531,10 +531,40 @@ class ContainerBuilderTest extends \PHPUnit_Framework_TestCase
}
$container = new ContainerBuilder();
$container->setDefinition('a', new Definition('stdClass'));
$container->compile();
$container->set('a', new \stdClass());
}
/**
* @expectedException BadMethodCallException
*/
public function testThrowsExceptionWhenAddServiceOnAFrozenContainer()
{
if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
$this->markTestSkipped('The "Config" component is not available');
}
$container = new ContainerBuilder();
$container->compile();
$container->set('a', new \stdClass());
}
public function testNoExceptionWhenSetSyntheticServiceOnAFrozenContainer()
{
if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
$this->markTestSkipped('The "Config" component is not available');
}
$container = new ContainerBuilder();
$def = new Definition('stdClass');
$def->setSynthetic(true);
$container->setDefinition('a', $def);
$container->compile();
$container->set('a', $a = new \stdClass());
$this->assertEquals($a, $container->get('a'));
}
/**
* @expectedException BadMethodCallException
*/