@@ -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
@@ -545,15 +545,19 @@ If the argument is named ``$shoutyTransformer``,
545
545
But, you can also manually wire any *other * service by specifying the argument
546
546
under the arguments key.
547
547
548
- Another possibility is to use the ``#[Target] `` attribute. By using this attribute
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).
548
+ Another option is to use the ``#[Target] `` attribute. By adding this attribute
549
+ to the argument you want to autowire, you can specify which service to inject by
550
+ passing the name of the argument used in the named alias. This way, you can have
551
+ multiple services implementing the same interface and keep the argument name
552
+ separate from 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
557
+ named 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,17 @@ attribute like this::
564
568
class MastodonClient
565
569
{
566
570
public function __construct(
567
- #[Target('app.uppercase_transformer ')]
571
+ #[Target('shoutyTransformer ')]
568
572
private TransformerInterface $transformer,
569
- ){
573
+ ) {
570
574
}
571
575
}
572
576
577
+ .. tip ::
578
+
579
+ Since the ``#[Target] `` attribute normalizes the string passed to it to its
580
+ camelCased form, name variations (e.g. ``shouty.transformer ``) also work.
581
+
573
582
.. note ::
574
583
575
584
Some IDEs will show an error when using ``#[Target] `` as in the previous example:
0 commit comments