Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit59d5b18

Browse files
committed
minor#19789 [DependencyInjection] Clarify the#[Target] attribute (HypeMC)
This PR was merged into the 5.4 branch.Discussion----------[DependencyInjection] Clarify the `#[Target]` attributeThe `#[Target]` attribute seems to be a constant source of confusion for developers, as evident by:-symfony/symfony#50541-symfony/symfony#51565-symfony/symfony#54578Also, the example given is either unclear or just wrong. Hopefully, this helps clarify things.Commits-------2fb1ada [DependencyInjection] Clarify the `#[Target]` attribute
2 parentse1b69a1 +2fb1ada commit59d5b18

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

‎service_container/autowiring.rst‎

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,8 @@ To fix that, add an :ref:`alias <service-autowiring-alias>`:
320320
321321
App\Util\Rot13Transformer:~
322322
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
325325
App\Util\TransformerInterface:'@App\Util\Rot13Transformer'
326326
327327
..code-block::xml
@@ -428,7 +428,7 @@ type hinted, but use the ``UppercaseTransformer`` implementation in some
428428
specific cases. To do so, you can create a normal alias from the
429429
``TransformerInterface`` interface to ``Rot13Transformer``, and then
430430
create a *named autowiring alias* from a special string containing the
431-
interface followed bya variable name matching the one you use when doing
431+
interface followed byan argument name matching the one you use when doing
432432
the injection::
433433

434434
// src/Service/MastodonClient.php
@@ -464,13 +464,13 @@ the injection::
464464
App\Util\Rot13Transformer:~
465465
App\Util\UppercaseTransformer:~
466466
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
470470
App\Util\TransformerInterface $shoutyTransformer:'@App\Util\UppercaseTransformer'
471471
472472
# 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
474474
# service will be injected.
475475
App\Util\TransformerInterface:'@App\Util\Rot13Transformer'
476476
@@ -527,7 +527,7 @@ the injection::
527527
528528
// the App\Util\UppercaseTransformer service will be
529529
// injected when an App\Util\TransformerInterface
530-
// type-hint for a $shoutyTransformer argument is detected.
530+
// type-hint for a $shoutyTransformer argument is detected
531531
$services->alias(TransformerInterface::class.' $shoutyTransformer', UppercaseTransformer::class);
532532
533533
// If the argument used for injection does not match, but the
@@ -555,13 +555,17 @@ under the arguments key.
555555

556556
Another possibility is to use the ``#[Target]`` attribute. By using this attribute
557557
on the argument you want to autowire, you can define exactly which service to inject
558-
byusing itsalias. 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+
bypassing the name of the argument used in the namedalias. Thanks to this, you're able
559+
to have multiple services implementingthe same interface and keep the argument name
560+
decorrelated of any implementation name(like shown in the example above).
561561

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 the name of the ``$shoutyTransformer`` argument::
565569

566570
// src/Service/MastodonClient.php
567571
namespace App\Service;
@@ -573,12 +577,16 @@ attribute like this::
573577
{
574578
private $transformer;
575579

576-
public function __construct(#[Target('app.uppercase_transformer')] TransformerInterface $transformer)
577-
{
580+
public function __construct(
581+
#[Target('shoutyTransformer')] TransformerInterface $transformer,
582+
) {
578583
$this->transformer = $transformer;
579584
}
580585
}
581586

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+
582590
..note::
583591

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp