Skip to content

Commit a35d20e

Browse files
committed
Document how to use named aliases
1 parent a588a92 commit a35d20e

File tree

1 file changed

+54
-5
lines changed

1 file changed

+54
-5
lines changed

service_container/autowiring.rst

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,37 @@ If you register this as a service, you now have *two* services that implement th
363363
which one to use. Remember, autowiring isn't magic; it looks for a service
364364
whose id matches the type-hint. So you need to choose one by creating an alias
365365
from the type to the correct service id (see :ref:`autowiring-interface-alias`).
366+
Additionally, you can define several named aliases if you want to use
367+
one implementation in some cases, and another implementation in some
368+
other cases.
366369

367-
If you want ``Rot13Transformer`` to be the service that's used for autowiring, create
368-
that alias:
370+
If you want ``Rot13Transformer`` to be the service that's used for
371+
autowiring by default, and use ``UppercaseTransformer`` in some special
372+
cases, you can create a simple alias from the interface to
373+
``Rot13Transformer``, and then create a *named alias* from a special
374+
string containing the interface followed by a variable named you will
375+
have to use when doing the injection::
376+
377+
namespace App\Service;
378+
379+
use App\Util\TransformerInterface;
380+
381+
class MastodonClient
382+
{
383+
private $transformer;
384+
385+
public function __construct(TransformerInterface $shoutyTransformer)
386+
{
387+
$this->transformer = $shoutyTransformer;
388+
}
389+
390+
public function toot($user, $key, $status)
391+
{
392+
$transformedStatus = $this->transformer->transform($status);
393+
394+
// ... connect to Mastodon and send the transformed status
395+
}
396+
}
369397

370398
.. configuration-block::
371399

@@ -378,15 +406,21 @@ that alias:
378406
App\Util\Rot13Transformer: ~
379407
App\Util\UppercaseTransformer: ~
380408
409+
# the ``App\Util\UppercaseTransformer`` service will be
410+
# injected when an ``App\Util\TransformerInterface``
411+
# type-hint for a ``$shoutyTransformer`` argument is detected.
381412
# the ``App\Util\Rot13Transformer`` service will be injected when
382-
# a ``App\Util\TransformerInterface`` type-hint is detected
413+
# an ``App\Util\TransformerInterface`` type-hint is detected,
414+
# but the argument name does not match that of a named alias
383415
App\Util\TransformerInterface: '@App\Util\Rot13Transformer'
416+
App\Util\TransformerInterface $shoutyTransformer: '@App\Util\UppercaseTransformer'
384417
385418
App\Service\TwitterClient:
386419
# the Rot13Transformer will be passed as the $transformer argument
387420
autowire: true
388421
389-
# If you wanted to choose the non-default service, wire it manually
422+
# If you wanted to choose the non-default service and
423+
# do not want to use a named alias, wire it manually
390424
# arguments:
391425
# $transformer: '@App\Util\UppercaseTransformer'
392426
# ...
@@ -405,6 +439,9 @@ that alias:
405439
<service id="App\Util\UppercaseTransformer"/>
406440
407441
<service id="App\Util\TransformerInterface" alias="App\Util\Rot13Transformer"/>
442+
<service
443+
id="App\Util\TransformerInterface $shoutyTransformer"
444+
alias="App\Util\UppercaseTransformer"/>
408445
409446
<service id="App\Service\TwitterClient" autowire="true">
410447
<!-- <argument key="$transformer" type="service" id="App\Util\UppercaseTransformer"/> -->
@@ -419,6 +456,7 @@ that alias:
419456
use App\Util\UppercaseTransformer;
420457
use App\Util\TransformerInterface;
421458
use App\Service\TwitterClient;
459+
use App\Service\MastodonClient;
422460
423461
// ...
424462
$container->autowire(Rot13Transformer::class);
@@ -427,12 +465,23 @@ that alias:
427465
$container->autowire(TwitterClient::class)
428466
//->setArgument('$transformer', new Reference(UppercaseTransformer::class))
429467
;
468+
$container->registerAliasForArgument(
469+
TransformerInterface::class,
470+
MastodonClient::class,
471+
'shoutyVariable'
472+
);
430473
431474
Thanks to the ``App\Util\TransformerInterface`` alias, any argument type-hinted
432475
with this interface will be passed the ``App\Util\Rot13Transformer`` service.
433-
But, you can also manually wire the *other* service by specifying the argument
476+
If the argument is named ``$shoutyTransformer``,
477+
``App\Util\UppercaseTransformer`` will be used instead.
478+
But, you can also manually wire any *other* service by specifying the argument
434479
under the arguments key.
435480

481+
.. versionadded:: 4.2
482+
483+
Named aliases have been introduced in Symfony 4.2
484+
436485
Fixing Non-Autowireable Arguments
437486
---------------------------------
438487

0 commit comments

Comments
 (0)