-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[DependencyInjection] Introduce #[AsAlias]
attribute
#49198
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
Conversation
Hey! I see that this is your first PR. That is great! Welcome! Symfony has a contribution guide which I suggest you to read. In short:
Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change. When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor! I am going to sit back now and wait for the reviews. Cheers! Carsonbot |
Hey! Thanks for your PR. You are targeting branch "6.3" but it seems your PR description refers to branch "6.3 for features / 5.4, 6.0, 6.1, or 6.2 for bug fixes". Cheers! Carsonbot |
903d6f4
to
42aa315
Compare
Sometimes we want to alias something in the container with another service. This is usually done in less visible config files like `services.php` or a `Bundle` file. To make these aliases configurations more visible, the `#[AsAlias]` attribute is introduced. The attribute can be used on class that you want your e.g. Interface to alias to. This allows doing: ```php #[AsAlias(alias: 'bar')] class HelloWorldService { } ``` instead of ``` $services->setAlias(‘bar’, HelloWorldService::class); ```
@nicolas-grekas Thanks for the quick reaction 🙂 The main differences that I noticed are the following;
There was a mention in #41207 that it can become harder to find aliases with this attribute. Since it is still possible to still configure the alias in a service file and also use the attribute it will be a matter of preference. For example the main project that I am working in has loads of these configuration files (one per bundle), so the configuration is not so centralised anymore. This attribute can help with clarification of the application. |
Regarding the central location of the aliases mentioned in #41207 (review). The same can be said for If you are using FQCN references, like this: #[AsAlias(UserRepository::class)
final class DoctrineUserRepository implements UserRepository
{
} It is easy to find what aliased the UserRepository service by clicking it's usage in your IDE. |
#[AsAlias]
attribute#[AsAlias]
attribute
I just spent some time on this and I'm still 👎 with this approach. I tried to explain why and propose another approach in #49361. |
Sometimes we want to alias something in the container with another service. This is usually done in less visible config files like
services.php
or aBundle
file. To make these aliases configurations more visible, the#[AsAlias]
attribute is introduced. The attribute can be used on class that you want your e.g. Interface to alias to.This allows doing:
instead of