Skip to content

Add "has attribute" collectors #42037

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
xepozz opened this issue Jul 8, 2021 · 11 comments
Closed

Add "has attribute" collectors #42037

xepozz opened this issue Jul 8, 2021 · 11 comments

Comments

@xepozz
Copy link
Contributor

xepozz commented Jul 8, 2021

Hi everyone.
I need to collect all classes that was registered with some attribute.

For example, I want to have a collection with some classes that was tagged with attribute MyTag. I want to process all these classes with some logic.

I can explain my request in abstract code

#[MyTag]
class TaggedClass1 {}

#[MyTag]
class TaggedClass2 {}

Then I want to collect them and pass to another class:

services:
  _has_attribute:
    MyTag:
      tag: [ 'tagged_with_attribute' ]

  SomeService:
    arguments: [ !tagged_iterator 'tagged_with_attribute' ]

And then I want to process all these classes:

class SomeService 
{
    public function __construct(iterable $classes)
    {
        $processor = new SomeProcessor();

        foreach($classes as $class) {
            $processor->process($class);
        }
    }
}

It will allow to process classes by third-party libraries, whom don't need to have specific methods/properties or another behaviour to work with classes.

If you have similar functional please say me where I can read about it.
If you don't mind I can try to do PR, but most likely I will need help with it.

Thanks!


QA:

  1. You can use interface instead of this feature.
    Yes, I can. But interface won't have any methods, it will exist like just a marker. It's OK for userland code, but not for external libraries code.
  2. You can make your own CompilerPass and share it.
    Yes, but it would be great to supports it by default in framework. Also I think community can find more useful use-cases.
@stof
Copy link
Member

stof commented Jul 9, 2021

Your proposal with something in the config file would have to apply only to services registered in that file, for consistency with other features. This kinda defeats the purpose.
The autoconfiguration feature is the one that can manage cases for the whole project. And as of Symfony 5.3, the autoconfiguration can be based on attributes too.

1. Yes, I can. But interface won't have any methods, it will exist like just a marker. It's OK for userland code, but not for external libraries code.

is a third-party library more likely to accept adding your attribute than your interface ?

@mansijain980
Copy link

Can i work on this?

@carsonbot
Copy link

Thank you for this suggestion.
There has not been a lot of activity here for a while. Would you still like to see this feature?

@carsonbot
Copy link

Friendly reminder that this issue exists. If I don't hear anything I'll close this.

@xepozz
Copy link
Contributor Author

xepozz commented Mar 16, 2022

Leave it

@carsonbot carsonbot removed the Stalled label Mar 16, 2022
@carsonbot
Copy link

Thank you for this suggestion.
There has not been a lot of activity here for a while. Would you still like to see this feature?

@xepozz
Copy link
Contributor Author

xepozz commented Oct 29, 2022

For sure

@carsonbot carsonbot removed the Stalled label Oct 29, 2022
@carsonbot
Copy link

Thank you for this suggestion.
There has not been a lot of activity here for a while. Would you still like to see this feature?

@xepozz
Copy link
Contributor Author

xepozz commented May 3, 2023

Yes sure

@carsonbot carsonbot removed the Stalled label May 3, 2023
@carsonbot
Copy link

Thank you for this suggestion.
There has not been a lot of activity here for a while. Would you still like to see this feature?

@derrabus
Copy link
Member

derrabus commented Nov 5, 2023

This use-case should be covered by #39897 already.

If anyone really wants attribute autoconfiguration scoped to a specific configuration file only, feel free to open a PR that adds that functionality. But I highly doubt that we need that.

@derrabus derrabus closed this as not planned Won't fix, can't repro, duplicate, stale Nov 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants