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