10000 minor #19814 [DependencyInjection] Clarify the `#[Target]` attribute … · symfony/symfony-docs@35d808d · GitHub
[go: up one dir, main page]

Skip to content

Commit 35d808d

Browse files
committed
minor #19814 [DependencyInjection] Clarify the #[Target] attribute 2 (HypeMC)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [DependencyInjection] Clarify the `#[Target]` attribute 2 Follow-up to #19789, adds a [paragraph](bd00116) about the `debug:autowiring` command. Commits ------- 81cc7f0 [DependencyInjection] Clarify the `#[Target]` attribute 2
2 parents ad1e3b2 + 81cc7f0 commit 35d808d

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

service_container/autowiring.rst

+23-1
Original file line numberDiff line numberDiff line change
@@ -549,13 +549,35 @@ Another option is to use the ``#[Target]`` attribute. By adding this attribute
549549
to the argument you want to autowire, you can specify which service to inject by
550550
passing the name of the argument used in the named alias. This way, you can have
551551
multiple services implementing the same interface and keep the argument name
552-
separate from any implementation name (like shown in the example above).
552+
separate from 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
557558
named 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)
0