Skip to content

[DependencyInjection] Add support for nesting autowiring-related attributes into #[Autowire(...)] #48710

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
Dec 21, 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
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Add options `inline_factories` and `inline_class_loader` to `PhpDumper::dump()`
* Deprecate `PhpDumper` options `inline_factories_parameter` and `inline_class_loader_parameter`
* Add `RemoveBuildParametersPass`, which removes parameters starting with a dot during compilation
* Add support for nesting autowiring-related attributes into `#[Autowire(...)]`

6.2
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,24 @@ public function process(ContainerBuilder $container)

protected function processValue(mixed $value, bool $isRoot = false): mixed
{
if ($value instanceof Autowire) {
return $this->processValue($this->container->getParameterBag()->resolveValue($value->value));
}

if ($value instanceof TaggedIterator) {
return new TaggedIteratorArgument($value->tag, $value->indexAttribute, $value->defaultIndexMethod, false, $value->defaultPriorityMethod, (array) $value->exclude);
}

if ($value instanceof TaggedLocator) {
return new ServiceLocatorArgument(new TaggedIteratorArgument($value->tag, $value->indexAttribute, $value->defaultIndexMethod, true, $value->defaultPriorityMethod, (array) $value->exclude));
}

if ($value instanceof MapDecorated) {
$definition = $this->container->getDefinition($this->currentId);

return new Reference($definition->innerServiceId ?? $this->currentId.'.inner', $definition->decorationOnInvalid ?? ContainerInterface::NULL_ON_INVALID_REFERENCE);
}

try {
return $this->doProcessValue($value, $isRoot);
} catch (AutowiringFailedException $e) {
Expand Down Expand Up @@ -170,20 +188,14 @@ private function processAttribute(object $attribute, bool $isOptional = false):
{
switch (true) {
case $attribute instanceof Autowire:
$value = $this->container->getParameterBag()->resolveValue($attribute->value);

return $value instanceof Reference && $isOptional ? new Reference($value, ContainerInterface::NULL_ON_INVALID_REFERENCE) : $value;

if ($isOptional && $attribute->value instanceof Reference) {
return new Reference($attribute->value, ContainerInterface::NULL_ON_INVALID_REFERENCE);
}
// no break
case $attribute instanceof TaggedIterator:
return new TaggedIteratorArgument($attribute->tag, $attribute->indexAttribute, $attribute->defaultIndexMethod, false, $attribute->defaultPriorityMethod, (array) $attribute->exclude);

case $attribute instanceof TaggedLocator:
return new ServiceLocatorArgument(new TaggedIteratorArgument($attribute->tag, $attribute->indexAttribute, $attribute->defaultIndexMethod, true, $attribute->defaultPriorityMethod, (array) $attribute->exclude));

case $attribute instanceof MapDecorated:
$definition = $this->container->getDefinition($this->currentId);

return new Reference($definition->innerServiceId ?? $this->currentId.'.inner', $definition->decorationOnInvalid ?? ContainerInterface::NULL_ON_INVALID_REFERENCE);
return $this->processValue($attribute);
}

throw new AutowiringFailedException($this->currentId, sprintf('"%s" is an unsupported attribute.', $attribute::class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
use Symfony\Component\DependencyInjection\Compiler\AutowireAsDecoratorPass;
use Symfony\Component\DependencyInjection\Compiler\AutowirePass;
use Symfony\Component\DependencyInjection\Compiler\AutowireRequiredMethodsPass;
Expand Down Expand Up @@ -1254,4 +1256,25 @@ public function testTypeNamespaceExcluded()
$this->assertSame('Cannot autowire service "a": argument "$k" of method "Symfony\Component\DependencyInjection\Tests\Compiler\NotGuessableArgument::__construct()" needs an instance of "Symfony\Component\DependencyInjection\Tests\Compiler\Foo" but this type has been excluded from autowiring.', (string) $e->getMessage());
}
}

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

$container->register(AsDecoratorFoo::class);
$container->register(AutowireNestedAttributes::class)->setAutowired(true);

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

$expected = [
'decorated' => new Reference(AutowireNestedAttributes::class.'.inner'),
'iterator' => new TaggedIteratorArgument('foo'),
'locator' => new ServiceLocatorArgument(new TaggedIteratorArgument('foo', needsIndexes: true)),
'service' => new Reference('bar'),
];
$this->assertEquals($expected, $container->getDefinition(AutowireNestedAttributes::class)->getArgument(0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\Attribute\MapDecorated;
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Contracts\Service\Attribute\Required;

Expand Down Expand Up @@ -85,3 +87,17 @@ public function __construct(#[MapDecorated] AsDecoratorInterface $inner = null)
{
}
}

#[AsDecorator(decorates: AsDecoratorFoo::class)]
class AutowireNestedAttributes implements AsDecoratorInterface
{
public function __construct(
#[Autowire([
'decorated' => new MapDecorated(),
'iterator' => new TaggedIterator('foo'),
'locator' => new TaggedLocator('foo'),
'service' => new Autowire(service: 'bar')
])] array $options)
{
}
}