From 8b79ff5d8a3eb7274418132cf180468e76bf47a9 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 26 Jun 2025 12:09:09 +0200 Subject: [PATCH] [DependencyInjection] Add argument `$target` to `ContainerBuilder::registerAliasForArgument()` --- UPGRADE-7.4.md | 5 ++ .../Command/DebugAutowiringCommand.php | 2 +- .../FrameworkExtension.php | 24 +++----- .../Factory/LoginThrottlingFactory.php | 12 ++-- .../DependencyInjection/CHANGELOG.md | 1 + .../Compiler/AutowirePass.php | 57 +++++++------------ .../Compiler/ResolveBindingsPass.php | 3 +- .../DependencyInjection/ContainerBuilder.php | 9 ++- .../Tests/Compiler/AutowirePassTest.php | 28 +++++++++ .../Tests/ContainerBuilderTest.php | 4 ++ 10 files changed, 81 insertions(+), 64 deletions(-) diff --git a/UPGRADE-7.4.md b/UPGRADE-7.4.md index d3f628bc16b54..e2d6a4b4ebab5 100644 --- a/UPGRADE-7.4.md +++ b/UPGRADE-7.4.md @@ -13,6 +13,11 @@ Console * Deprecate `Symfony\Component\Console\Application::add()` in favor of `Symfony\Component\Console\Application::addCommand()` +DependencyInjection +------------------- + + * Add argument `$target` to `ContainerBuilder::registerAliasForArgument()` + FrameworkBundle --------------- diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/DebugAutowiringCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/DebugAutowiringCommand.php index e159c5a39593d..ddebb35c6f5ce 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/DebugAutowiringCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/DebugAutowiringCommand.php @@ -137,7 +137,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int } $target = substr($id, \strlen($previousId) + 3); - if ($previousId.' $'.(new Target($target))->getParsedName() === $serviceId) { + if ($container->findDefinition($id) === $container->findDefinition($serviceId)) { $serviceLine .= ' - target:'.$target.''; break; } diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 8dca1fa4b70cf..a427a0061776a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -1190,8 +1190,7 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $ // Store to container $container->setDefinition($workflowId, $workflowDefinition); $container->setDefinition($definitionDefinitionId, $definitionDefinition); - $container->registerAliasForArgument($workflowId, WorkflowInterface::class, $name.'.'.$type); - $container->registerAliasForArgument($workflowId, WorkflowInterface::class, $name); + $container->registerAliasForArgument($workflowId, WorkflowInterface::class, $name.'.'.$type, $name); // Add workflow to Registry if ($workflow['supports']) { @@ -1426,8 +1425,7 @@ private function registerAssetsConfiguration(array $config, ContainerBuilder $co $packageDefinition = $this->createPackageDefinition($package['base_path'], $package['base_urls'], $version) ->addTag('assets.package', ['package' => $name]); $container->setDefinition('assets._package_'.$name, $packageDefinition); - $container->registerAliasForArgument('assets._package_'.$name, PackageInterface::class, $name.'.package'); - $container->registerAliasForArgument('assets._package_'.$name, PackageInterface::class, $name); + $container->registerAliasForArgument('assets._package_'.$name, PackageInterface::class, $name.'.package', $name); } } @@ -2248,8 +2246,7 @@ private function registerLockConfiguration(array $config, ContainerBuilder $cont $container->setAlias('lock.factory', new Alias('lock.'.$resourceName.'.factory', false)); $container->setAlias(LockFactory::class, new Alias('lock.factory', false)); } else { - $container->registerAliasForArgument('lock.'.$resourceName.'.factory', LockFactory::class, $resourceName.'.lock.factory'); - $container->registerAliasForArgument('lock.'.$resourceName.'.factory', LockFactory::class, $resourceName); + $container->registerAliasForArgument('lock.'.$resourceName.'.factory', LockFactory::class, $resourceName.'.lock.factory', $resourceName); } } } @@ -2284,8 +2281,7 @@ private function registerSemaphoreConfiguration(array $config, ContainerBuilder $container->setAlias('semaphore.factory', new Alias('semaphore.'.$resourceName.'.factory', false)); $container->setAlias(SemaphoreFactory::class, new Alias('semaphore.factory', false)); } else { - $container->registerAliasForArgument('semaphore.'.$resourceName.'.factory', SemaphoreFactory::class, $resourceName.'.semaphore.factory'); - $container->registerAliasForArgument('semaphore.'.$resourceName.'.factory', SemaphoreFactory::class, $resourceName); + $container->registerAliasForArgument('semaphore.'.$resourceName.'.factory', SemaphoreFactory::class, $resourceName.'.semaphore.factory', $resourceName); } } } @@ -3310,13 +3306,11 @@ private function registerRateLimiterConfiguration(array $config, ContainerBuilde $factoryAlias = $container->registerAliasForArgument($limiterId, RateLimiterFactory::class, $name.'.limiter'); if (interface_exists(RateLimiterFactoryInterface::class)) { - $container->registerAliasForArgument($limiterId, RateLimiterFactoryInterface::class, $name.'.limiter'); - $factoryAlias->setDeprecated('symfony/framework-bundle', '7.3', \sprintf('The "%%alias_id%%" autowiring alias is deprecated and will be removed in 8.0, use "%s $%s" instead.', RateLimiterFactoryInterface::class, (new Target($name.'.limiter'))->getParsedName())); - $internalAliasId = \sprintf('.%s $%s.limiter', RateLimiterFactory::class, $name); + $container->registerAliasForArgument($limiterId, RateLimiterFactoryInterface::class, $name.'.limiter', $name); - if ($container->hasAlias($internalAliasId)) { - $container->getAlias($internalAliasId)->setDeprecated('symfony/framework-bundle', '7.3', \sprintf('The "%%alias_id%%" autowiring alias is deprecated and will be removed in 8.0, use "%s $%s" instead.', RateLimiterFactoryInterface::class, (new Target($name.'.limiter'))->getParsedName())); - } + $factoryAlias->setDeprecated('symfony/framework-bundle', '7.3', 'The "%alias_id%" autowiring alias is deprecated and will be removed in 8.0, use "RateLimiterFactoryInterface" instead.'); + $container->getAlias(\sprintf('.%s $%s.limiter', RateLimiterFactory::class, $name)) + ->setDeprecated('symfony/framework-bundle', '7.3', 'The "%alias_id%" autowiring alias is deprecated and will be removed in 8.0, use "RateLimiterFactoryInterface" instead.'); } } @@ -3341,7 +3335,7 @@ private function registerRateLimiterConfiguration(array $config, ContainerBuilde ))) ; - $container->registerAliasForArgument($limiterId, RateLimiterFactoryInterface::class, $name.'.limiter'); + $container->registerAliasForArgument($limiterId, RateLimiterFactoryInterface::class, $name.'.limiter', $name); } } diff --git a/src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/Factory/LoginThrottlingFactory.php b/src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/Factory/LoginThrottlingFactory.php index 946abdfddfb85..fa1a9901a67ea 100644 --- a/src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/Factory/LoginThrottlingFactory.php +++ b/src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/Factory/LoginThrottlingFactory.php @@ -120,15 +120,11 @@ private function registerRateLimiter(ContainerBuilder $container, string $name, $factoryAlias = $container->registerAliasForArgument($limiterId, RateLimiterFactory::class, $name.'.limiter'); if (interface_exists(RateLimiterFactoryInterface::class)) { - $container->registerAliasForArgument($limiterId, RateLimiterFactoryInterface::class, $name.'.limiter'); - $container->registerAliasForArgument($limiterId, RateLimiterFactoryInterface::class, $name); - $factoryAlias->setDeprecated('symfony/security-bundle', '7.4', \sprintf('The "%%alias_id%%" autowiring alias is deprecated and will be removed in 8.0, use "%s $%s" instead.', RateLimiterFactoryInterface::class, (new Target($name.'.limiter'))->getParsedName())); + $container->registerAliasForArgument($limiterId, RateLimiterFactoryInterface::class, $name.'.limiter', $name); - $internalAliasId = \sprintf('.%s $%s.limiter', RateLimiterFactory::class, $name); - - if ($container->hasAlias($internalAliasId)) { - $container->getAlias($internalAliasId)->setDeprecated('symfony/security-bundle', '7.4', \sprintf('The "%%alias_id%%" autowiring alias is deprecated and will be removed in 8.0, use "%s $%s" instead.', RateLimiterFactoryInterface::class, (new Target($name.'.limiter'))->getParsedName())); - } + $factoryAlias->setDeprecated('symfony/security-bundle', '7.4', 'The "%alias_id%" autowiring alias is deprecated and will be removed in 8.0, use "RateLimiterFactoryInterface" instead.'); + $container->getAlias(\sprintf('.%s $%s.limiter', RateLimiterFactory::class, $name)) + ->setDeprecated('symfony/security-bundle', '7.4', 'The "%alias_id%" autowiring alias is deprecated and will be removed in 8.0, use "RateLimiterFactoryInterface" instead.'); } } } diff --git a/src/Symfony/Component/DependencyInjection/CHANGELOG.md b/src/Symfony/Component/DependencyInjection/CHANGELOG.md index 5c6c41cfdf27b..9451c0f76fd1e 100644 --- a/src/Symfony/Component/DependencyInjection/CHANGELOG.md +++ b/src/Symfony/Component/DependencyInjection/CHANGELOG.md @@ -5,6 +5,7 @@ CHANGELOG --- * Allow `#[AsAlias]` to be extended + * Add argument `$target` to `ContainerBuilder::registerAliasForArgument()` 7.3 --- diff --git a/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php b/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php index e394cf1057f8d..19daa1e64145b 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php @@ -459,30 +459,14 @@ private function getAutowiredReference(TypedReference $reference, bool $filterTy $name = $target = (array_filter($reference->getAttributes(), static fn ($a) => $a instanceof Target)[0] ?? null)?->name; if (null !== $name ??= $reference->getName()) { - if ($this->container->has($alias = $type.' $'.$name) && !$this->container->findDefinition($alias)->isAbstract()) { + if (null !== ($alias = $this->getCombinedAlias($type, $name, $target)) && !$this->container->findDefinition($alias)->isAbstract()) { return new TypedReference($alias, $type, $reference->getInvalidBehavior()); } - if (null !== ($alias = $this->getCombinedAlias($type, $name)) && !$this->container->findDefinition($alias)->isAbstract()) { - return new TypedReference($alias, $type, $reference->getInvalidBehavior()); - } - - $parsedName = (new Target($name))->getParsedName(); - - if ($this->container->has($alias = $type.' $'.$parsedName) && !$this->container->findDefinition($alias)->isAbstract()) { - return new TypedReference($alias, $type, $reference->getInvalidBehavior()); - } - - if (null !== ($alias = $this->getCombinedAlias($type, $parsedName)) && !$this->container->findDefinition($alias)->isAbstract()) { - return new TypedReference($alias, $type, $reference->getInvalidBehavior()); - } - - if (($this->container->has($n = $name) && !$this->container->findDefinition($n)->isAbstract()) - || ($this->container->has($n = $parsedName) && !$this->container->findDefinition($n)->isAbstract()) - ) { + if ($this->container->has($name) && !$this->container->findDefinition($name)->isAbstract()) { foreach ($this->container->getAliases() as $id => $alias) { - if ($n === (string) $alias && str_starts_with($id, $type.' $')) { - return new TypedReference($n, $type, $reference->getInvalidBehavior()); + if ($name === (string) $alias && str_starts_with($id, $type.' $')) { + return new TypedReference($name, $type, $reference->getInvalidBehavior()); } } } @@ -492,10 +476,6 @@ private function getAutowiredReference(TypedReference $reference, bool $filterTy } } - if ($this->container->has($type) && !$this->container->findDefinition($type)->isAbstract()) { - return new TypedReference($type, $type, $reference->getInvalidBehavior()); - } - if (null !== ($alias = $this->getCombinedAlias($type)) && !$this->container->findDefinition($alias)->isAbstract()) { return new TypedReference($alias, $type, $reference->getInvalidBehavior()); } @@ -710,38 +690,43 @@ private function populateAutowiringAlias(string $id, ?string $target = null): vo $name = $m[3] ?? ''; if (class_exists($type, false) || interface_exists($type, false)) { - if (null !== $target && str_starts_with($target, '.'.$type.' $') - && (new Target($target = substr($target, \strlen($type) + 3)))->getParsedName() === $name - ) { - $name = $target; + if (null !== $target && str_starts_with($target, '.'.$type.' $')) { + $name = substr($target, \strlen($type) + 3); } $this->autowiringAliases[$type][$name] = $name; } } - private function getCombinedAlias(string $type, ?string $name = null): ?string + private function getCombinedAlias(string $type, ?string $name = null, ?string $target = null): ?string { + $prefix = $target && $name ? '.' : ''; + $suffix = $name ? ' $'.($target ?? $name) : ''; + $parsedName = $target ?? ($name ? (new Target($name))->getParsedName() : null); + + if ($this->container->has($alias = $prefix.$type.$suffix) && !$this->container->findDefinition($alias)->isAbstract()) { + return $alias; + } + if (str_contains($type, '&')) { $types = explode('&', $type); } elseif (str_contains($type, '|')) { $types = explode('|', $type); } else { - return null; + return $prefix || $name !== $parsedName && ($name = $parsedName) ? $this->getCombinedAlias($type, $name) : null; } $alias = null; - $suffix = $name ? ' $'.$name : ''; foreach ($types as $type) { - if (!$this->container->hasAlias($type.$suffix)) { - return null; + if (!$this->container->hasAlias($prefix.$type.$suffix)) { + return $prefix || $name !== $parsedName && ($name = $parsedName) ? $this->getCombinedAlias($type, $name) : null; } if (null === $alias) { - $alias = (string) $this->container->getAlias($type.$suffix); - } elseif ((string) $this->container->getAlias($type.$suffix) !== $alias) { - return null; + $alias = (string) $this->container->getAlias($prefix.$type.$suffix); + } elseif ((string) $this->container->getAlias($prefix.$type.$suffix) !== $alias) { + return $prefix || $name !== $parsedName && ($name = $parsedName) ? $this->getCombinedAlias($type, $name) : null; } } diff --git a/src/Symfony/Component/DependencyInjection/Compiler/ResolveBindingsPass.php b/src/Symfony/Component/DependencyInjection/Compiler/ResolveBindingsPass.php index b2c6f6ef78c76..30ecda0debb9b 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/ResolveBindingsPass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/ResolveBindingsPass.php @@ -189,7 +189,8 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed if ( $value->isAutowired() && !$value->hasTag('container.ignore_attributes') - && $parameter->getAttributes(Autowire::class, \ReflectionAttribute::IS_INSTANCEOF) + && ($parameter->getAttributes(Autowire::class, \ReflectionAttribute::IS_INSTANCEOF) + || $parameter->getAttributes(Target::class, \ReflectionAttribute::IS_INSTANCEOF)) ) { continue; } diff --git a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php index 38208124d3baf..e2e90b5380289 100644 --- a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php +++ b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php @@ -1459,10 +1459,13 @@ public function registerAttributeForAutoconfiguration(string $attributeClass, ca * using camel case: "foo.bar" or "foo_bar" creates an alias bound to * "$fooBar"-named arguments with $type as type-hint. Such arguments will * receive the service $id when autowiring is used. + * + * @param ?string $target */ - public function registerAliasForArgument(string $id, string $type, ?string $name = null): Alias + public function registerAliasForArgument(string $id, string $type, ?string $name = null/*, ?string $target = null */): Alias { $parsedName = (new Target($name ??= $id))->getParsedName(); + $target = (\func_num_args() > 3 ? func_get_arg(3) : null) ?? $name; if (!preg_match('/^[a-zA-Z_\x7f-\xff]/', $parsedName)) { if ($id !== $name) { @@ -1472,8 +1475,8 @@ public function registerAliasForArgument(string $id, string $type, ?string $name throw new InvalidArgumentException(\sprintf('Invalid argument name "%s"'.$id.': the first character must be a letter.', $name)); } - if ($parsedName !== $name) { - $this->setAlias('.'.$type.' $'.$name, $type.' $'.$parsedName); + if ($parsedName !== $target) { + $this->setAlias('.'.$type.' $'.$target, $type.' $'.$parsedName); } return $this->setAlias($type.' $'.$parsedName, $id); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php index 114d514adddec..a73bdb01c8161 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php @@ -1121,6 +1121,20 @@ public function testArgumentWithTarget() { $container = new ContainerBuilder(); + $container->register(BarInterface::class, BarInterface::class); + $container->register('.'.BarInterface::class.' $image.storage', BarInterface::class); + $container->register('with_target', WithTarget::class) + ->setAutowired(true); + + (new AutowirePass())->process($container); + + $this->assertSame('.'.BarInterface::class.' $image.storage', (string) $container->getDefinition('with_target')->getArgument(0)); + } + + public function testArgumentWithParsedTarget() + { + $container = new ContainerBuilder(); + $container->register(BarInterface::class, BarInterface::class); $container->register(BarInterface::class.' $imageStorage', BarInterface::class); $container->register('with_target', WithTarget::class) @@ -1161,6 +1175,20 @@ public function testArgumentWithTypoTargetAnonymous() (new AutowirePass())->process($container); } + public function testArgumentWithIdTarget() + { + $container = new ContainerBuilder(); + + $container->register('image.storage', BarInterface::class); + $container->registerAliasForArgument('image.storage', BarInterface::class, 'image'); + $container->register('with_target', WithTarget::class) + ->setAutowired(true); + + (new AutowirePass())->process($container); + + $this->assertSame('image.storage', (string) $container->getDefinition('with_target')->getArgument(0)); + } + public function testDecorationWithServiceAndAliasedInterface() { $container = new ContainerBuilder(); diff --git a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php index 5e08e47ab908c..93fa8a3291699 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php @@ -1779,6 +1779,10 @@ public function testRegisterAliasForArgument() $container->registerAliasForArgument('Foo.bar_baz', 'Some\FooInterface', 'Bar_baz.foo'); $this->assertEquals(new Alias('Foo.bar_baz'), $container->getAlias('Some\FooInterface $barBazFoo')); $this->assertEquals(new Alias('Some\FooInterface $barBazFoo'), $container->getAlias('.Some\FooInterface $Bar_baz.foo')); + + $container->registerAliasForArgument('Foo.bar_baz', 'Some\FooInterface', 'Bar_baz.foo', 'foo'); + $this->assertEquals(new Alias('Foo.bar_baz'), $container->getAlias('Some\FooInterface $barBazFoo')); + $this->assertEquals(new Alias('Some\FooInterface $barBazFoo'), $container->getAlias('.Some\FooInterface $foo')); } public function testCaseSensitivity() pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy