Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[DependencyInjection] Add argument$target toContainerBuilder::registerAliasForArgument()#60910

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
nicolas-grekas merged 1 commit intosymfony:7.4fromnicolas-grekas:di-target
Jun 27, 2025
Merged
Show file tree
Hide file tree
Changes fromall 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
5 changes: 5 additions & 0 deletionsUPGRADE-7.4.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
---------------

Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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 .= ' - <fg=magenta>target:</><fg=cyan>'.$target.'</>';
break;
}
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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']) {
Expand DownExpand Up@@ -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);
}
}

Expand DownExpand Up@@ -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);
}
}
}
Expand DownExpand Up@@ -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);
}
}
}
Expand DownExpand Up@@ -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.');
Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I think it's better to use a short RateLimiterFactoryInterface, that's the only thing that one needs to change when upgrading.

$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.');
}
}

Expand All@@ -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);
}
}

Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -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.');
}
}
}
1 change: 1 addition & 0 deletionssrc/Symfony/Component/DependencyInjection/CHANGELOG.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,6 +5,7 @@ CHANGELOG
---

* Allow `#[AsAlias]` to be extended
* Add argument `$target` to `ContainerBuilder::registerAliasForArgument()`

7.3
---
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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());
}
}
}
Expand All@@ -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());
}
Expand DownExpand Up@@ -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;
}
}

Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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;
}
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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) {
Expand All@@ -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);
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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)
Expand DownExpand Up@@ -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();
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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()
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp