8000 Use classes for container tags · Issue #37194 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Use classes for container tags #37194
Closed
Closed
@Tobion

Description

@Tobion

Currently tags are just strings and the attributes are arrays whose values depend on the tag. See for example

->set('data_collector.twig', TwigDataCollector::class)
->args([service('twig.profile'), service('twig')])
->tag('data_collector', ['template' => '@WebProfiler/Collector/twig.html.twig', 'id' => 'twig', 'priority' => 257])

I propose to allow define tags as classes which just need to implement an interface like

interface ContainerTagInterface
{
    public function getName(): string;
    public function getAttributes(): array;
}

Then we can allow tags to be instances of the interface and can create classes that represent the tags , e.g. ->tag((new DataCollectorTag)->template('...')->id('twig')->priority(257)). This makes tags discovorable, autocomplete, supporting types, documentable in code and validatable.

The main objective with the move to use PHP to define services (#36778) is discovorability and auto-completion. With tags as classes this would also improve the situation for tags as people can easiliy find the available tags as the classes implement this interface and the attributes for each tag allow auto-completion.
Before using php for service definitions, tags as classes wasn't really an option in YAML/XML. But now I think it's time to do that transition for the php configuration format. This is also more in-line with similar concepts like the Messenger stamps that are classes as well.

A previous proposal to use constants for tags (#24377) did not solve the problem for attributes and also does not improve discovorability much as it highly depends on where you define the constants.

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