Skip to content

Commit ad1e3b2

Browse files
committed
Merge branch '5.4' into 6.4
* 5.4: Minor reword [DependencyInjection] Clarify the `#[Target]` attribute
2 parents 5b44145 + dd658c6 commit ad1e3b2

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

service_container/autowiring.rst

Lines changed: 27 additions & 18 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
@@ -545,15 +545,19 @@ If the argument is named ``$shoutyTransformer``,
545545
But, you can also manually wire any *other* service by specifying the argument
546546
under the arguments key.
547547

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).
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
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::
557561

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

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+
573582
.. note::
574583

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy