Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[FrameworkBundle] Allow using their name without added suffix when using #[Target] for custom services #60874

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

Conversation

Valmonzo
Copy link
Contributor

Q A
Branch? 7.4
Bug fix? no
New feature? yes
Deprecations? no
Issues Fix #60349
License MIT

@carsonbot carsonbot added this to the 7.4 milestone Jun 23, 2025
@carsonbot carsonbot changed the title [Lock][Framework] Add private aliases for LockFactory services [Lock] [Framework] Add private aliases for LockFactory services Jun 23, 2025
@Valmonzo Valmonzo changed the title [Lock] [Framework] Add private aliases for LockFactory services [Lock] [Framework] Add private aliases with only name for LockFactory services Jun 23, 2025
@Valmonzo Valmonzo requested a review from stof June 23, 2025 11:42
nicolas-grekas

This comment was marked as duplicate.

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

Workflow works because it has two lines:

$container->registerAliasForArgument($workflowId, WorkflowInterface::class, $name.'.'.$type);
$container->registerAliasForArgument($workflowId, WorkflowInterface::class, $name);

HttpClient/Cache work because they don't add any conventions: scoped clients' / cache pool names are their service names.

And there are other subsystems that create their own conventions:

$container->registerAliasForArgument($serializerId, SerializerInterface::class, $serializerName.'.serializer');
$container->registerAliasForArgument($serializerId, NormalizerInterface::class, $serializerName.'.normalizer');
$container->registerAliasForArgument($serializerId, DenormalizerInterface::class, $serializerName.'.denormalizer');
$container->registerAliasForArgument('assets._package_'.$name, PackageInterface::class, $name.'.package');
$container->registerAliasForArgument('lock.'.$resourceName.'.factory', LockFactory::class, $resourceName.'.lock.factory');
$container->registerAliasForArgument('semaphore.'.$resourceName.'.factory', SemaphoreFactory::class, $resourceName.'.semaphore.factory');
$container->registerAliasForArgument($limiterId, RateLimiterFactoryInterface::class, $name.'.limiter');

Looks like they would all benefit from something similar, isn't it?

To improve the situation, we could add an argument, $target to registerAliasForArgument (using a proper BC layer). The implementation would just create this second alias we're considering to add here.
And we would also patch debug:autowiring so that it groups those two lines in one (see "target" entry in the output).

WDYT?
Is this something you might want to work on @Valmonzo?

@stof
Copy link
Member

stof commented Jun 23, 2025

@nicolas-grekas most cases are adding a suffix mostly because they were implemented at a time where named autowiring was based on argument names, where we would expect the argument name (and so often the property name) to end with normalizer if it is a NormalizerInterface for instance.

When using the Target attribute, using the original name directly is much better (personally, I would even vote for deprecating parameter-name based named autowiring in favor of the attribute which fails clearly for invalid targets thanks to explicit intent)

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Jun 23, 2025

So let's start with adding all those aliases and call registerAliasForArgument()->setDeprecated('symfony/the-bundle', '7.4', \sprintf('Using "%%alias_id%%" to get a named service is deprecated, use #[Target('%s')] on the arguments instead.', $name)) for them all?

@stof
Copy link
Member

stof commented Jun 23, 2025

@nicolas-grekas given the maintenance cost of the double aliases is low, maybe we should make it easier to support multiple versions of Symfony by delaying the deprecation (you cannot use #[Target('foo') when supporting 7.3 or 6.4 as the alias using the unsuffixed target is not there). and adding a conditional attribute depending on the version of FrameworkBundle is not possible.

@chalasr
Copy link
Member

chalasr commented Jun 23, 2025

I like the fact that one can choose not to use the attribute as it’s less boilerplate for aliases that are used a lot.

@Valmonzo
Copy link
Contributor Author

Same preference as @chalasr , but whatever you choose, I'll be happy to work on it !

@nicolas-grekas
Copy link
Member

Fine, then let's also add this line for the other components? See my list above.

@Valmonzo Valmonzo requested a review from dunglas as a code owner June 24, 2025 09:59
@Valmonzo Valmonzo changed the title [Lock] [Framework] Add private aliases with only name for LockFactory services [Framework] Add private aliases with only name for custom services Jun 24, 2025
@Valmonzo Valmonzo force-pushed the feat/add-aliases-for-lock-component branch from 79d2994 to aa54036 Compare June 24, 2025 10:14
@nicolas-grekas nicolas-grekas changed the title [Framework] Add private aliases with only name for custom services [FrameworkBundle] Allow using their name without added suffix when using #[Target] for custom services Jun 24, 2025
@carsonbot carsonbot changed the title [FrameworkBundle] Allow using their name without added suffix when using #[Target] for custom services [FrameworkBundle][Lock] Allow using their name without added suffix when using #[Target] for custom services Jun 24, 2025
@carsonbot carsonbot changed the title [FrameworkBundle][Lock] Allow using their name without added suffix when using #[Target] for custom services [FrameworkBundle] Allow using their name without added suffix when using #[Target] for custom services Jun 24, 2025
@nicolas-grekas nicolas-grekas force-pushed the feat/add-aliases-for-lock-component branch from aa54036 to 8df6691 Compare June 24, 2025 14:17
@nicolas-grekas nicolas-grekas requested a review from chalasr as a code owner June 24, 2025 14:17
@nicolas-grekas
Copy link
Member

Thank you @Valmonzo.

@nicolas-grekas nicolas-grekas merged commit a3c1d1f into symfony:7.4 Jun 24, 2025
5 of 11 checks passed
@nicolas-grekas
Copy link
Member

With this PR, the debug:autowiring command is going to display eg:

 Describes a workflow instance.
 Symfony\Component\Workflow\WorkflowInterface $comment - alias:state_machine.comment
 Symfony\Component\Workflow\WorkflowInterface $commentStateMachine - target:comment.state_machine - alias:state_machine.comment

It'd be nice to have display this instead:

 Describes a workflow instance.
 Symfony\Component\Workflow\WorkflowInterface $commentStateMachine - target:comment - alias:state_machine.comment

If anyone reading is up to giving it a try (the logic should be in DebugAutowiringCommand)

@Valmonzo Valmonzo deleted the feat/add-aliases-for-lock-component branch June 24, 2025 14:20
@OskarStark OskarStark changed the title [FrameworkBundle] Allow using their name without added suffix when using #[Target] for custom services [FrameworkBundle] Allow using their name without added suffix when using #[Target] for custom services Jun 24, 2025
@@ -4,6 +4,7 @@ CHANGELOG
7.4
---

* Allow using their name without added suffix when using `#[Target]` for custom services
Copy link
Contributor

@mttsch mttsch Jun 24, 2025

Choose a reason for hiding this comment

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

Am I alone in having a problem properly understanding this line? (May-be its the "their" being used before its clear what it is referencing?)

Copy link
Contributor Author

@Valmonzo Valmonzo Jun 24, 2025

Choose a reason for hiding this comment

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

Imagine you have some services like a LockFactory named foo, before the target worked like #[Target('foo.lock.factory)] LockFactory $lockFactory, now you can use #[Target('foo')] LockFactory $lockFactory

Copy link
Contributor

Choose a reason for hiding this comment

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

But from the code diff, this only applies to the concrete services, for which you added suffix-less aliases? Is it really tightly coupled to #[Target] in general, or just for specific services and wouldn't these aliases also work in services.yaml, for example?

@Valmonzo
Copy link
Contributor Author

With this PR, the debug:autowiring command is going to display eg:

 Describes a workflow instance.
 Symfony\Component\Workflow\WorkflowInterface $comment - alias:state_machine.comment
 Symfony\Component\Workflow\WorkflowInterface $commentStateMachine - target:comment.state_machine - alias:state_machine.comment

It'd be nice to have display this instead:

 Describes a workflow instance.
 Symfony\Component\Workflow\WorkflowInterface $commentStateMachine - target:comment - alias:state_machine.comment

If anyone reading is up to giving it a try (the logic should be in DebugAutowiringCommand)

It's a silly question, but since we know that Symfony\Component\Workflow\WorkflowInterface $comment also works without the target attribute, wouldn't it be better to leave the two as they are? Or is this kind of naming bad practice?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Lock] Renamed target for autowire
7 participants