bug #27688 [DI] fix dumping errored definitions (nicolas-grekas)

This PR was merged into the 4.2-dev branch.

Discussion
----------

[DI] fix dumping errored definitions

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Removes dead code and correctly adds the "throw()" method at class' end.

Commits
-------

6285e68fa6 [DI] fix dumping errored definitions
This commit is contained in:
Nicolas Grekas 2018-06-24 09:55:11 +02:00
commit df44236ef3
3 changed files with 25 additions and 9 deletions

View File

@ -186,8 +186,7 @@ class PhpDumper extends Dumper
$code =
$this->startClass($options['class'], $baseClass, $baseClassWithNamespace).
$this->addServices().
$this->addDefaultParametersMethod().
$this->endClass()
$this->addDefaultParametersMethod()
;
if ($this->asFiles) {
@ -223,7 +222,7 @@ EOF;
foreach ($this->generateProxyClasses() as $file => $c) {
$files[$file] = "<?php\n".$c;
}
$files[$options['class'].'.php'] = $code;
$files[$options['class'].'.php'] = $code.$this->endClass();
$hash = ucfirst(strtr(ContainerBuilder::hash($files), '._', 'xx'));
$code = array();
@ -261,6 +260,7 @@ return new \\Container{$hash}\\{$options['class']}(array(
EOF;
} else {
$code .= $this->endClass();
foreach ($this->generateProxyClasses() as $c) {
$code .= $c;
}
@ -755,12 +755,6 @@ EOTXT
EOF;
}
if ($e = $definition->getErrors()) {
$e = sprintf("throw new RuntimeException(%s);\n", $this->export(reset($e)));
return $asFile ? substr($code, 8).$e : $code.' '.$e." }\n";
}
$inlinedDefinitions = $this->getDefinitionsFromArguments(array($definition));
$constructorDefinitions = $this->getDefinitionsFromArguments(array($definition->getArguments(), $definition->getFactory()));
$otherDefinitions = new \SplObjectStorage();

View File

@ -213,6 +213,11 @@ class PhpDumperTest extends TestCase
->setFile(realpath(self::$fixturesPath.'/includes/foo.php'))
->setShared(false)
->setPublic(true);
$container->register('throwing_one', \Bar\FooClass::class)
->addArgument(new Reference('errored_one', ContainerBuilder::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE))
->setPublic(true);
$container->register('errored_one', 'stdClass')
->addError('No-no-no-no');
$container->compile();
$dumper = new PhpDumper($container);
$dump = print_r($dumper->dump(array('as_files' => true, 'file' => __DIR__, 'hot_path_tag' => 'hot')), true);

View File

@ -10,6 +10,7 @@ return array(
'decorated.pif-pouf' => true,
'decorator_service.inner' => true,
'errored_definition' => true,
'errored_one' => true,
'factory_simple' => true,
'inlined' => true,
'new_factory' => true,
@ -341,6 +342,16 @@ return $this->services['tagged_iterator'] = new \Bar(new RewindableGenerator(fun
yield 1 => ($this->privates['tagged_iterator_foo'] ?? $this->privates['tagged_iterator_foo'] = new \Bar());
}, 2));
[Container%s/getThrowingOneService.php] => <?php
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
// This file has been auto-generated by the Symfony Dependency Injection Component for internal use.
// Returns the public 'throwing_one' shared service.
return $this->services['throwing_one'] = new \Bar\FooClass($this->throw('No-no-no-no'));
[Container%s/ProjectServiceContainer.php] => <?php
namespace Container%s;
@ -412,6 +423,7 @@ class ProjectServiceContainer extends Container
'runtime_error' => 'getRuntimeErrorService.php',
'service_from_static_method' => 'getServiceFromStaticMethodService.php',
'tagged_iterator' => 'getTaggedIteratorService.php',
'throwing_one' => 'getThrowingOneService.php',
);
$this->aliases = array(
'alias_for_alias' => 'foo',
@ -540,6 +552,11 @@ class ProjectServiceContainer extends Container
'foo' => 'bar',
);
}
protected function throw($message)
{
throw new RuntimeException($message);
}
}
[ProjectServiceContainer.php] => <?php