Summary
Add a #[Tag('name')] class-level attribute that automatically tags a service when it is registered or auto-wired.
#[Tag('handlers')]
class OrderHandler implements HandlerInterface {}
Context
Tags are currently a per-registration concept via addTag(). A class-level #[Tag] attribute would allow services to self-declare their tags, reducing boilerplate in bootstrap code.
The committee deferred this because it implies class scanning, which is a bigger design decision. Without a scanning mechanism, the attribute would only apply during auto-wiring via ReflectionContainer or explicit registration, similar to how #[Shared] works.
Implementation notes
- Class-level attribute (
Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)
ReflectionContainer would need to read the attribute and communicate the tag to the container (the delegate container pattern makes this non-trivial since ReflectionContainer does not register definitions)
- Compilation support via
DefinitionAnalyser::ensureSynthesisedIfNeeded() where synthesised CompiledDefinition objects could include tags from the attribute
- Consider whether this requires a class-scanning mechanism or works within the existing lazy-resolution model
Target
6.1 minor release
Summary
Add a
#[Tag('name')]class-level attribute that automatically tags a service when it is registered or auto-wired.Context
Tags are currently a per-registration concept via
addTag(). A class-level#[Tag]attribute would allow services to self-declare their tags, reducing boilerplate in bootstrap code.The committee deferred this because it implies class scanning, which is a bigger design decision. Without a scanning mechanism, the attribute would only apply during auto-wiring via
ReflectionContaineror explicit registration, similar to how#[Shared]works.Implementation notes
Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)ReflectionContainerwould need to read the attribute and communicate the tag to the container (the delegate container pattern makes this non-trivial sinceReflectionContainerdoes not register definitions)DefinitionAnalyser::ensureSynthesisedIfNeeded()where synthesisedCompiledDefinitionobjects could include tags from the attributeTarget
6.1 minor release