Skip to content

[DependencyInjection] add AsDecorator class attribute and InnerService parameter attribute #45834

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 19, 2022
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Attribute;

use Symfony\Component\DependencyInjection\ContainerInterface;

#[\Attribute(\Attribute::TARGET_CLASS)]
class AsDecorator
{
public function __construct(
public string $decorates,
public int $priority = 0,
public int $onInvalid = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Attribute;

#[\Attribute(\Attribute::TARGET_PARAMETER)]
class InnerService
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

/**
* Reads #[AsDecorator] attributes on definitions that are autowired
* and don't have the "container.ignore_attributes" tag.
*/
final class AutowireAsDecoratorPass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
foreach ($container->getDefinitions() as $definition) {
if ($this->accept($definition) && $reflectionClass = $container->getReflectionClass($definition->getClass(), false)) {
$this->processClass($definition, $reflectionClass);
}
}
}

private function accept(Definition $definition): bool
{
return !$definition->hasTag('container.ignore_attributes') && $definition->isAutowired();
}

private function processClass(Definition $definition, \ReflectionClass $reflectionClass)
{
foreach ($reflectionClass->getAttributes(AsDecorator::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
$attribute = $attribute->newInstance();

$definition->setDecoratedService($attribute->decorates, null, $attribute->priority, $attribute->onInvalid);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\Attribute\InnerService;
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
use Symfony\Component\DependencyInjection\Attribute\Target;
Expand Down Expand Up @@ -271,6 +272,12 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a

break;
}

if (InnerService::class === $attribute->getName()) {
$arguments[$index] = new Reference($this->currentId.'.inner', ContainerInterface::NULL_ON_INVALID_REFERENCE);

break;
}
}

if ('' !== ($arguments[$index] ?? '')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function __construct()
100 => [
new ResolveClassPass(),
new RegisterAutoconfigureAttributesPass(),
new AutowireAsDecoratorPass(),
new AttributeAutoconfigurationPass(),
new ResolveInstanceofConditionalsPass(),
new RegisterEnvVarProcessorsPass(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Compiler\AutowireAsDecoratorPass;
use Symfony\Component\DependencyInjection\Compiler\AutowirePass;
use Symfony\Component\DependencyInjection\Compiler\AutowireRequiredMethodsPass;
use Symfony\Component\DependencyInjection\Compiler\DecoratorServicePass;
Expand Down Expand Up @@ -1164,4 +1165,25 @@ public function testAutowireAttribute()
$this->assertSame('@bar', $service->escapedRawValue);
$this->assertNull($service->invalid);
}

public function testAsDecoratorAttribute()
{
$container = new ContainerBuilder();

$container->register(AsDecoratorFoo::class);
$container->register(AsDecoratorBar10::class)->setAutowired(true)->setArgument(0, 'arg1');
$container->register(AsDecoratorBar20::class)->setAutowired(true)->setArgument(0, 'arg1');
$container->register(AsDecoratorBaz::class)->setAutowired(true);

(new ResolveClassPass())->process($container);
(new AutowireAsDecoratorPass())->process($container);
(new DecoratorServicePass())->process($container);
(new AutowirePass())->process($container);

$this->assertSame(AsDecoratorBar10::class.'.inner', (string) $container->getDefinition(AsDecoratorBar10::class)->getArgument(1));

$this->assertSame(AsDecoratorBar20::class.'.inner', (string) $container->getDefinition(AsDecoratorBar20::class)->getArgument(1));
$this->assertSame(AsDecoratorBaz::class.'.inner', (string) $container->getDefinition(AsDecoratorBaz::class)->getArgument(0));
$this->assertSame(2, $container->getDefinition(AsDecoratorBaz::class)->getArgument(0)->getInvalidBehavior());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace Symfony\Component\DependencyInjection\Tests\Compiler;

use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\Attribute\InnerService;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Contracts\Service\Attribute\Required;

class AutowireSetter
Expand Down Expand Up @@ -50,3 +53,35 @@ public function __construct(
) {
}
}

interface AsDecoratorInterface
{
}

class AsDecoratorFoo implements AsDecoratorInterface
{
}

#[AsDecorator(decorates: AsDecoratorFoo::class, priority: 10)]
class AsDecoratorBar10 implements AsDecoratorInterface
{
public function __construct(string $arg1, #[InnerService] AsDecoratorInterface $inner)
{
}
}

#[AsDecorator(decorates: AsDecoratorFoo::class, priority: 20)]
class AsDecoratorBar20 implements AsDecoratorInterface
{
public function __construct(string $arg1, #[InnerService] AsDecoratorInterface $inner)
{
}
}

#[AsDecorator(decorates: \NonExistent::class, onInvalid: ContainerInterface::NULL_ON_INVALID_REFERENCE)]
class AsDecoratorBaz implements AsDecoratorInterface
{
public function __construct(#[InnerService] AsDecoratorInterface $inner = null)
{
}
}