Skip to content

Commit fa9b851

Browse files
committed
Merge branch '6.0' into 6.1
* 6.0: [DependencyInjection] Add #[Target] documentation
2 parents e4afa0e + 96198d0 commit fa9b851

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

service_container/autowiring.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,32 @@ If the argument is named ``$shoutyTransformer``,
529529
But, you can also manually wire any *other* service by specifying the argument
530530
under the arguments key.
531531

532+
Another possibility is to use the ``#[Target]`` attribute. By using this attribute
533+
on the argument you want to autowire, you can define exactly which service to inject
534+
by using its alias. Thanks to this, you're able to have multiple services implementing
535+
the same interface and keep the argument name decorrelated of any implementation name
536+
(like shown in the example above).
537+
538+
Let's say you defined the ``app.uppercase_transformer`` alias for the
539+
``App\Util\UppercaseTransformer`` service. You would be able to use the ``#[Target]``
540+
attribute like this::
541+
542+
// src/Service/MastodonClient.php
543+
namespace App\Service;
544+
545+
use App\Util\TransformerInterface;
546+
use Symfony\Component\DependencyInjection\Attribute\Target;
547+
548+
class MastodonClient
549+
{
550+
private $transformer;
551+
552+
public function __construct(#[Target('app.uppercase_transformer')] TransformerInterface $transformer)
553+
{
554+
$this->transformer = $transformer;
555+
}
556+
}
557+
532558
.. _autowire-attribute:
533559

534560
Fixing Non-Autowireable Arguments

0 commit comments

Comments
 (0)