Skip to content

[DependencyInjection] Allow loading and dumping tags with an attribute named "name" #18802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions service_container/tags.rst
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,45 @@ To answer this, change the service declaration:
;
};

.. tip::

In XML and YAML format, you may provide the tag an attribute called ``name``.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this "feature".

  • Is the name attribute special here?
  • If it's special: what does that imply?
  • If it's not special: why mention this at all? If it's just like any other attribute, why mention this name attribute in particular?

Thanks!

Copy link
Member Author

@alexandre-daubois alexandre-daubois Aug 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name attribute is not special itself. Actually, this tip indicates that you can use an attribute named name, which wasn't possible before 5.1. Also, this shows that if you need to add a name attribute on a tag, you must use a "special" syntax. Otherwise, the name attribute will be interpreted as the tag name, not as an attribute of it.

By doing

MailerSendmailTransport:
    tags:
        - { name: 'app.mail_transport', alias: 'sendmail' }

You declare a tag with the app.mail_transport name and an attribute called alias. This is a bit misleading because alias and name keys are at the same level. If you actually need an attribute called name, you must do the following:

MailerSmtpTransport:
    arguments: ['%mailer_host%']
        tags:
            - app.mail_transport: { name: 'arbitrary-value', alias: 'smtp' }

It took me some time to fully understand what this PR was all about 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default, the name XML attribute contains the tag name, and so cannot be used to set an attribute named name. As of 5.1, there is a way to use name as an attribute by passing the tag name in the body of the XML element instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your explanation. Now I can fully understand what's happening here. So, I've done some tweaks while merging to hopefully make it less confusing to readers: 6e6565b

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect! 🙂

When doing so, this is the syntax you should follow:

.. configuration-block::

.. code-block:: yaml

# config/services.yaml
services:
MailerSmtpTransport:
arguments: ['%mailer_host%']
tags:
- app.mail_transport: { name: 'arbitrary-value', alias: 'smtp' }

.. code-block:: xml

<!-- config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="MailerSmtpTransport">
<argument>%mailer_host%</argument>

<tag name="arbitrary-value" alias="smtp">app.mail_transport</tag>
</service>
</services>
</container>

.. versionadded:: 5.1

The possibility to add the ``name`` attribute to a tag in XML and YAML
formats was introduced in Symfony 5.1.

.. tip::

In YAML format, you may provide the tag as a simple string as long as
Expand Down