@@ -363,9 +363,40 @@ 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
+
371
+ For instance, you may want to use by default the ``Rot13Transformer ``
372
+ implementation when the ``TransformerInterface `` interface is type
373
+ hinted, but use the ``UppercaseTransformer `` implementation in some
374
+ specific cases. To do so, you can create a normal alias from the
375
+ ``TransformerInterface `` interface to ``Rot13Transformer, `` and then
376
+ create a *named alias * from a special string containing the interface
377
+ followed by a variable named you will have to use when doing the
378
+ injection::
379
+
380
+ namespace App\Service;
381
+
382
+ use App\Util\TransformerInterface;
383
+
384
+ class MastodonClient
385
+ {
386
+ private $transformer;
387
+
388
+ public function __construct(TransformerInterface $shoutyTransformer)
389
+ {
390
+ $this->transformer = $shoutyTransformer;
391
+ }
392
+
393
+ public function toot($user, $key, $status)
394
+ {
395
+ $transformedStatus = $this->transformer->transform($status);
396
+
397
+ // ... connect to Mastodon and send the transformed status
398
+ }
399
+ }
369
400
370
401
.. configuration-block ::
371
402
@@ -378,15 +409,21 @@ that alias:
378
409
App\Util\Rot13Transformer : ~
379
410
App\Util\UppercaseTransformer : ~
380
411
412
+ # the ``App\Util\UppercaseTransformer`` service will be
413
+ # injected when an ``App\Util\TransformerInterface``
414
+ # type-hint for a ``$shoutyTransformer`` argument is detected.
381
415
# the ``App\Util\Rot13Transformer`` service will be injected when
382
- # a ``App\Util\TransformerInterface`` type-hint is detected
416
+ # an ``App\Util\TransformerInterface`` type-hint is detected,
417
+ # but the argument name does not match that of a named alias
383
418
App\Util\TransformerInterface : ' @App\Util\Rot13Transformer'
419
+ App\Util\TransformerInterface $shoutyTransformer : ' @App\Util\UppercaseTransformer'
384
420
385
421
App\Service\TwitterClient :
386
422
# the Rot13Transformer will be passed as the $transformer argument
387
423
autowire : true
388
424
389
- # If you wanted to choose the non-default service, wire it manually
425
+ # If you wanted to choose the non-default service and
426
+ # do not want to use a named alias, wire it manually
390
427
# arguments:
391
428
# $transformer: '@App\Util\UppercaseTransformer'
392
429
# ...
@@ -405,6 +442,9 @@ that alias:
405
442
<service id =" App\Util\UppercaseTransformer" />
406
443
407
444
<service id =" App\Util\TransformerInterface" alias =" App\Util\Rot13Transformer" />
445
+ <service
446
+ id =" App\Util\TransformerInterface $shoutyTransformer"
447
+ alias =" App\Util\UppercaseTransformer" />
408
448
409
449
<service id =" App\Service\TwitterClient" autowire =" true" >
410
450
<!-- <argument key="$transformer" type="service" id="App\Util\UppercaseTransformer"/> -->
@@ -419,20 +459,33 @@ that alias:
419
459
use App\Util\UppercaseTransformer;
420
460
use App\Util\TransformerInterface;
421
461
use App\Service\TwitterClient;
462
+ use App\Service\MastodonClient;
422
463
423
464
// ...
424
465
$container->autowire(Rot13Transformer::class);
425
466
$container->autowire(UppercaseTransformer::class);
426
467
$container->setAlias(TransformerInterface::class, Rot13Transformer::class);
468
+ $container->registerAliasForArgument(
469
+ TransformerInterface::class,
470
+ UppercaseTransformer::class,
471
+ 'shoutyVariable'
472
+ );
427
473
$container->autowire(TwitterClient::class)
428
474
//->setArgument('$transformer', new Reference(UppercaseTransformer::class))
429
475
;
476
+ $container->autowire(MastodonClient::class);
430
477
431
478
Thanks to the ``App\Util\TransformerInterface `` alias, any argument type-hinted
432
479
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
480
+ If the argument is named ``$shoutyTransformer ``,
481
+ ``App\Util\UppercaseTransformer `` will be used instead.
482
+ But, you can also manually wire any *other * service by specifying the argument
434
483
under the arguments key.
435
484
485
+ .. versionadded :: 4.2
486
+
487
+ Named aliases have been introduced in Symfony 4.2
488
+
436
489
Fixing Non-Autowireable Arguments
437
490
---------------------------------
438
491
0 commit comments