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! 🙏

@santysisi
Copy link
Contributor Author

I was thinking a bit about this, and I have a question 🤔
If we change the constructor argument to accept string|array|null, I believe we would end up with the same behavior as the factory.
So, my question is: maybe we can leave the constructor argument as it is today, and instead, add the factory argument to the AutoConfigure?

I’m asking this from a place of curiosity and ignorance, totally open to discussion if I’m missing something 🙂
Thanks! ❤️

@zolex
Copy link

zolex commented Jun 7, 2025

I was thinking a bit about this, and I have a question 🤔 If we change the constructor argument to accept string|array|null, I believe we would end up with the same behavior as the factory. So, my question is: maybe we can leave the constructor argument as it is today, and instead, add the factory argument to the AutoConfigure?

I would definitely prefer the argument to be called factory. If you introduce a factory argument, maybe also support a string for a method in the same class like constructor does and at the same time deprecate the constructor argument to prepare the people to migrate to the factory argument. It was a bad naming anyway.

@santysisi
Copy link
Contributor Author

I like that idea 😄 If others agree, I can go ahead and make that change:

  • Add a new argument called factory
  • Deprecate the constructor argument to help people migrate smoothly

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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy