8000 bug #20543 [DI] Fix error when trying to resolve a DefinitionDecorato… · symfony/symfony@015fe87 · GitHub
[go: up one dir, main page]

Skip to content

Commit 015fe87

Browse files
committed
bug #20543 [DI] Fix error when trying to resolve a DefinitionDecorator (nicolas-grekas)
This PR was merged into the 2.7 branch. Discussion ---------- [DI] Fix error when trying to resolve a DefinitionDecorator | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Instead of the currently obscure `ReflectionException: Class does not exist` message, let's throw a useful error message. Commits ------- 8e0da2f [DI] Fix error when trying to resolve a DefinitionDecorator
2 parents d34333f + 8e0da2f commit 015fe87

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,10 @@ public function findDefinition($id)
840840
*/
841841
public function createService(Definition $definition, $id, $tryProxy = true)
842842
{
843+
if ('Symfony\Component\DependencyInjection\Definition' !== get_class($definition)) {
844+
throw new RuntimeException(sprintf('Constructing service "%s" from a %s is not supported at build time.', $id, get_class($definition)));
845+
}
846+
843847
if ($definition->isSynthetic()) {
844848
throw new RuntimeException(sprintf('You have requested a synthetic service ("%s"). The DIC does not know how to construct this service.', $id));
845849
}

src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\DependencyInjection\ContainerBuilder;
2020
use Symfony\Component\DependencyInjection\ContainerInterface;
2121
use Symfony\Component\DependencyInjection\Definition;
22+
use Symfony\Component\DependencyInjection\DefinitionDecorator;
2223
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
2324
use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;
2425
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
@@ -404,6 +405,20 @@ public function testResolveServices()
404405
$this->assertEquals($builder->get('foo'), $builder->resolveServices(new Expression('service("foo")')), '->resolveServices() resolves expressions');
405406
}
406407

408+
/**
409+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
410+
* @expectedExceptionMessage Constructing service "foo" from a Symfony\Component\DependencyInjection\DefinitionDecorator is not supported at build time.
411+
*/
412+
public function testResolveServicesWithDecoratedDefinition()
413+
{
414+
$builder = new ContainerBuilder();
415+
$builder->setDefinition('grandpa', new Definition('stdClass'));
416+
$builder->setDefinition('parent', new DefinitionDecorator('grandpa'));
417+
$builder->setDefinition('foo', new DefinitionDecorator('parent'));
418+
419+
$builder->get('foo');
420+
}
421+
407422
public function testMerge()
408423
{
409424
$container = new ContainerBuilder(new ParameterBag(array('bar' => 'foo')));

0 commit comments

Comments
 (0)
0