Skip to content

[DependencyInjection] Document FQCN aliases #7656

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions service_container/autowiring.rst
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,9 @@ and a Twitter client using it:

services:
rot13_transformer:
class: Acme\Rot13Transformer
autowiring_types: Acme\TransformerInterface
class: Acme\Rot13Transformer

Acme\TransformerInterface: '@rot13_transformer'

twitter_client:
class: Acme\TwitterClient
Expand All @@ -330,9 +331,9 @@ and a Twitter client using it:
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="rot13_transformer" class="Acme\Rot13Transformer">
<autowiring-type>Acme\TransformerInterface</autowiring-type>
</service>
<service id="rot13_transformer" class="Acme\Rot13Transformer" />

<service id="Acme\TransformerInterface" alias="rot13_transformer" />

<service id="twitter_client" class="Acme\TwitterClient" autowire="true" />

Expand All @@ -356,9 +357,8 @@ and a Twitter client using it:
use Symfony\Component\DependencyInjection\Definition;

// ...
$rot13Definition = new Definition(Rot13Transformer::class);
$rot13Definition->setAutowiringTypes(array(TransformerInterface::class));
$container->setDefinition('rot13_transformer', $rot13Definition);
$container->register('rot13_transformer', Rot13Transformer::class);
$container->setAlias(TransformerInterface::class, 'rot13_transformer')

$clientDefinition = new Definition(TwitterClient::class);
$clientDefinition->setAutowired(true);
Expand All @@ -382,10 +382,14 @@ to use which leads to errors like this:
[Symfony\Component\DependencyInjection\Exception\RuntimeException]
Unable to autowire argument of type "Acme\TransformerInterface" for the service "twitter_client".

Fortunately, the ``autowiring_types`` key is here to specify which implementation
to use by default. This key can take a list of types if necessary.
Fortunately, the FQCN alias is here to specify which implementation
to use by default.

.. versionadded:: 3.3
Using FQCN aliases to fix autowiring ambiguities is allowed since Symfony
3.3. Prior to version 3.3, you needed to use the ``autowiring_types`` key.

Thanks to this setting, the ``rot13_transformer`` service is automatically injected
Thanks to this alias, the ``rot13_transformer`` service is automatically injected
as an argument of the ``uppercase_transformer`` and ``twitter_client`` services. For
the ``uppercase_twitter_client``, a standard service definition is used to
inject the specific ``uppercase_transformer`` service.
Expand Down