Skip to content

Commit

Permalink
feature #22764 [DI] Remove deprecated dumping an uncompiled container…
Browse files Browse the repository at this point in the history
… (ro0NL)

This PR was merged into the 4.0-dev branch.

Discussion
----------

[DI] Remove deprecated dumping an uncompiled container

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | yes
| Deprecations? | no
| Tests pass?   | should be
| Fixed tickets | #... <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!--highly recommended for new features-->

See #20634

Commits
-------

c1c525c [DI] Remove deprecated dumping an uncompiled container
  • Loading branch information
nicolas-grekas committed May 22, 2017
2 parents 898516a + c1c525c commit 3892a95
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 79 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
-----

* removed `Container::isFrozen`
* removed support for dumping an ucompiled container in `PhpDumper`

3.3.0
-----
Expand Down
101 changes: 26 additions & 75 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Expand Up @@ -25,6 +25,7 @@
use Symfony\Component\DependencyInjection\Parameter;
use Symfony\Component\DependencyInjection\Exception\EnvParameterException;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
use Symfony\Component\DependencyInjection\LazyProxy\ProxyHelper;
Expand Down Expand Up @@ -80,7 +81,7 @@ class PhpDumper extends Dumper
public function __construct(ContainerBuilder $container)
{
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);
throw new LogicException('Cannot dump an uncompiled container.');
}

parent::__construct($container);
Expand Down Expand Up @@ -154,17 +155,11 @@ public function dump(array $options = array())
}
}

$code = $this->startClass($options['class'], $options['base_class'], $options['namespace']);

if ($this->container->isCompiled()) {
$code .= $this->addFrozenConstructor();
$code .= $this->addFrozenCompile();
$code .= $this->addFrozenIsCompiled();
} else {
$code .= $this->addConstructor();
}

$code .=
$code =
$this->startClass($options['class'], $options['base_class'], $options['namespace']).
$this->addConstructor().
$this->addCompile().
$this->addIsCompiled().
$this->addServices().
$this->addDefaultParametersMethod().
$this->endClass().
Expand Down Expand Up @@ -783,7 +778,6 @@ private function addNewInstance(Definition $definition, $return, $instantiation,
*/
private function startClass($class, $baseClass, $namespace)
{
$bagClass = $this->container->isCompiled() ? 'use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;' : 'use Symfony\Component\DependencyInjection\ParameterBag\\ParameterBag;';
$namespaceLine = $namespace ? "\nnamespace $namespace;\n" : '';

return <<<EOF
Expand All @@ -795,7 +789,7 @@ private function startClass($class, $baseClass, $namespace)
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
$bagClass
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
/*{$this->docStar}
* $class.
Expand All @@ -813,46 +807,12 @@ class $class extends $baseClass
EOF;
}

/**
* Adds the constructor.
*
* @return string
*/
private function addConstructor()
{
$targetDirs = $this->exportTargetDirs();
$arguments = $this->container->getParameterBag()->all() ? 'new ParameterBag($this->getDefaultParameters())' : null;

$code = <<<EOF
/*{$this->docStar}
* Constructor.
*/
public function __construct()
{{$targetDirs}
parent::__construct($arguments);
EOF;

$code .= $this->addNormalizedIds();
$code .= $this->addMethodMap();
$code .= $this->addPrivateServices();
$code .= $this->addAliases();

$code .= <<<'EOF'
}
EOF;

return $code;
}

/**
* Adds the constructor for a compiled container.
*
* @return string
*/
private function addFrozenConstructor()
private function addConstructor()
{
$targetDirs = $this->exportTargetDirs();

Expand Down Expand Up @@ -888,7 +848,7 @@ public function __construct()
*
* @return string
*/
private function addFrozenCompile()
private function addCompile()
{
return <<<EOF
Expand All @@ -908,7 +868,7 @@ public function compile()
*
* @return string
*/
private function addFrozenIsCompiled()
private function addIsCompiled()
{
return <<<EOF
Expand Down Expand Up @@ -1001,7 +961,7 @@ private function addPrivateServices()
private function addAliases()
{
if (!$aliases = $this->container->getAliases()) {
return $this->container->isCompiled() ? "\n \$this->aliases = array();\n" : '';
return "\n \$this->aliases = array();\n";
}

$code = " \$this->aliases = array(\n";
Expand Down Expand Up @@ -1046,9 +1006,7 @@ private function addDefaultParametersMethod()
}
$parameters = sprintf("array(\n%s\n%s)", implode("\n", $php), str_repeat(' ', 8));

$code = '';
if ($this->container->isCompiled()) {
$code .= <<<'EOF'
$code = <<<'EOF'
/**
* {@inheritdoc}
Expand Down Expand Up @@ -1102,13 +1060,13 @@ public function getParameterBag()
}
EOF;
if ('' === $this->docStar) {
$code = str_replace('/**', '/*', $code);
}
if ('' === $this->docStar) {
$code = str_replace('/**', '/*', $code);
}

if ($dynamicPhp) {
$loadedDynamicParameters = $this->exportParameters(array_combine(array_keys($dynamicPhp), array_fill(0, count($dynamicPhp), false)), '', 8);
$getDynamicParameter = <<<'EOF'
if ($dynamicPhp) {
$loadedDynamicParameters = $this->exportParameters(array_combine(array_keys($dynamicPhp), array_fill(0, count($dynamicPhp), false)), '', 8);
$getDynamicParameter = <<<'EOF'
switch ($name) {
%s
default: throw new InvalidArgumentException(sprintf('The dynamic parameter "%%s" must be defined.', $name));
Expand All @@ -1117,13 +1075,13 @@ public function getParameterBag()
return $this->dynamicParameters[$name] = $value;
EOF;
$getDynamicParameter = sprintf($getDynamicParameter, implode("\n", $dynamicPhp));
} else {
$loadedDynamicParameters = 'array()';
$getDynamicParameter = str_repeat(' ', 8).'throw new InvalidArgumentException(sprintf(\'The dynamic parameter "%s" must be defined.\', $name));';
}
$getDynamicParameter = sprintf($getDynamicParameter, implode("\n", $dynamicPhp));
} else {
$loadedDynamicParameters = 'array()';
$getDynamicParameter = str_repeat(' ', 8).'throw new InvalidArgumentException(sprintf(\'The dynamic parameter "%s" must be defined.\', $name));';
}

$code .= <<<EOF
$code .= <<<EOF
private \$loadedDynamicParameters = {$loadedDynamicParameters};
private \$dynamicParameters = array();
Expand All @@ -1142,13 +1100,6 @@ private function getDynamicParameter(\$name)
{$getDynamicParameter}
}
EOF;
} elseif ($dynamicPhp) {
throw new RuntimeException('You cannot dump a not-frozen container with dynamic parameters.');
}

$code .= <<<EOF
/*{$this->docStar}
* Gets the default parameters.
*
Expand Down Expand Up @@ -1595,7 +1546,7 @@ private function dumpLiteralClass($class)
*/
private function dumpParameter($name)
{
if ($this->container->isCompiled() && $this->container->hasParameter($name)) {
if ($this->container->hasParameter($name)) {
return $this->dumpValue($this->container->getParameter($name), false);
}

Expand Down
Expand Up @@ -130,14 +130,13 @@ public function testAddParameters()
}

/**
* @group legacy
* @expectedDeprecation Dumping an uncompiled ContainerBuilder is deprecated since version 3.3 and will not be supported anymore in 4.0. Compile the container beforehand.
* @expectedException \Symfony\Component\DependencyInjection\Exception\LogicException
* @expectedExceptionMessage Cannot dump an uncompiled container.
*/
public function testAddServiceWithoutCompilation()
{
$container = include self::$fixturesPath.'/containers/container9.php';
$dumper = new PhpDumper($container);
$this->assertEquals(str_replace('%path%', str_replace('\\', '\\\\', self::$fixturesPath.DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR), file_get_contents(self::$fixturesPath.'/php/services9.php')), $dumper->dump(), '->dump() dumps services');
new PhpDumper($container);
}

public function testAddService()
Expand Down

0 comments on commit 3892a95

Please sign in to comment.