Jeroen-G / autowire

🔌 Autowire and configure using PHP 8 Attributes in Laravel.
MIT License
21 stars 3 forks source link

Feature: attribute to automatically tag implementations of interface #11

Closed bitwise-operators closed 1 year ago

bitwise-operators commented 1 year ago

Hi there,

This is a 'simple' addition of a new Attribute (and interface) that allows for instant tagging of all implementations of the interface the attribute is added to.

In the included implementation, the fully namespaced interface name is used as the tag name by default, but can be overridden by a string argument to the attribute.

The interface for the attribute specifies passing the ReflectionClass of the interface to the getTag() method, so that implementations can generate their tags in whatever way they desire.

I'm also working on an expansion of the Configure functionality to use tags, but once I'm happy with that, I'll submit it as a separate PR.

Jeroen-G commented 1 year ago

You're doing an awesome job! Its quite a few changes, I'll have a better look when I'm back from vacation.

Do I understand it correctly and is this feature the same as the tagged iterator from Symfony?

bitwise-operators commented 1 year ago

Most of the changes are in the tests, as usual :-)

If by tagged iterator, you're referring to tagged services, then yes, it's pretty much the same (as far as I can see, not very familiar with Symfony)

Usually, you have to add something like this to the Laravel service container:

$this->app->tag(
    [
        Implementation1::class,
        Implementation2::class,
        // repeat
        ImplementationX::class,
    ], 
    'myTag'
);

Which, especially with classes with lots of implementations, can be tedious and error-prone.

This add-on should make it easier.

Enjoy your vacation!

Jeroen-G commented 1 year ago

Ah so it will group the implementations in an array and that one array is then injected?

bitwise-operators commented 1 year ago

Exactly.

bitwise-operators commented 1 year ago

@Jeroen-G I was wondering if you'd had any time to look at this yet?