[DI] Deprecate Container::isFrozen and introduce isCompiled

This commit is contained in:
Roland Franssen 2016-08-19 12:18:08 +00:00
parent 6327b4161d
commit 6abd312800
25 changed files with 275 additions and 48 deletions

View File

@ -77,6 +77,7 @@ class Container implements ResettableContainerInterface
private $underscoreMap = array('_' => '', '.' => '_', '\\' => '_');
private $envCache = array();
private $compiled = false;
/**
* @param ParameterBagInterface $parameterBag A ParameterBagInterface instance
@ -99,15 +100,31 @@ class Container implements ResettableContainerInterface
$this->parameterBag->resolve();
$this->parameterBag = new FrozenParameterBag($this->parameterBag->all());
$this->compiled = true;
}
/**
* Returns true if the container is compiled.
*
* @return bool
*/
public function isCompiled()
{
return $this->compiled;
}
/**
* Returns true if the container parameter bag are frozen.
*
* Deprecated since 3.3, to be removed in 4.0.
*
* @return bool true if the container parameter bag are frozen, false otherwise
*/
public function isFrozen()
{
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Use the isCompiled() method instead.', __METHOD__), E_USER_DEPRECATED);
return $this->parameterBag instanceof FrozenParameterBag;
}

View File

@ -416,13 +416,13 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
*
* @return $this
*
* @throws BadMethodCallException When this ContainerBuilder is frozen
* @throws \LogicException if the container is frozen
* @throws BadMethodCallException When this ContainerBuilder is compiled
* @throws \LogicException if the extension is not registered
*/
public function loadFromExtension($extension, array $values = array())
{
if ($this->isFrozen()) {
throw new BadMethodCallException('Cannot load from an extension on a frozen container.');
if ($this->isCompiled()) {
throw new BadMethodCallException('Cannot load from an extension on a compiled container.');
}
$namespace = $this->getExtension($extension)->getAlias();
@ -493,13 +493,13 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
* @param string $id The service identifier
* @param object $service The service instance
*
* @throws BadMethodCallException When this ContainerBuilder is frozen
* @throws BadMethodCallException When this ContainerBuilder is compiled
*/
public function set($id, $service)
{
$id = $this->normalizeId($id);
if ($this->isFrozen() && (isset($this->definitions[$id]) && !$this->definitions[$id]->isSynthetic())) {
if ($this->isCompiled() && (isset($this->definitions[$id]) && !$this->definitions[$id]->isSynthetic())) {
// setting a synthetic service on a frozen container is alright
throw new BadMethodCallException(sprintf('Setting service "%s" for an unknown or non-synthetic service definition on a frozen container is not allowed.', $id));
}
@ -601,12 +601,12 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
*
* @param ContainerBuilder $container The ContainerBuilder instance to merge
*
* @throws BadMethodCallException When this ContainerBuilder is frozen
* @throws BadMethodCallException When this ContainerBuilder is compiled
*/
public function merge(ContainerBuilder $container)
{
if ($this->isFrozen()) {
throw new BadMethodCallException('Cannot merge on a frozen container.');
if ($this->isCompiled()) {
throw new BadMethodCallException('Cannot merge on a compiled container.');
}
$this->addDefinitions($container->getDefinitions());
@ -922,12 +922,12 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
*
* @return Definition the service definition
*
* @throws BadMethodCallException When this ContainerBuilder is frozen
* @throws BadMethodCallException When this ContainerBuilder is compiled
*/
public function setDefinition($id, Definition $definition)
{
if ($this->isFrozen()) {
throw new BadMethodCallException('Adding definition to a frozen container is not allowed');
if ($this->isCompiled()) {
throw new BadMethodCallException('Adding definition to a compiled container is not allowed');
}
$id = $this->normalizeId($id);

View File

@ -83,7 +83,7 @@ class PhpDumper extends Dumper
*/
public function __construct(ContainerBuilder $container)
{
if (!$container->isFrozen()) {
if (!$container->isCompiled()) {
@trigger_error('Dumping an uncompiled ContainerBuilder is deprecated since version 3.3 and will not be supported anymore in 4.0. Compile the container beforehand.', E_USER_DEPRECATED);
}
@ -162,10 +162,10 @@ class PhpDumper extends Dumper
$code = $this->startClass($options['class'], $options['base_class'], $options['namespace']);
if ($this->container->isFrozen()) {
if ($this->container->isCompiled()) {
$code .= $this->addFrozenConstructor();
$code .= $this->addFrozenCompile();
$code .= $this->addIsFrozenMethod();
$code .= $this->addFrozenIsCompiled();
} else {
$code .= $this->addConstructor();
}
@ -893,7 +893,7 @@ EOF;
*/
private function startClass($class, $baseClass, $namespace)
{
$bagClass = $this->container->isFrozen() ? 'use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;' : 'use Symfony\Component\DependencyInjection\ParameterBag\\ParameterBag;';
$bagClass = $this->container->isCompiled() ? 'use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;' : 'use Symfony\Component\DependencyInjection\ParameterBag\\ParameterBag;';
$namespaceLine = $namespace ? "namespace $namespace;\n" : '';
return <<<EOF
@ -958,7 +958,7 @@ EOF;
}
/**
* Adds the constructor for a frozen container.
* Adds the constructor for a compiled container.
*
* @return string
*/
@ -994,7 +994,7 @@ EOF;
}
/**
* Adds the constructor for a frozen container.
* Adds the compile method for a compiled container.
*
* @return string
*/
@ -1007,26 +1007,36 @@ EOF;
*/
public function compile()
{
throw new LogicException('You cannot compile a dumped frozen container.');
throw new LogicException('You cannot compile a dumped container that was already compiled.');
}
EOF;
}
/**
* Adds the isFrozen method for a frozen container.
* Adds the isCompiled method for a compiled container.
*
* @return string
*/
private function addIsFrozenMethod()
private function addFrozenIsCompiled()
{
return <<<EOF
/*{$this->docStar}
* {@inheritdoc}
*/
public function isCompiled()
{
return true;
}
/*{$this->docStar}
* {@inheritdoc}
*/
public function isFrozen()
{
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Use the isCompiled() method instead.', __METHOD__), E_USER_DEPRECATED);
return true;
}
@ -1112,7 +1122,7 @@ EOF;
private function addAliases()
{
if (!$aliases = $this->container->getAliases()) {
return $this->container->isFrozen() ? "\n \$this->aliases = array();\n" : '';
return $this->container->isCompiled() ? "\n \$this->aliases = array();\n" : '';
}
$code = " \$this->aliases = array(\n";
@ -1158,7 +1168,7 @@ EOF;
$parameters = sprintf("array(\n%s\n%s)", implode("\n", $php), str_repeat(' ', 8));
$code = '';
if ($this->container->isFrozen()) {
if ($this->container->isCompiled()) {
$code .= <<<'EOF'
/**
@ -1721,7 +1731,7 @@ EOF;
*/
private function dumpParameter($name)
{
if ($this->container->isFrozen() && $this->container->hasParameter($name)) {
if ($this->container->isCompiled() && $this->container->hasParameter($name)) {
return $this->dumpValue($this->container->getParameter($name), false);
}

View File

@ -73,7 +73,7 @@ class XmlDumper extends Dumper
return;
}
if ($this->container->isFrozen()) {
if ($this->container->isCompiled()) {
$data = $this->escape($data);
}

View File

@ -218,7 +218,7 @@ class YamlDumper extends Dumper
return '';
}
$parameters = $this->prepareParameters($this->container->getParameterBag()->all(), $this->container->isFrozen());
$parameters = $this->prepareParameters($this->container->getParameterBag()->all(), $this->container->isCompiled());
return $this->dumper->dump(array('parameters' => $parameters), 2);
}

View File

@ -848,7 +848,7 @@ class ContainerBuilderTest extends TestCase
/**
* @expectedException \BadMethodCallException
*/
public function testThrowsExceptionWhenSetServiceOnAFrozenContainer()
public function testThrowsExceptionWhenSetServiceOnACompiledContainer()
{
$container = new ContainerBuilder();
$container->setResourceTracking(false);
@ -857,7 +857,7 @@ class ContainerBuilderTest extends TestCase
$container->set('a', new \stdClass());
}
public function testThrowsExceptionWhenAddServiceOnAFrozenContainer()
public function testThrowsExceptionWhenAddServiceOnACompiledContainer()
{
$container = new ContainerBuilder();
$container->compile();
@ -865,7 +865,7 @@ class ContainerBuilderTest extends TestCase
$this->assertSame($foo, $container->get('a'));
}
public function testNoExceptionWhenSetSyntheticServiceOnAFrozenContainer()
public function testNoExceptionWhenSetSyntheticServiceOnACompiledContainer()
{
$container = new ContainerBuilder();
$def = new Definition('stdClass');
@ -879,7 +879,7 @@ class ContainerBuilderTest extends TestCase
/**
* @expectedException \BadMethodCallException
*/
public function testThrowsExceptionWhenSetDefinitionOnAFrozenContainer()
public function testThrowsExceptionWhenSetDefinitionOnACompiledContainer()
{
$container = new ContainerBuilder();
$container->setResourceTracking(false);

View File

@ -16,6 +16,7 @@ use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
class ContainerTest extends TestCase
{
@ -82,6 +83,11 @@ class ContainerTest extends TestCase
$this->assertEquals(array('foo' => 'bar'), $sc->getParameterBag()->all(), '->compile() copies the current parameters to the new parameter bag');
}
/**
* @group legacy
* @expectedDeprecation The Symfony\Component\DependencyInjection\Container::isFrozen() method is deprecated since version 3.3 and will be removed in 4.0. Use the isCompiled() method instead.
* @expectedDeprecation The Symfony\Component\DependencyInjection\Container::isFrozen() method is deprecated since version 3.3 and will be removed in 4.0. Use the isCompiled() method instead.
*/
public function testIsFrozen()
{
$sc = new Container(new ParameterBag(array('foo' => 'bar')));
@ -90,6 +96,20 @@ class ContainerTest extends TestCase
$this->assertTrue($sc->isFrozen(), '->isFrozen() returns true if the parameters are frozen');
}
public function testIsCompiled()
{
$sc = new Container(new ParameterBag(array('foo' => 'bar')));
$this->assertFalse($sc->isCompiled(), '->isCompiled() returns false if the container is not compiled');
$sc->compile();
$this->assertTrue($sc->isCompiled(), '->isCompiled() returns true if the container is compiled');
}
public function testIsCompiledWithFrozenParameters()
{
$sc = new Container(new FrozenParameterBag(array('foo' => 'bar')));
$this->assertFalse($sc->isCompiled(), '->isCompiled() returns false if the container is not compiled but the parameter bag is already frozen');
}
public function testGetParameterBag()
{
$sc = new Container();

View File

@ -41,7 +41,15 @@ class Container extends AbstractContainer
*/
public function compile()
{
throw new LogicException('You cannot compile a dumped frozen container.');
throw new LogicException('You cannot compile a dumped container that was already compiled.');
}
/**
* {@inheritdoc}
*/
public function isCompiled()
{
return true;
}
/**
@ -49,6 +57,8 @@ class Container extends AbstractContainer
*/
public function isFrozen()
{
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Use the isCompiled() method instead.', __METHOD__), E_USER_DEPRECATED);
return true;
}
}

View File

@ -40,7 +40,15 @@ class ProjectServiceContainer extends Container
*/
public function compile()
{
throw new LogicException('You cannot compile a dumped frozen container.');
throw new LogicException('You cannot compile a dumped container that was already compiled.');
}
/**
* {@inheritdoc}
*/
public function isCompiled()
{
return true;
}
/**
@ -48,6 +56,8 @@ class ProjectServiceContainer extends Container
*/
public function isFrozen()
{
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Use the isCompiled() method instead.', __METHOD__), E_USER_DEPRECATED);
return true;
}
}

View File

@ -45,7 +45,15 @@ class ProjectServiceContainer extends Container
*/
public function compile()
{
throw new LogicException('You cannot compile a dumped frozen container.');
throw new LogicException('You cannot compile a dumped container that was already compiled.');
}
/**
* {@inheritdoc}
*/
public function isCompiled()
{
return true;
}
/**
@ -53,6 +61,8 @@ class ProjectServiceContainer extends Container
*/
public function isFrozen()
{
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Use the isCompiled() method instead.', __METHOD__), E_USER_DEPRECATED);
return true;
}

View File

@ -49,7 +49,15 @@ class ProjectServiceContainer extends Container
*/
public function compile()
{
throw new LogicException('You cannot compile a dumped frozen container.');
throw new LogicException('You cannot compile a dumped container that was already compiled.');
}
/**
* {@inheritdoc}
*/
public function isCompiled()
{
return true;
}
/**
@ -57,6 +65,8 @@ class ProjectServiceContainer extends Container
*/
public function isFrozen()
{
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Use the isCompiled() method instead.', __METHOD__), E_USER_DEPRECATED);
return true;
}

View File

@ -43,7 +43,15 @@ class ProjectServiceContainer extends Container
*/
public function compile()
{
throw new LogicException('You cannot compile a dumped frozen container.');
throw new LogicException('You cannot compile a dumped container that was already compiled.');
}
/**
* {@inheritdoc}
*/
public function isCompiled()
{
return true;
}
/**
@ -51,6 +59,8 @@ class ProjectServiceContainer extends Container
*/
public function isFrozen()
{
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Use the isCompiled() method instead.', __METHOD__), E_USER_DEPRECATED);
return true;
}

View File

@ -44,7 +44,15 @@ class ProjectServiceContainer extends Container
*/
public function compile()
{
throw new LogicException('You cannot compile a dumped frozen container.');
throw new LogicException('You cannot compile a dumped container that was already compiled.');
}
/**
* {@inheritdoc}
*/
public function isCompiled()
{
return true;
}
/**
@ -52,6 +60,8 @@ class ProjectServiceContainer extends Container
*/
public function isFrozen()
{
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Use the isCompiled() method instead.', __METHOD__), E_USER_DEPRECATED);
return true;
}

View File

@ -43,7 +43,15 @@ class ProjectServiceContainer extends Container
*/
public function compile()
{
throw new LogicException('You cannot compile a dumped frozen container.');
throw new LogicException('You cannot compile a dumped container that was already compiled.');
}
/**
* {@inheritdoc}
*/
public function isCompiled()
{
return true;
}
/**
@ -51,6 +59,8 @@ class ProjectServiceContainer extends Container
*/
public function isFrozen()
{
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Use the isCompiled() method instead.', __METHOD__), E_USER_DEPRECATED);
return true;
}

View File

@ -45,7 +45,15 @@ class ProjectServiceContainer extends Container
*/
public function compile()
{
throw new LogicException('You cannot compile a dumped frozen container.');
throw new LogicException('You cannot compile a dumped container that was already compiled.');
}
/**
* {@inheritdoc}
*/
public function isCompiled()
{
return true;
}
/**
@ -53,6 +61,8 @@ class ProjectServiceContainer extends Container
*/
public function isFrozen()
{
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Use the isCompiled() method instead.', __METHOD__), E_USER_DEPRECATED);
return true;
}

View File

@ -44,7 +44,15 @@ class Symfony_DI_PhpDumper_Test_Overriden_Getters extends Container
*/
public function compile()
{
throw new LogicException('You cannot compile a dumped frozen container.');
throw new LogicException('You cannot compile a dumped container that was already compiled.');
}
/**
* {@inheritdoc}
*/
public function isCompiled()
{
return true;
}
/**
@ -52,6 +60,8 @@ class Symfony_DI_PhpDumper_Test_Overriden_Getters extends Container
*/
public function isFrozen()
{
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Use the isCompiled() method instead.', __METHOD__), E_USER_DEPRECATED);
return true;
}

View File

@ -44,7 +44,15 @@ class ProjectServiceContainer extends Container
*/
public function compile()
{
throw new LogicException('You cannot compile a dumped frozen container.');
throw new LogicException('You cannot compile a dumped container that was already compiled.');
}
/**
* {@inheritdoc}
*/
public function isCompiled()
{
return true;
}
/**
@ -52,6 +60,8 @@ class ProjectServiceContainer extends Container
*/
public function isFrozen()
{
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Use the isCompiled() method instead.', __METHOD__), E_USER_DEPRECATED);
return true;
}

View File

@ -44,7 +44,15 @@ class ProjectServiceContainer extends Container
*/
public function compile()
{
throw new LogicException('You cannot compile a dumped frozen container.');
throw new LogicException('You cannot compile a dumped container that was already compiled.');
}
/**
* {@inheritdoc}
*/
public function isCompiled()
{
return true;
}
/**
@ -52,6 +60,8 @@ class ProjectServiceContainer extends Container
*/
public function isFrozen()
{
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Use the isCompiled() method instead.', __METHOD__), E_USER_DEPRECATED);
return true;
}

View File

@ -44,7 +44,15 @@ class ProjectServiceContainer extends Container
*/
public function compile()
{
throw new LogicException('You cannot compile a dumped frozen container.');
throw new LogicException('You cannot compile a dumped container that was already compiled.');
}
/**
* {@inheritdoc}
*/
public function isCompiled()
{
return true;
}
/**
@ -52,6 +60,8 @@ class ProjectServiceContainer extends Container
*/
public function isFrozen()
{
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Use the isCompiled() method instead.', __METHOD__), E_USER_DEPRECATED);
return true;
}

View File

@ -42,7 +42,15 @@ class ProjectServiceContainer extends Container
*/
public function compile()
{
throw new LogicException('You cannot compile a dumped frozen container.');
throw new LogicException('You cannot compile a dumped container that was already compiled.');
}
/**
* {@inheritdoc}
*/
public function isCompiled()
{
return true;
}
/**
@ -50,6 +58,8 @@ class ProjectServiceContainer extends Container
*/
public function isFrozen()
{
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Use the isCompiled() method instead.', __METHOD__), E_USER_DEPRECATED);
return true;
}

View File

@ -66,7 +66,15 @@ class ProjectServiceContainer extends Container
*/
public function compile()
{
throw new LogicException('You cannot compile a dumped frozen container.');
throw new LogicException('You cannot compile a dumped container that was already compiled.');
}
/**
* {@inheritdoc}
*/
public function isCompiled()
{
return true;
}
/**
@ -74,6 +82,8 @@ class ProjectServiceContainer extends Container
*/
public function isFrozen()
{
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Use the isCompiled() method instead.', __METHOD__), E_USER_DEPRECATED);
return true;
}

View File

@ -44,7 +44,15 @@ class Symfony_DI_PhpDumper_Test_Overriden_Getters_With_Constructor extends Conta
*/
public function compile()
{
throw new LogicException('You cannot compile a dumped frozen container.');
throw new LogicException('You cannot compile a dumped container that was already compiled.');
}
/**
* {@inheritdoc}
*/
public function isCompiled()
{
return true;
}
/**
@ -52,6 +60,8 @@ class Symfony_DI_PhpDumper_Test_Overriden_Getters_With_Constructor extends Conta
*/
public function isFrozen()
{
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Use the isCompiled() method instead.', __METHOD__), E_USER_DEPRECATED);
return true;
}

View File

@ -44,7 +44,15 @@ class ProjectServiceContainer extends Container
*/
public function compile()
{
throw new LogicException('You cannot compile a dumped frozen container.');
throw new LogicException('You cannot compile a dumped container that was already compiled.');
}
/**
* {@inheritdoc}
*/
public function isCompiled()
{
return true;
}
/**
@ -52,6 +60,8 @@ class ProjectServiceContainer extends Container
*/
public function isFrozen()
{
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Use the isCompiled() method instead.', __METHOD__), E_USER_DEPRECATED);
return true;
}

View File

@ -48,7 +48,15 @@ class ProjectServiceContainer extends Container
*/
public function compile()
{
throw new LogicException('You cannot compile a dumped frozen container.');
throw new LogicException('You cannot compile a dumped container that was already compiled.');
}
/**
* {@inheritdoc}
*/
public function isCompiled()
{
return true;
}
/**
@ -56,6 +64,8 @@ class ProjectServiceContainer extends Container
*/
public function isFrozen()
{
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Use the isCompiled() method instead.', __METHOD__), E_USER_DEPRECATED);
return true;
}

View File

@ -48,7 +48,15 @@ class ProjectServiceContainer extends Container
*/
public function compile()
{
throw new LogicException('You cannot compile a dumped frozen container.');
throw new LogicException('You cannot compile a dumped container that was already compiled.');
}
/**
* {@inheritdoc}
*/
public function isCompiled()
{
return true;
}
/**
@ -56,6 +64,8 @@ class ProjectServiceContainer extends Container
*/
public function isFrozen()
{
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Use the isCompiled() method instead.', __METHOD__), E_USER_DEPRECATED);
return true;
}