From 6e1cdd1263d693d2bdde615faee76a7e75ebba4e Mon Sep 17 00:00:00 2001 From: HypeMC Date: Mon, 4 Sep 2023 22:36:44 +0200 Subject: [PATCH 1/2] [DependencyInjection] #[Autowire] attribute should have precedence over bindings --- Compiler/ResolveBindingsPass.php | 8 +++ Tests/Compiler/IntegrationTest.php | 62 +++++++++++++++++++ .../LocatorConsumerWithServiceSubscriber.php | 51 +++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 Tests/Fixtures/LocatorConsumerWithServiceSubscriber.php diff --git a/Compiler/ResolveBindingsPass.php b/Compiler/ResolveBindingsPass.php index 55a358efd..614b15257 100644 --- a/Compiler/ResolveBindingsPass.php +++ b/Compiler/ResolveBindingsPass.php @@ -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; @@ -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) ?? '', '?'); diff --git a/Tests/Compiler/IntegrationTest.php b/Tests/Compiler/IntegrationTest.php index 3bf66f031..51d780d25 100644 --- a/Tests/Compiler/IntegrationTest.php +++ b/Tests/Compiler/IntegrationTest.php @@ -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; @@ -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; @@ -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 diff --git a/Tests/Fixtures/LocatorConsumerWithServiceSubscriber.php b/Tests/Fixtures/LocatorConsumerWithServiceSubscriber.php new file mode 100644 index 000000000..ed7b4f3cc --- /dev/null +++ b/Tests/Fixtures/LocatorConsumerWithServiceSubscriber.php @@ -0,0 +1,51 @@ + + * + * 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', + ]; + } +} From 45474d527212ca67cdb93f6c5e6da68f4bc67118 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Thu, 11 Jan 2024 09:51:06 +0100 Subject: [PATCH 2/2] support lazy evaluated exception messages with Xdebug 3 --- Exception/AutowiringFailedException.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Exception/AutowiringFailedException.php b/Exception/AutowiringFailedException.php index 688770195..f9c4ffa34 100644 --- a/Exception/AutowiringFailedException.php +++ b/Exception/AutowiringFailedException.php @@ -23,9 +23,7 @@ public function __construct(string $serviceId, $message = '', int $code = 0, ?\T { $this->serviceId = $serviceId; - if ($message instanceof \Closure - && (\function_exists('xdebug_is_enabled') ? xdebug_is_enabled() : \function_exists('xdebug_info')) - ) { + if ($message instanceof \Closure && \function_exists('xdebug_is_enabled') && xdebug_is_enabled()) { $message = $message(); }