Skip to content

[DI] allow decorators to reference their decorated service using the special .inner id #36389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
5.1.0
-----

* allow decorators to reference their decorated service using the special `.inner` id
* added support to autowire public typed properties in php 7.4
* added support for defining method calls, a configurator, and property setters in `InlineServiceConfigurator`
* added possibility to define abstract service arguments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,15 @@
* @author Fabien Potencier <fabien@symfony.com>
* @author Diego Saint Esteben <diego@saintesteben.me>
*/
class DecoratorServicePass implements CompilerPassInterface
class DecoratorServicePass extends AbstractRecursivePass
{
private $innerId = '.inner';

public function __construct(?string $innerId = '.inner')
{
$this->innerId = $innerId;
}

public function process(ContainerBuilder $container)
{
$definitions = new \SplPriorityQueue();
Expand All @@ -49,6 +56,10 @@ public function process(ContainerBuilder $container)
if (!$renamedId) {
$renamedId = $id.'.inner';
}

$this->currentId = $renamedId;
$this->processValue($definition);

$definition->innerServiceId = $renamedId;
$definition->decorationOnInvalid = $invalidBehavior;

Expand Down Expand Up @@ -96,4 +107,13 @@ public function process(ContainerBuilder $container)
$container->setAlias($inner, $id)->setPublic($public)->setPrivate($private);
}
}

protected function processValue($value, bool $isRoot = false)
{
if ($value instanceof Reference && $this->innerId === (string) $value) {
return new Reference($this->currentId, $value->getInvalidBehavior());
}

return parent::processValue($value, $isRoot);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\DependencyInjection\Compiler\DecoratorServicePass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Reference;

class DecoratorServicePassTest extends TestCase
{
Expand Down Expand Up @@ -242,6 +243,20 @@ public function testProcessLeavesServiceLocatorTagOnOriginalDefinition()
$this->assertEquals(['bar' => ['attr' => 'baz'], 'foobar' => ['attr' => 'bar']], $container->getDefinition('baz')->getTags());
}

public function testGenericInnerReference()
{
$container = new ContainerBuilder();
$container->register('foo');

$container->register('bar')
->setDecoratedService('foo')
->setProperty('prop', new Reference('.inner'));

$this->process($container);

$this->assertEquals(['prop' => new Reference('bar.inner')], $container->getDefinition('bar')->getProperties());
}

protected function process(ContainerBuilder $container)
{
$pass = new DecoratorServicePass();
Expand Down