Skip to content

Conversation

@mtarld
Copy link
Contributor

@mtarld mtarld commented Dec 3, 2025

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

Allow to decorate tags.
When decorating a tag, every service with that tag will be decorated.

Consider the following example:

services:
    foo: 
        tags: [ tag ]

    bar: 
        tags: [ tag ]

    tag_decorator:
        class: MyDecorator
        decorates_tag: tag

In that case, foo and bar will be decorated by tag_decorator.

This can be useful when you need to decorate several services to trace them, for example.
With this PR, no need to write compiler passes to achieve that.

@Spomky
Copy link
Contributor

Spomky commented Dec 4, 2025

That's look powerful e.g. to decorate cache warmer ✨.

When a tag collide with an existing service id/alias, it will not decorate the tag, but instead will decorate the service.

Let say I have an interface App\Acme\MyInterface that holds the #[AutoconfigureTag] attribute. In this case there is the possibility to have

  • tag = App\Acme\MyInterface
  • alias = App\Acme\MyInterface

I understand this avoids breaks. Am I right?

@stof
Copy link
Member

stof commented Dec 4, 2025

This is very fragile as defining a service with that id in the future will suddenly change what is decorated.

If we want to add such feature, we should have a dedicated configuring setting dedicated to tags, to avoid any ambiguous meaning of getDecoratedService.

Thus, decorating a tag rather than a service means that your original definition is more or less used as an parent definition to create other services (except that's not how it is implemented, so there might be subtle differences) and the original definition is removed from the container (so anything referencing its id is invalid)

@mtarld mtarld force-pushed the feat/as-decorator-tag branch from f3080a0 to 049a8df Compare December 5, 2025 10:04
@mtarld
Copy link
Contributor Author

mtarld commented Dec 5, 2025

To make the usage more clear, I updated the Definition to add a setDecoratedTag method.

The usage will be:

#[AsTagDecorator(decoratesTag: 'my_tag', priority: 10)]
class MyDecorator implements MyInterface {
    public function __construct(
        #[AutowireDecorated] MyInterface $inner
    ) {}
}
services:
    my_decorator:
        class: MyDecorator
        decorates_tag: my_tag
        decoration_priority: 10
$services->set('my_decorator', MyDecorator::class)
    ->decorateTag('my_tag', priority: 10);

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