Skip to content

[DependencyInjection] #[Autowire] attribute should have precedence over bindings #51559

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
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
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\Attribute\Target;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
Expand Down Expand Up @@ -185,6 +186,13 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
if (\array_key_exists($parameter->name, $arguments) && '' !== $arguments[$parameter->name]) {
continue;
}
if (
$value->isAutowired()
&& !$value->hasTag('container.ignore_attributes')
&& $parameter->getAttributes(Autowire::class, \ReflectionAttribute::IS_INSTANCEOF)
) {
continue;
}

$typeHint = ltrim(ProxyHelper::exportType($parameter) ?? '', '?');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Psr\Container\ContainerInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
use Symfony\Component\DependencyInjection\ChildDefinition;
Expand Down Expand Up @@ -47,6 +48,7 @@
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumerWithDefaultIndexMethodAndWithDefaultPriorityMethod;
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumerWithDefaultPriorityMethod;
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumerWithoutIndex;
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumerWithServiceSubscriber;
use Symfony\Component\DependencyInjection\Tests\Fixtures\StaticMethodTag;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedConsumerWithExclude;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService1;
Expand Down Expand Up @@ -1085,6 +1087,66 @@ public function testTaggedIteratorAndLocatorWithExclude()
$this->assertTrue($locator->has(AutoconfiguredService2::class));
$this->assertFalse($locator->has(TaggedConsumerWithExclude::class));
}

public function testAutowireAttributeHasPriorityOverBindings()
{
$container = new ContainerBuilder();
$container->register(FooTagClass::class)
->setPublic(true)
->addTag('foo_bar', ['key' => 'tagged_service'])
;
$container->register(LocatorConsumerWithServiceSubscriber::class)
->setBindings([
'$locator' => new BoundArgument(new Reference('service_container'), false),
])
->setPublic(true)
->setAutowired(true)
->addTag('container.service_subscriber')
;
$container->register('subscribed_service', \stdClass::class)
->setPublic(true)
;

$container->compile();

/** @var LocatorConsumerWithServiceSubscriber $s */
$s = $container->get(LocatorConsumerWithServiceSubscriber::class);

self::assertInstanceOf(ContainerInterface::class, $subscriberLocator = $s->getContainer());
self::assertTrue($subscriberLocator->has('subscribed_service'));
self::assertNotSame($subscriberLocator, $taggedLocator = $s->getLocator());
self::assertInstanceOf(ContainerInterface::class, $taggedLocator);
self::assertTrue($taggedLocator->has('tagged_service'));
}

public function testBindingsWithAutowireAttributeAndAutowireFalse()
{
$container = new ContainerBuilder();
$container->register(FooTagClass::class)
->setPublic(true)
->addTag('foo_bar', ['key' => 'tagged_service'])
;
$container->register(LocatorConsumerWithServiceSubscriber::class)
->setBindings([
'$locator' => new BoundArgument(new Reference('service_container'), false),
])
->setPublic(true)
->setAutowired(false)
->addTag('container.service_subscriber')
;
$container->register('subscribed_service', \stdClass::class)
->setPublic(true)
;

$container->compile();

/** @var LocatorConsumerWithServiceSubscriber $s */
$s = $container->get(LocatorConsumerWithServiceSubscriber::class);

self::assertNull($s->getContainer());
self::assertInstanceOf(ContainerInterface::class, $taggedLocator = $s->getLocator());
self::assertSame($container, $taggedLocator);
}
}

class ServiceSubscriberStub implements ServiceSubscriberInterface
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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\Tests\Fixtures;

use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
use Symfony\Contracts\Service\Attribute\Required;
use Symfony\Contracts\Service\ServiceSubscriberInterface;

final class LocatorConsumerWithServiceSubscriber implements ServiceSubscriberInterface
{
private ?ContainerInterface $container = null;

public function __construct(
#[TaggedLocator('foo_bar', indexAttribute: 'key')]
private ContainerInterface $locator,
) {
}

public function getLocator(): ContainerInterface
{
return $this->locator;
}

public function getContainer(): ?ContainerInterface
{
return $this->container;
}

#[Required]
public function setContainer(ContainerInterface $container): void
{
$this->container = $container;
}

public static function getSubscribedServices(): array
{
return [
'subscribed_service',
];
}
}