Skip to content

Commit b8d488d

Browse files
committed
[DependencyInjection] Clarify the #[Target] attribute
1 parent a619916 commit b8d488d

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

service_container/autowiring.rst

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,8 @@ To fix that, add an :ref:`alias <service-autowiring-alias>`:
317317
318318
App\Util\Rot13Transformer: ~
319319
320-
# the ``App\Util\Rot13Transformer`` service will be injected when
321-
# an ``App\Util\TransformerInterface`` type-hint is detected
320+
# the App\Util\Rot13Transformer service will be injected when
321+
# an App\Util\TransformerInterface type-hint is detected
322322
App\Util\TransformerInterface: '@App\Util\Rot13Transformer'
323323
324324
.. code-block:: xml
@@ -422,7 +422,7 @@ type hinted, but use the ``UppercaseTransformer`` implementation in some
422422
specific cases. To do so, you can create a normal alias from the
423423
``TransformerInterface`` interface to ``Rot13Transformer``, and then
424424
create a *named autowiring alias* from a special string containing the
425-
interface followed by a variable name matching the one you use when doing
425+
interface followed by an argument name matching the one you use when doing
426426
the injection::
427427

428428
// src/Service/MastodonClient.php
@@ -456,13 +456,13 @@ the injection::
456456
App\Util\Rot13Transformer: ~
457457
App\Util\UppercaseTransformer: ~
458458
459-
# the ``App\Util\UppercaseTransformer`` service will be
460-
# injected when an ``App\Util\TransformerInterface``
461-
# type-hint for a ``$shoutyTransformer`` argument is detected.
459+
# the App\Util\UppercaseTransformer service will be
460+
# injected when an App\Util\TransformerInterface
461+
# type-hint for a $shoutyTransformer argument is detected
462462
App\Util\TransformerInterface $shoutyTransformer: '@App\Util\UppercaseTransformer'
463463
464464
# If the argument used for injection does not match, but the
465-
# type-hint still matches, the ``App\Util\Rot13Transformer``
465+
# type-hint still matches, the App\Util\Rot13Transformer
466466
# service will be injected.
467467
App\Util\TransformerInterface: '@App\Util\Rot13Transformer'
468468
@@ -519,7 +519,7 @@ the injection::
519519
520520
// the App\Util\UppercaseTransformer service will be
521521
// injected when an App\Util\TransformerInterface
522-
// type-hint for a $shoutyTransformer argument is detected.
522+
// type-hint for a $shoutyTransformer argument is detected
523523
$services->alias(TransformerInterface::class.' $shoutyTransformer', UppercaseTransformer::class);
524524
525525
// If the argument used for injection does not match, but the
@@ -547,13 +547,17 @@ under the arguments key.
547547

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

554-
Let's say you defined the ``app.uppercase_transformer`` alias for the
555-
``App\Util\UppercaseTransformer`` service. You would be able to use the ``#[Target]``
556-
attribute like this::
554+
.. warning::
555+
556+
The ``#[Target]`` attribute only accepts the name of the argument used in the named
557+
alias, it **does not** accept service ids or service aliases.
558+
559+
Suppose you want to inject the ``App\Util\UppercaseTransformer`` service. You would use
560+
the ``#[Target]`` attribute by passing the name of the ``$shoutyTransformer`` argument::
557561

558562
// src/Service/MastodonClient.php
559563
namespace App\Service;
@@ -564,12 +568,15 @@ attribute like this::
564568
class MastodonClient
565569
{
566570
public function __construct(
567-
#[Target('app.uppercase_transformer')]
568-
private TransformerInterface $transformer
569-
){
571+
#[Target('shoutyTransformer')]
572+
private TransformerInterface $transformer,
573+
) {
570574
}
571575
}
572576

577+
Since the ``#[Target]`` attribute normalizes the string passed to it to its camelCased form,
578+
name variations such as ``shouty.transformer`` also work.
579+
573580
.. note::
574581

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

0 commit comments

Comments
 (0)