Skip to content

[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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from

Conversation

santysisi
Copy link
Contributor

@santysisi santysisi commented May 30, 2025

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

✅ 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 in services.yaml:

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

You can now use the attribute directly:

use Symfony\Component\DependencyInjection\Attribute\AsFactory;

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

@santysisi santysisi force-pushed the feature/as-factory-attribute branch from e09532c to 52cdb9a Compare May 30, 2025 03:25
@zolex
Copy link

zolex commented May 30, 2025

I like the feature and I know the As prefix for these attributes is common in symfony, but I wonder a bit about the semantics. Usually if I see a AsSomething attribute on a class, it means that the class with the attribute now is a Something. When null 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, the As 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 just Factory.

But thinking about it a bit more: I believe it would be a better option to reuse the existing Autoconfigure attribute, which has a constrcutor 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.

@santysisi
Copy link
Contributor Author

Hi @zolex, thanks for your comment ❤️ I really appreciate it 😄
You're absolutely right, the semantics of the As prefix can be misleading, especially when an external factory is used. Renaming the attribute to just Factory makes a lot more sense.
I also really like your suggestion about reusing the existing Autoconfigure 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! 🙏

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.

4 participants