Skip to content

Commit f1df4ab

Browse files
committed
[DependencyInjection] Document FQCN aliases
1 parent 01b10f1 commit f1df4ab

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

components/dependency_injection/autowiring.rst

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,10 @@ and a Twitter client using it:
308308
services:
309309
rot13_transformer:
310310
class: Acme\Rot13Transformer
311-
autowiring_types: Acme\TransformerInterface
311+
312+
Acme\TransformerInterface:
313+
alias: rot13_transformer
314+
public: false
312315
313316
twitter_client:
314317
class: Acme\TwitterClient
@@ -330,9 +333,8 @@ and a Twitter client using it:
330333
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
331334
332335
<services>
333-
<service id="rot13_transformer" class="Acme\Rot13Transformer">
334-
<autowiring-type>Acme\TransformerInterface</autowiring-type>
335-
</service>
336+
<service id="rot13_transformer" class="Acme\Rot13Transformer" />
337+
<service id="Acme\TransformerInterface" alias="rot13_transformer" public="false" />
336338
337339
<service id="twitter_client" class="Acme\TwitterClient" autowire="true" />
338340
@@ -352,13 +354,13 @@ and a Twitter client using it:
352354
use Acme\TransformerInterface;
353355
use Acme\TwitterClient;
354356
use Acme\UppercaseTransformer;
357+
use Symfony\Component\DependencyInjection\Alias;
355358
use Symfony\Component\DependencyInjection\Reference;
356359
use Symfony\Component\DependencyInjection\Definition;
357360
358361
// ...
359-
$rot13Definition = new Definition(Rot13Transformer::class);
360-
$rot13Definition->setAutowiringTypes(array(TransformerInterface::class));
361-
$container->setDefinition('rot13_transformer', $rot13Definition);
362+
$container->register('rot13_transformer', Rot13Transformer::class);
363+
$container->setAlias(TransformerInterface::class, new Alias('rot13_transformer', false))
362364
363365
$clientDefinition = new Definition(TwitterClient::class);
364366
$clientDefinition->setAutowired(true);
@@ -382,10 +384,14 @@ to use which leads to errors like this:
382384
[Symfony\Component\DependencyInjection\Exception\RuntimeException]
383385
Unable to autowire argument of type "Acme\TransformerInterface" for the service "twitter_client".
384386
385-
Fortunately, the ``autowiring_types`` key is here to specify which implementation
386-
to use by default. This key can take a list of types if necessary.
387+
Fortunately, the FQCN alias is here to specify which implementation
388+
to use by default.
389+
390+
.. versionadded:: 3.2
391+
Using FQCN aliases to fix autowiring ambiguities is allowed since Symfony
392+
3.3. Prior to version 3.3, you needed to use the ``autowiring_types`` key.
387393

388-
Thanks to this setting, the ``rot13_transformer`` service is automatically injected
394+
Thanks to this alias, the ``rot13_transformer`` service is automatically injected
389395
as an argument of the ``uppercase_transformer`` and ``twitter_client`` services. For
390396
the ``uppercase_twitter_client``, a standard service definition is used to
391397
inject the specific ``uppercase_transformer`` service.

0 commit comments

Comments
 (0)