Skip to content

Add #[Tag] class-level attribute for auto-tagging #292

@philipobenito

Description

@philipobenito

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

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