Skip to content

Commit

Permalink
[DI] fix dumping errored definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Jun 23, 2018
1 parent 72bf72a commit 6285e68
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
12 changes: 3 additions & 9 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -261,6 +260,7 @@ public function dump(array $options = array())
EOF;
} else {
$code .= $this->endClass();
foreach ($this->generateProxyClasses() as $c) {
$code .= $c;
}
Expand Down Expand Up @@ -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();
Expand Down
Expand Up @@ -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);
Expand Down
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -540,6 +552,11 @@ class ProjectServiceContainer extends Container
'foo' => 'bar',
);
}

protected function throw($message)
{
throw new RuntimeException($message);
}
}

[ProjectServiceContainer.php] => <?php
Expand Down

0 comments on commit 6285e68

Please sign in to comment.