@@ -86,13 +86,26 @@ autowired:
86
86
87
87
The autowiring subsystem will detect the dependencies of the ``TwitterClient ``
88
88
class by parsing its constructor. For instance it will find here an instance of
89
- a ``Rot13Transformer `` as dependency. If an existing service definition (and only
90
- one – see below) is of the required type, this service will be injected. If it's
91
- not the case (like in this example), the subsystem is smart enough to automatically
92
- register a private service for the ``Rot13Transformer `` class and set it as first
93
- argument of the ``twitter_client `` service. Again, it can work only if there is one
94
- class of the given type. If there are several classes of the same type, you must
95
- use an explicit service definition or register a default implementation.
89
+ a ``Rot13Transformer `` as dependency.
90
+
91
+ The subsystem will first try to find a service whose id is the FQCN (fully-qualified
92
+ class name) of the type hint, so here it'll search for a service named
93
+ ``AppBundle\Rot13Transformer ``.
94
+
95
+ In case this service does not exist, the subsystem will detect the types of all
96
+ services and check if one - and only one - implements the required type, and
97
+ inject it if it's the case. If there are several services of the same type, an
98
+ exception will be thrown. You'll have to use an explicit service definition or
99
+ register a default implementation by creating a service or an alias whose id is
100
+ the FQCN of the required type.
101
+ Note that this check is deprecated and will be removed in 4.0. The subsystem
102
+ will directly pass to the last option it has.
103
+
104
+ At last, if no service implements the required type, as it's the case here, the
105
+ subsystem is, as long as it's a concrete class, smart enough to automatically
106
+ register a private service for it.
107
+ Here it'll register a private service for the ``Rot13Transformer `` class and set
108
+ it as first argument of the ``twitter_client `` service.
96
109
97
110
As you can see, the autowiring feature drastically reduces the amount of configuration
98
111
required to define a service. No more arguments section! It also makes it easy
@@ -185,7 +198,9 @@ And update ``TwitterClient`` to depend of this new interface::
185
198
}
186
199
187
200
Finally the service definition must be updated because, obviously, the autowiring
188
- subsystem isn't able to find itself the interface implementation to register:
201
+ subsystem isn't able to find itself the interface implementation to register.
202
+ You have to indicate which service must be injected for your interface when
203
+ using autowiring:
189
204
190
205
.. configuration-block ::
191
206
@@ -194,6 +209,10 @@ subsystem isn't able to find itself the interface implementation to register:
194
209
services :
195
210
AppBundle\Rot13Transformer : ~
196
211
212
+ # the ``AppBundle\Rot13Transformer`` service will be injected when
213
+ # a ``AppBundle\TransformerInterface`` type-hint is detected
214
+ AppBundle\TransformerInterface : ' @AppBundle\Rot13Transformer'
215
+
197
216
AppBundle\TwitterClient :
198
217
autowire : true
199
218
@@ -207,6 +226,8 @@ subsystem isn't able to find itself the interface implementation to register:
207
226
<services >
208
227
<service id =" AppBundle\Rot13Transformer" />
209
228
229
+ <service id =" AppBundle\TransformerInterface" alias =" AppBundle\Rot13Transformer" />
230
+
210
231
<service id =" AppBundle\TwitterClient" autowire =" true" />
211
232
</services >
212
233
</container >
@@ -218,21 +239,26 @@ subsystem isn't able to find itself the interface implementation to register:
218
239
219
240
// ...
220
241
$container->register(Rot13Transformer::class);
242
+ $container->setAlias(TransformerInterface::class, Rot13Transformer::class);
243
+
221
244
$container->autowire(TwitterClient::class);
222
245
223
- The autowiring subsystem detects that the ``AppBundle\Rot13Transformer `` service
224
- implements the ``TransformerInterface `` and injects it automatically. Even when
225
- using interfaces (and you should), building the service graph and refactoring
226
- the project is easier than with standard definitions.
246
+ The autowiring subsystem knows that the ``AppBundle\Rot13Transformer `` service
247
+ must be injected when dealing with the ``TransformerInterface `` and injects it
248
+ automatically. Even when using interfaces (and you should), building the service
249
+ graph and refactoring the project is easier than with standard definitions.
227
250
228
251
.. _service-autowiring-alias :
229
252
230
253
Dealing with Multiple Implementations of the Same Type
231
254
------------------------------------------------------
232
255
233
- Last but not least, the autowiring feature allows to specify the default implementation
234
- of a given type. Let's introduce a new implementation of the ``TransformerInterface ``
235
- returning the result of the ROT13 transformation uppercased::
256
+ When you defined aliases whose id were FQCN earlier, you told the autowiring
257
+ subsystem which service was the default implementation for those FQCN.
258
+ So if you have several services implementing the same type, you can decide which
259
+ one the subsystem should use. Let's introduce a new implementation of the
260
+ ``TransformerInterface `` returning the result of the ROT13 transformation
261
+ uppercased::
236
262
237
263
namespace AppBundle;
238
264
@@ -274,6 +300,9 @@ transformer::
274
300
*/
275
301
public function tweetAction(Request $request, TwitterClient $twitterClient)
276
302
{
303
+ // Here the client is directly injected because it's the default
304
+ // implementation
305
+
277
306
return $this->tweet($request, $twitterClient);
278
307
}
279
308
@@ -315,6 +344,9 @@ and a Twitter client using it:
315
344
services :
316
345
AppBundle\Rot13Transformer : ~
317
346
347
+ # the ``AppBundle\Rot13Transformer`` service will *always* be used
348
+ # when ``AppBundle\TransformerInterface`` is detected by the
349
+ # autowiring subsystem
318
350
AppBundle\TransformerInterface : ' @AppBundle\Rot13Transformer'
319
351
320
352
AppBundle\TwitterClient :
@@ -359,7 +391,7 @@ and a Twitter client using it:
359
391
360
392
// ...
361
393
$container->register(Rot13Transformer::class);
362
- $container->setAlias(TransformerInterface::class, Rot13Transformer::class)
394
+ $container->setAlias(TransformerInterface::class, Rot13Transformer::class);
363
395
364
396
$container->autowire(TwitterClient::class);
365
397
$container->autowire(UppercaseTransformer::class);
0 commit comments