Skip to content

Commit 2cad785

Browse files
committed
[DependencyInjection] Clarify the #[Target] attribute 2
1 parent b8d488d commit 2cad785

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

service_container/autowiring.rst

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,13 +549,35 @@ Another possibility is to use the ``#[Target]`` attribute. By using this attribu
549549
on the argument you want to autowire, you can define exactly which service to inject
550550
by passing the name of the argument used in the named alias. Thanks to this, you're able
551551
to have multiple services implementing the same interface and keep the argument name
552-
decorrelated of any implementation name (like shown in the example above).
552+
decorrelated of any implementation name (like shown in the example above). In addition,
553+
you'll get an exception in case you make any typo in the target name.
553554

554555
.. warning::
555556

556557
The ``#[Target]`` attribute only accepts the name of the argument used in the named
557558
alias, it **does not** accept service ids or service aliases.
558559

560+
You can get a list of named autowiring aliases by running the ``debug:autowiring`` command::
561+
562+
.. code-block:: terminal
563+
564+
$ php bin/console debug:autowiring LoggerInterface
565+
566+
Autowirable Types
567+
=================
568+
569+
The following classes & interfaces can be used as type-hints when autowiring:
570+
(only showing classes/interfaces matching LoggerInterface)
571+
572+
Describes a logger instance.
573+
Psr\Log\LoggerInterface - alias:monolog.logger
574+
Psr\Log\LoggerInterface $assetMapperLogger - target:asset_mapperLogger - alias:monolog.logger.asset_mapper
575+
Psr\Log\LoggerInterface $cacheLogger - alias:monolog.logger.cache
576+
Psr\Log\LoggerInterface $httpClientLogger - target:http_clientLogger - alias:monolog.logger.http_client
577+
Psr\Log\LoggerInterface $mailerLogger - alias:monolog.logger.mailer
578+
579+
[...]
580+
559581
Suppose you want to inject the ``App\Util\UppercaseTransformer`` service. You would use
560582
the ``#[Target]`` attribute by passing the name of the ``$shoutyTransformer`` argument::
561583

0 commit comments

Comments
 (0)