Skip to content

[DI] Remove deprecated dumping an uncompiled container #22764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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