Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8445564

Browse filesBrowse files
committed
bug #41779 [DependencyInjection] throw proper exception when decorating a synthetic service (nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- [DependencyInjection] throw proper exception when decorating a synthetic service | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #40784 | License | MIT | Doc PR | - Commits ------- b763a29 [DependencyInjection] throw proper exception when decorating a synthetic service
2 parents 8391e0b + b763a29 commit 8445564
Copy full SHA for 8445564

File tree

2 files changed

+25
-0
lines changed
Filter options

2 files changed

+25
-0
lines changed

‎src/Symfony/Component/DependencyInjection/Compiler/DecoratorServicePass.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Compiler/DecoratorServicePass.php
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\DependencyInjection\Alias;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616
use Symfony\Component\DependencyInjection\ContainerInterface;
17+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1718
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
1819
use Symfony\Component\DependencyInjection\Reference;
1920

@@ -59,6 +60,7 @@ public function process(ContainerBuilder $container)
5960
$public = $alias->isPublic();
6061
$private = $alias->isPrivate();
6162
$container->setAlias($renamedId, new Alias((string) $alias, false));
63+
$decoratedDefinition = $container->findDefinition($alias);
6264
} elseif ($container->hasDefinition($inner)) {
6365
$decoratedDefinition = $container->getDefinition($inner);
6466
$public = $decoratedDefinition->isPublic();
@@ -72,10 +74,15 @@ public function process(ContainerBuilder $container)
7274
} elseif (ContainerInterface::NULL_ON_INVALID_REFERENCE === $invalidBehavior) {
7375
$public = $definition->isPublic();
7476
$private = $definition->isPrivate();
77+
$decoratedDefinition = null;
7578
} else {
7679
throw new ServiceNotFoundException($inner, $id);
7780
}
7881

82+
if ($decoratedDefinition && $decoratedDefinition->isSynthetic()) {
83+
throw new InvalidArgumentException(sprintf('A synthetic service cannot be decorated: service "%s" cannot decorate "%s".', $id, $inner));
84+
}
85+
7986
if (isset($decoratingDefinitions[$inner])) {
8087
$decoratingDefinition = $decoratingDefinitions[$inner];
8188

‎src/Symfony/Component/DependencyInjection/Tests/Compiler/DecoratorServicePassTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Compiler/DecoratorServicePassTest.php
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\DependencyInjection\Compiler\DecoratorServicePass;
1717
use Symfony\Component\DependencyInjection\ContainerBuilder;
1818
use Symfony\Component\DependencyInjection\ContainerInterface;
19+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1920
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
2021

2122
class DecoratorServicePassTest extends TestCase
@@ -262,6 +263,23 @@ public function testProcessLeavesServiceSubscriberTagOnOriginalDefinition()
262263
$this->assertEquals(['bar' => ['attr' => 'baz'], 'foobar' => ['attr' => 'bar']], $container->getDefinition('baz')->getTags());
263264
}
264265

266+
public function testCannotDecorateSyntheticService()
267+
{
268+
$container = new ContainerBuilder();
269+
$container
270+
->register('foo')
271+
->setSynthetic(true)
272+
;
273+
$container
274+
->register('baz')
275+
->setDecoratedService('foo')
276+
;
277+
278+
$this->expectException(InvalidArgumentException::class);
279+
$this->expectExceptionMessage('A synthetic service cannot be decorated: service "baz" cannot decorate "foo".');
280+
$this->process($container);
281+
}
282+
265283
protected function process(ContainerBuilder $container)
266284
{
267285
$repeatedPass = new DecoratorServicePass();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.