Skip to content

[DependencyInjection] Clarify the #[Target] attribute #19789

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
Jun 24, 2024
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
40 changes: 24 additions & 16 deletions service_container/autowiring.rst
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,8 @@ To fix that, add an :ref:`alias <service-autowiring-alias>`:

App\Util\Rot13Transformer: ~

# the ``App\Util\Rot13Transformer`` service will be injected when
# an ``App\Util\TransformerInterface`` type-hint is detected
# the App\Util\Rot13Transformer service will be injected when
# an App\Util\TransformerInterface type-hint is detected
App\Util\TransformerInterface: '@App\Util\Rot13Transformer'

.. code-block:: xml
Expand Down Expand Up @@ -428,7 +428,7 @@ type hinted, but use the ``UppercaseTransformer`` implementation in some
specific cases. To do so, you can create a normal alias from the
``TransformerInterface`` interface to ``Rot13Transformer``, and then
create a *named autowiring alias* from a special string containing the
interface followed by a variable name matching the one you use when doing
interface followed by an argument name matching the one you use when doing
the injection::

// src/Service/MastodonClient.php
Expand Down Expand Up @@ -464,13 +464,13 @@ the injection::
App\Util\Rot13Transformer: ~
App\Util\UppercaseTransformer: ~

# the ``App\Util\UppercaseTransformer`` service will be
# injected when an ``App\Util\TransformerInterface``
# type-hint for a ``$shoutyTransformer`` argument is detected.
# the App\Util\UppercaseTransformer service will be
# injected when an App\Util\TransformerInterface
# type-hint for a $shoutyTransformer argument is detected
App\Util\TransformerInterface $shoutyTransformer: '@App\Util\UppercaseTransformer'

# If the argument used for injection does not match, but the
# type-hint still matches, the ``App\Util\Rot13Transformer``
# type-hint still matches, the App\Util\Rot13Transformer
# service will be injected.
App\Util\TransformerInterface: '@App\Util\Rot13Transformer'

Expand Down Expand Up @@ -527,7 +527,7 @@ the injection::

// the App\Util\UppercaseTransformer service will be
// injected when an App\Util\TransformerInterface
// type-hint for a $shoutyTransformer argument is detected.
// type-hint for a $shoutyTransformer argument is detected
$services->alias(TransformerInterface::class.' $shoutyTransformer', UppercaseTransformer::class);

// If the argument used for injection does not match, but the
Expand Down Expand Up @@ -555,13 +555,17 @@ under the arguments key.

Another possibility is to use the ``#[Target]`` attribute. By using this attribute
on the argument you want to autowire, you can define exactly which service to inject
by using its alias. Thanks to this, you're able to have multiple services implementing
the same interface and keep the argument name decorrelated of any implementation name
(like shown in the example above).
by passing the name of the argument used in the named alias. Thanks to this, you're able
to have multiple services implementing the same interface and keep the argument name
decorrelated of any implementation name (like shown in the example above).

Let's say you defined the ``app.uppercase_transformer`` alias for the
``App\Util\UppercaseTransformer`` service. You would be able to use the ``#[Target]``
attribute like this::
.. warning::

The ``#[Target]`` attribute only accepts the name of the argument used in the named
alias, it **does not** accept service ids or service aliases.

Suppose you want to inject the ``App\Util\UppercaseTransformer`` service. You would use
the ``#[Target]`` attribute by passing the name of the ``$shoutyTransformer`` argument::

// src/Service/MastodonClient.php
namespace App\Service;
Expand All @@ -573,12 +577,16 @@ attribute like this::
{
private $transformer;

public function __construct(#[Target('app.uppercase_transformer')] TransformerInterface $transformer)
{
public function __construct(
#[Target('shoutyTransformer')] TransformerInterface $transformer,
) {
$this->transformer = $transformer;
}
}

Since the ``#[Target]`` attribute normalizes the string passed to it to its camelCased form,
name variations such as ``shouty.transformer`` also work.

.. note::

Some IDEs will show an error when using ``#[Target]`` as in the previous example:
Expand Down
Loading