Description
Currently tags are just strings and the attributes are arrays whose values depend on the tag. See for example
symfony/src/Symfony/Bundle/TwigBundle/Resources/config/twig.php
Lines 95 to 97 in 9b08626
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.