8000 minor #19789 [DependencyInjection] Clarify the `#[Target]` attribute … · symfony/symfony-docs@59d5b18 · GitHub
[go: up one dir, main page]

Skip to content

Commit 59d5b18

Browse files
committed
minor #19789 [DependencyInjection] Clarify the #[Target] attribute (HypeMC)
This PR was merged into the 5.4 branch. Discussion ---------- [DependencyInjection] Clarify the `#[Target]` attribute The `#[Target]` attribute seems to be a constant source of confusion for developers, as evident by: - symfony/symfony#50541 - symfony/symfony#51565 - symfony/symfony#54578 Also, the example given is either unclear or just wrong. Hopefully, this helps clarify things. Commits ------- 2fb1ada [DependencyInjection] Clarify the `#[Target]` attribute
2 parents e1b69a1 + 2fb1ada commit 59d5b18

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

service_container/autowiring.rst

+24-16
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,8 @@ To fix that, add an :ref:`alias <service-autowiring-alias>`:
320320
321321
App\Util\Rot13Transformer: ~
322322
323-
# the ``App\Util\Rot13Transformer`` service will be injected when
324-
# an ``App\Util\TransformerInterface`` type-hint is detected
323+
# the App\Util\Rot13Transformer service will be injected when
324+
# an App\Util\TransformerInterface type-hint is detected
325325
App\Util\TransformerInterface: '@App\Util\Rot13Transformer'
326326
327327
.. code-block:: xml
@@ -428,7 +428,7 @@ type hinted, but use the ``UppercaseTransformer`` implementation in some
428428
specific cases. To do so, you can create a normal alias from the
429429
``TransformerInterface`` interface to ``Rot13Transformer``, and then
430430
create a *named autowiring alias* from a special string containing the
431-
interface followed by a variable name matching the one you use when doing
431+
interface followed by an argument name matching the one you use when doing
432432
the injection::
433433

434434
// src/Service/MastodonClient.php
@@ -464,13 +464,13 @@ the injection::
464464
App\Util\Rot13Transformer: ~
465465
App\Util\UppercaseTransformer: ~
466466
467-
# the ``App\Util\UppercaseTransformer`` service will be
468-
# injected when an ``App\Util\TransformerInterface``
469-
# type-hint for a ``$shoutyTransformer`` argument is detected.
467+
# the App\Util\UppercaseTransformer service will be
468+
# injected when an App\Util\TransformerInterface
469+
# type-hint for a $shoutyTransformer argument is detected
470470
App\Util\TransformerInterface $shoutyTransformer: '@App\Util\UppercaseTransformer'
471471
472472
# If the argument used for injection does not match, but the
473-
# type-hint still matches, the ``App\Util\Rot13Transformer``
473+
# type-hint still matches, the App\Util\Rot13Transformer
474474
# service will be injected.
475475
App\Util\TransformerInterface: '@App\Util\Rot13Transformer'
476476
@@ -527,7 +527,7 @@ the injection::
527527
528528
// the App\Util\UppercaseTransformer service will be
529529
// injected when an App\Util\TransformerInterface
530-
// type-hint for a $shoutyTransformer argument is detected.
530+
// type-hint for a $shoutyTransformer argument is detected
531531
$services->alias(TransformerInterface::class.' $shoutyTransformer', UppercaseTransformer::class);
532532
533533
// If the argument used for injection does not match, but the
@@ -555,13 +555,17 @@ under the arguments key.
555555

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

562-
Let's say you defined the ``app.uppercase_transformer`` alias for the
563-
``App\Util\UppercaseTransformer`` service. You would be able to use the ``#[Target]``
564-
attribute like this::
562+
.. warning::
563+
564+
The ``#[Target]`` attribute only accepts the name of the argument used in the named
565+
alias, it **does not** accept service ids or service aliases.
566+
567+
Suppose you want to inject the ``App\Util\UppercaseTransformer`` service. You would use
568+
the ``#[Target]`` attribute by passing the name of the ``$shoutyTransformer`` argument::
565569

566570
// src/Service/MastodonClient.php
567571
namespace App\Service;
@@ -573,12 +577,16 @@ attribute like this::
573577
{
574578
private $transformer;
575579

576-
public function __construct(#[Target('app.uppercase_transformer')] TransformerInterface $transformer)
577-
{
580+
public function __construct(
581+
#[Target('shoutyTransformer')] TransformerInterface $transformer,
582+
) {
578583
$this->transformer = $transformer;
579584
}
580585
}
581586

587+
Since the ``#[Target]`` attribute normalizes the string passed to it to its camelCased form,
588+
name variations such as ``shouty.transformer`` also work.
589+
582590
.. note::
583591

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

0 commit comments

Comments
 (0)
0