@@ -363,9 +363,37 @@ If you register this as a service, you now have *two* services that implement th
363
363
which one to use. Remember, autowiring isn't magic; it looks for a service
364
364
whose id matches the type-hint. So you need to choose one by creating an alias
365
365
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.
366
369
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
+ }
369
397
370
398
.. configuration-block ::
371
399
@@ -378,15 +406,21 @@ that alias:
378
406
App\Util\Rot13Transformer : ~
379
407
App\Util\UppercaseTransformer : ~
380
408
409
+ # the ``App\Util\UppercaseTransformer`` service will be
410
+ # injected when an ``App\Util\TransformerInterface``
411
+ # type-hint for a ``$shoutyTransformer`` argument is detected.
381
412
# 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
383
415
App\Util\TransformerInterface : ' @App\Util\Rot13Transformer'
416
+ App\Util\TransformerInterface $shoutyTransformer : ' @App\Util\UppercaseTransformer'
384
417
385
418
App\Service\TwitterClient :
386
419
# the Rot13Transformer will be passed as the $transformer argument
387
420
autowire : true
388
421
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
390
424
# arguments:
391
425
# $transformer: '@App\Util\UppercaseTransformer'
392
426
# ...
@@ -405,6 +439,9 @@ that alias:
405
439
<service id =" App\Util\UppercaseTransformer" />
406
440
407
441
<service id =" App\Util\TransformerInterface" alias =" App\Util\Rot13Transformer" />
442
+ <service
443
+ id =" App\Util\TransformerInterface $shoutyTransformer"
444
+ alias =" App\Util\UppercaseTransformer" />
408
445
409
446
<service id =" App\Service\TwitterClient" autowire =" true" >
410
447
<!-- <argument key="$transformer" type="service" id="App\Util\UppercaseTransformer"/> -->
@@ -419,6 +456,7 @@ that alias:
419
456
use App\Util\UppercaseTransformer;
420
457
use App\Util\TransformerInterface;
421
458
use App\Service\TwitterClient;
459
+ use App\Service\MastodonClient;
422
460
423
461
// ...
424
462
$container->autowire(Rot13Transformer::class);
@@ -427,12 +465,23 @@ that alias:
427
465
$container->autowire(TwitterClient::class)
428
466
//->setArgument('$transformer', new Reference(UppercaseTransformer::class))
429
467
;
468
+ $container->registerAliasForArgument(
469
+ TransformerInterface::class,
470
+ MastodonClient::class,
471
+ 'shoutyVariable'
472
+ );
430
473
431
474
Thanks to the ``App\Util\TransformerInterface `` alias, any argument type-hinted
432
475
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
434
479
under the arguments key.
435
480
481
+ .. versionadded :: 4.2
482
+
483
+ Named aliases have been introduced in Symfony 4.2
484
+
436
485
Fixing Non-Autowireable Arguments
437
486
---------------------------------
438
487
0 commit comments