@@ -320,8 +320,8 @@ To fix that, add an :ref:`alias <service-autowiring-alias>`:
320
320
321
321
App\Util\Rot13Transformer : ~
322
322
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
325
325
App\Util\TransformerInterface : ' @App\Util\Rot13Transformer'
326
326
327
327
.. code-block :: xml
@@ -428,7 +428,7 @@ type hinted, but use the ``UppercaseTransformer`` implementation in some
428
428
specific cases. To do so, you can create a normal alias from the
429
429
``TransformerInterface `` interface to ``Rot13Transformer ``, and then
430
430
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 a argument name matching the one you use when doing
432
432
the injection::
433
433
434
434
// src/Service/MastodonClient.php
@@ -464,13 +464,13 @@ the injection::
464
464
App\Util\Rot13Transformer : ~
465
465
App\Util\UppercaseTransformer : ~
466
466
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
470
470
App\Util\TransformerInterface $shoutyTransformer : ' @App\Util\UppercaseTransformer'
471
471
472
472
# 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
474
474
# service will be injected.
475
475
App\Util\TransformerInterface : ' @App\Util\Rot13Transformer'
476
476
@@ -527,7 +527,7 @@ the injection::
527
527
528
528
// the App\Util\UppercaseTransformer service will be
529
529
// injected when an App\Util\TransformerInterface
530
- // type-hint for a $shoutyTransformer argument is detected.
530
+ // type-hint for a $shoutyTransformer argument is detected
531
531
$services->alias(TransformerInterface::class.' $shoutyTransformer', UppercaseTransformer::class);
532
532
533
533
// If the argument used for injection does not match, but the
@@ -555,13 +555,17 @@ under the arguments key.
555
555
556
556
Another possibility is to use the ``#[Target] `` attribute. By using this attribute
557
557
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).
561
561
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 it the name of the ``$shoutyTransformer `` argument::
565
569
566
570
// src/Service/MastodonClient.php
567
571
namespace App\Service;
@@ -573,12 +577,16 @@ attribute like this::
573
577
{
574
578
private $transformer;
575
579
576
- public function __construct(#[Target('app.uppercase_transformer')] TransformerInterface $transformer)
577
- {
580
+ public function __construct(
581
+ #[Target('shoutyTransformer')] TransformerInterface $transformer
582
+ ) {
578
583
$this->transformer = $transformer;
579
584
}
580
585
}
581
586
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
+
582
590
.. note ::
583
591
584
592
Some IDEs will show an error when using ``#[Target] `` as in the previous example:
0 commit comments