From d1e3024e12d0cf339241d288460b2557c51b9a50 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Wed, 10 Feb 2016 19:45:51 +0100 Subject: [PATCH] [#6032] some tweaks for the autowiring feature --- .../dependency_injection/autowiring.rst | 48 +++++++++++-------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/components/dependency_injection/autowiring.rst b/components/dependency_injection/autowiring.rst index 86287d5d5a7..46196cc846d 100644 --- a/components/dependency_injection/autowiring.rst +++ b/components/dependency_injection/autowiring.rst @@ -4,13 +4,17 @@ Defining Services Dependencies Automatically ============================================ +.. versionadded:: 2.8 + Support for autowiring services was introduced in Symfony 2.8. + Autowiring allows to register services in the container with minimal configuration. -It is useful in the field of `Rapid Application Development`_, when designing prototypes -in early stages of large projects. It makes it easy to register a service graph -and eases refactoring. +It automatically resolves the service dependencies based on the constructor's +typehint which is useful in the field of `Rapid Application Development`_, +when designing prototypes in early stages of large projects. It makes it easy +to register a service graph and eases refactoring. -Imagine you're building an API to publish statuses on a Twitter feed, obfuscated -with `ROT13`.. (a special case of the Caesar cipher). +Imagine you're building an API to publish statuses on a Twitter feed, which +has to be obfuscated with ``ROT13`` (a special case of the Caesar cipher). Start by creating a ROT13 transformer class:: @@ -47,8 +51,9 @@ And now a Twitter client using this transformer:: } } -The Dependency Injection Component will be able to automatically register the dependencies -of this ``TwitterClient`` class by marking the ``twitter_client`` service as autowired: +The DependencyInjection component will be able to automatically register +the dependencies of this ``TwitterClient`` class when the ``twitter_client`` +service is marked as autowired: .. configuration-block:: @@ -133,9 +138,11 @@ Here is a typical controller using the ``twitter_client`` service:: } } -You can give a try to the API with ``curl``:: +You can give the API a try using ``curl``: + +.. code-block:: bash - curl -d "user=kevin&key=ABCD&status=Hello" http://localhost:8000/tweet + $ curl -d "user=kevin&key=ABCD&status=Hello" http://localhost:8000/tweet It should return ``OK``. @@ -316,20 +323,20 @@ and a Twitter client using it:: # app/config/services.yml services: rot13_transformer: - class: 'AppBundle\Rot13Transformer' - autowiring_types: 'AppBundle\TransformerInterface' + class: AppBundle\Rot13Transformer + autowiring_types: AppBundle\TransformerInterface twitter_client: - class: 'AppBundle\TwitterClient' + class: AppBundle\TwitterClient autowire: true uppercase_rot13_transformer: - class: 'AppBundle\UppercaseRot13Transformer' + class: AppBundle\UppercaseRot13Transformer autowire: true uppercase_twitter_client: - class: 'AppBundle\TwitterClient' - arguments: [ '@uppercase_rot13_transformer' ] + class: AppBundle\TwitterClient + arguments: ['@uppercase_rot13_transformer'] .. code-block:: xml @@ -373,16 +380,17 @@ and a Twitter client using it:: $definition4->addArgument(new Reference('uppercase_rot13_transformer')); $container->setDefinition('uppercase_twitter_client', $definition4); -It deserves some explanations. We now have 2 services implementing the ``TransformerInterface``. -The autowiring subsystem cannot guess which one to use, this leads to errors -like:: +This deserves some explanations. You now have two services implementing the +``TransformerInterface``. The autowiring subsystem cannot guess which one +to use which leads to errors like this: + +.. code-block:: text [Symfony\Component\DependencyInjection\Exception\RuntimeException] Unable to autowire argument of type "AppBundle\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 (using a YAML -array). +to use by default. This key can take a list of types if necessary. Thanks to this setting, the ``rot13_transformer`` service is automatically injected as an argument of the ``uppercase_rot13_transformer`` and ``twitter_client`` services. For