@@ -317,8 +317,8 @@ To fix that, add an :ref:`alias <service-autowiring-alias>`:
317
317
318
318
App\Util\Rot13Transformer : ~
319
319
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
322
322
App\Util\TransformerInterface : ' @App\Util\Rot13Transformer'
323
323
324
324
.. code-block :: xml
@@ -422,7 +422,7 @@ type hinted, but use the ``UppercaseTransformer`` implementation in some
422
422
specific cases. To do so, you can create a normal alias from the
423
423
``TransformerInterface `` interface to ``Rot13Transformer ``, and then
424
424
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
426
426
the injection::
427
427
428
428
// src/Service/MastodonClient.php
@@ -456,13 +456,13 @@ the injection::
456
456
App\Util\Rot13Transformer : ~
457
457
App\Util\UppercaseTransformer : ~
458
458
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
462
462
App\Util\TransformerInterface $shoutyTransformer : ' @App\Util\UppercaseTransformer'
463
463
464
464
# 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
466
466
# service will be injected.
467
467
App\Util\TransformerInterface : ' @App\Util\Rot13Transformer'
468
468
@@ -519,7 +519,7 @@ the injection::
519
519
520
520
// the App\Util\UppercaseTransformer service will be
521
521
// injected when an App\Util\TransformerInterface
522
- // type-hint for a $shoutyTransformer argument is detected.
522
+ // type-hint for a $shoutyTransformer argument is detected
523
523
$services->alias(TransformerInterface::class.' $shoutyTransformer', UppercaseTransformer::class);
524
524
525
525
// If the argument used for injection does not match, but the
@@ -547,13 +547,17 @@ under the arguments key.
547
547
548
548
Another possibility is to use the ``#[Target] `` attribute. By using this attribute
549
549
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).
553
553
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::
557
561
558
562
// src/Service/MastodonClient.php
559
563
namespace App\Service;
@@ -564,12 +568,15 @@ attribute like this::
564
568
class MastodonClient
565
569
{
566
570
public function __construct(
567
- #[Target('app.uppercase_transformer ')]
568
- private TransformerInterface $transformer
569
- ){
571
+ #[Target('shoutyTransformer ')]
572
+ private TransformerInterface $transformer,
573
+ ) {
570
574
}
571
575
}
572
576
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
+
573
580
.. note ::
574
581
575
582
Some IDEs will show an error when using ``#[Target] `` as in the previous example:
0 commit comments