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

Skip to content

[DependencyInjection] Add AsAlias attribute #49411

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
merged 1 commit into from
Feb 18, 2023

Conversation

alanpoulain
Copy link
Contributor

@alanpoulain alanpoulain commented Feb 16, 2023

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

Add the AsAlias attribute for defining an alias on a service:

use Symfony\Component\DependencyInjection\Attribute\AsAlias;
use App\MyInterface;

#[AsAlias(id: 'my-alias', public: true)]
#[AsAlias(id: MyInterface::class)]
class MyService
{}

If no parameter is used in the attribute and if there is only one implemented interface, it takes this interface for the alias:

use Symfony\Component\DependencyInjection\Attribute\AsAlias;
use App\MyInterface;

#[AsAlias]
class MyService implements MyInterface
{}

is equivalent to:

$services->alias(MyInterface::class, MyService::class);

This implementation follows @nicolas-grekas suggestion in #49361: doing it in FileLoader::registerClasses() allows to check if there is no collision.

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.

Really nice thank you!

@alanpoulain alanpoulain force-pushed the feat/di-as-alias-attribute branch from 649aac1 to 3fcf97a Compare February 16, 2023 16:20
@alanpoulain
Copy link
Contributor Author

Changes done, thanks for the review 🙂

Copy link
Member

@kbond kbond left a comment

Choose a reason for hiding this comment

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

🎉

@chalasr chalasr force-pushed the feat/di-as-alias-attribute branch from eea6905 to fe63d39 Compare February 18, 2023 13:31
@chalasr chalasr force-pushed the feat/di-as-alias-attribute branch from fe63d39 to 80d8cc1 Compare February 18, 2023 13:33
@chalasr
Copy link
Member

chalasr commented Feb 18, 2023

Thank you @alanpoulain.

]];
yield 'Multiple with id' => ['PrototypeAsAlias/{WithAsAliasIdMultipleInterface,AliasBarInterface,AliasFooInterface}.php', [
AliasBarInterface::class => new Alias(WithAsAliasIdMultipleInterface::class),
AliasFooInterface::class => new Alias(WithAsAliasIdMultipleInterface::class),
Copy link
Member

Choose a reason for hiding this comment

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

Why is the AliasFooInterface being registered? Is it because of the WithAsAliasIdMultipleInterface class being the only one implementing this interface?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I think it's the right behavior, don't you think so? The other possibility would be to prevent the singly implemented interface registration if there is an AsAlias attribute on the class.

@fabpot fabpot mentioned this pull request May 1, 2023
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.

Add #[AsAlias] to allow defining service aliases using attributes
6 participants