[DependencyInjection] fixed setting a synthetic service on a frozen container
This commit is contained in:
parent
35f5bca585
commit
3cac604352
@ -311,7 +311,10 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
|
|||||||
public function set($id, $service, $scope = self::SCOPE_CONTAINER)
|
public function set($id, $service, $scope = self::SCOPE_CONTAINER)
|
||||||
{
|
{
|
||||||
if ($this->isFrozen()) {
|
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);
|
$id = strtolower($id);
|
||||||
|
@ -531,10 +531,40 @@ class ContainerBuilderTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
$container = new ContainerBuilder();
|
$container = new ContainerBuilder();
|
||||||
|
$container->setDefinition('a', new Definition('stdClass'));
|
||||||
$container->compile();
|
$container->compile();
|
||||||
$container->set('a', new \stdClass());
|
$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
|
* @expectedException BadMethodCallException
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user