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#[AsFactory] attribute#60589

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

Open
santysisi wants to merge1 commit intosymfony:7.4
base:7.4
Choose a base branch
Loading
fromsantysisi:feature/as-factory-attribute

Conversation

santysisi
Copy link
Contributor

@santysisisantysisi commentedMay 30, 2025
edited
Loading

QA
Branch?7.4
Bug fix?no
New feature?yes
Deprecations?no
Issuesno
LicenseMIT

✅ Add#[AsFactory] Attribute for Declaring Factory Services

This PR introduces a new attribute:#[AsFactory], which allows developers to declare a service factory directly on a class using attributes, instead of via configuration files.

✅ Usage Example

Instead of configuring a factory inservices.yaml:

App\Service\MyService:factory:['@App\Factory\MyServiceFactory', 'create']arguments:['@some_dependency']

You can now use the attribute directly:

useSymfony\Component\DependencyInjection\Attribute\AsFactory;#[AsFactory(    factory: ['@App\Factory\MyServiceFactory','create'],    arguments: ['@some_dependency'])]class MyService{// ...}

@santysisisantysisiforce-pushed thefeature/as-factory-attribute branch frome09532c to52cdb9aCompareMay 30, 2025 03:25
@zolex
Copy link

zolex commentedMay 30, 2025
edited
Loading

I like the feature and I know theAs prefix for these attributes is common in symfony, but I wonder a bit about the semantics. Usually if I see aAsSomething attribute on a class, it means that the class with the attribute now is a Something. Whennull is passed as the class to the factory argument, the semantics are fine. But if you provide another classname, a service or even expression language, theAs prefix seems semantically incorrect to me, because now it is not the class with the attribute that becomes the factory, but something else. So in the first place I would suggest to rename the Attribute to justFactory.

But thinking about it a bit more: I believe it would be a better option to reuse the existingAutoconfigure attribute, which has aconstrcutor argument, that can be used for factories in the same class. At the moment it only allows a string, referencing a static method inside the same class. You could simply apply your logic on the existing attribute argument if it is an array.

nicolas-grekas, alamirault, and santysisi reacted with thumbs up emojisantysisi reacted with heart emojisantysisi reacted with rocket emoji

@santysisi
Copy link
ContributorAuthor

Hi@zolex, thanks for your comment ❤️ I really appreciate it 😄
You're absolutely right, the semantics of theAs prefix can be misleading, especially when an external factory is used. Renaming the attribute to justFactory makes a lot more sense.
I also really like your suggestion about reusing the existingAutoconfigure attribute. I’ll work on updating it to support arrays and apply the logic there as well.
I’ll make these changes as soon as possible. Thanks again for the valuable feedback! 🙏

OskarStark reacted with rocket emoji

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Projects
None yet
Milestone
7.4
Development

Successfully merging this pull request may close these issues.

4 participants
@santysisi@zolex@nicolas-grekas@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp