Skip to content

[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

Closed
wants to merge 1 commit into from

Conversation

samybasset
Copy link

@samybasset samybasset commented Feb 2, 2023

Q A
Branch? 6.3
Bug fix? no
New feature? yes
Deprecations? no
Tickets
License MIT
Doc PR symfony/symfony-docs#...

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:

#[AsAlias(alias: 'bar')]
class HelloWorldService
{
}

instead of

$services->setAlias('bar', HelloWorldService::class);

@carsonbot
Copy link

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:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 6.3 branch.

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!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@carsonbot
Copy link

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".
Could you update the PR description or change target branch? This helps core maintainers a lot.

Cheers!

Carsonbot

@samybasset samybasset force-pushed the as-alias branch 4 times, most recently from 903d6f4 to 42aa315 Compare February 2, 2023 12:14
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
Copy link
Member

nicolas-grekas commented Feb 2, 2023

Hi, thanks for the proposal.
There's been some discussion already on the topic, see #48523, and the closed ones: #47323, #41207, #41188
I didn't check how your approach is different from those. It would be interesting to see why some approaches have been refused to see how yours compares.

@samybasset
Copy link
Author

Hi, thanks for the proposal. There's been some discussion already on the topic, see #48523, and the closed ones: #47323, #41207, #41188 I didn't check how your approach is different from those. It would be interesting to see why some approaches have been refused to see how yours compares.

@nicolas-grekas Thanks for the quick reaction 🙂 The main differences that I noticed are the following;

  • The simplicity of the implementation, a service that aliases something implements the attribute, it gets tagged and we alias it, thats it.
  • In Define alias with an attribute #41188 the to be aliased id is fetched automagically from the class, which causes conflicts when multiple interfaces are implemented. While in this implementation the alias should be provided explicitly.

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.

@ruudk
Copy link
Contributor

ruudk commented Feb 3, 2023

Regarding the central location of the aliases mentioned in #41207 (review). The same can be said for #[AsDecorator]. It's also not clear centrally where something was decorated. You have to find the place where it happens.

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.

@carsonbot carsonbot changed the title Introduce #[AsAlias] attribute [DependencyInjection] Introduce #[AsAlias] attribute Feb 11, 2023
@nicolas-grekas
Copy link
Member

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.
Please have a look.

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.

5 participants