Skip to content
Permalink
Browse files

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
-------

6285e68 [DI] fix dumping errored definitions
  • Loading branch information...
nicolas-grekas committed Jun 24, 2018
2 parents 72bf72a + 6285e68 commit df44236ef3c1532597457b7f6955aec37840d187
@@ -186,8 +186,7 @@ public function dump(array $options = array())
$code =
$this->startClass($options['class'], $baseClass, $baseClassWithNamespace).
$this->addServices().
$this->addDefaultParametersMethod().
$this->endClass()
$this->addDefaultParametersMethod()
;
if ($this->asFiles) {
@@ -223,7 +222,7 @@ public function dump(array $options = array())
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 @@ public function dump(array $options = array())
EOF;
} else {
$code .= $this->endClass();
foreach ($this->generateProxyClasses() as $c) {
$code .= $c;
}
@@ -755,12 +755,6 @@ protected function {$methodName}($lazyInitialization)
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();
@@ -213,6 +213,11 @@ public function testDumpAsFiles()
->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);
@@ -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

0 comments on commit df44236

Please sign in to comment.
You can’t perform that action at this time.