8000 Add "has attribute" collectors · Issue #42037 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Add "has attribute" collectors #42037
Closed as not planned
Closed as not planned
@xepozz

Description

@xepozz

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0