@@ -18,8 +18,7 @@ with `ROT13`_ (a special case of the Caesar cipher).
1818
1919Start by creating a ROT13 transformer class::
2020
21- // src/AppBundle/Rot13Transformer.php
22- namespace AppBundle;
21+ namespace Acme;
2322
2423 class Rot13Transformer
2524 {
@@ -31,8 +30,7 @@ Start by creating a ROT13 transformer class::
3130
3231And now a Twitter client using this transformer::
3332
34- // src/AppBundle/TwitterClient.php
35- namespace AppBundle;
33+ namespace Acme;
3634
3735 class TwitterClient
3836 {
@@ -59,22 +57,20 @@ service is marked as autowired:
5957
6058 .. code-block :: yaml
6159
62- # app/config/services.yml
6360 services :
6461 twitter_client :
65- class : AppBundle \TwitterClient
62+ class : Acme \TwitterClient
6663 autowire : true
6764
6865 .. code-block :: xml
6966
70- <!-- app/config/services.xml -->
7167 <?xml version =" 1.0" encoding =" UTF-8" ?>
7268 <container xmlns =" http://symfony.com/schema/dic/services"
7369 xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
7470 xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
7571
7672 <services >
77- <service id =" twitter_client" class =" AppBundle \TwitterClient" autowire =" true" />
73+ <service id =" twitter_client" class =" Acme \TwitterClient" autowire =" true" />
7874 </services >
7975 </container >
8076
@@ -83,7 +79,7 @@ service is marked as autowired:
8379 use Symfony\Component\DependencyInjection\Definition;
8480
8581 // ...
86- $definition = new Definition('AppBundle \TwitterClient');
82+ $definition = new Definition('Acme \TwitterClient');
8783 $definition->setAutowired(true);
8884
8985 $container->setDefinition('twitter_client', $definition);
@@ -106,8 +102,7 @@ and edit related service definitions.
106102
107103Here is a typical controller using the ``twitter_client `` service::
108104
109- // src/AppBundle/Controller/DefaultController.php
110- namespace AppBundle\Controller;
105+ namespace Acme\Controller;
111106
112107 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
113108 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
@@ -159,8 +154,7 @@ if necessary. It also allows to use other transformers.
159154
160155Let's introduce a ``TransformerInterface ``::
161156
162- // src/AppBundle/TransformerInterface.php
163- namespace AppBundle;
157+ namespace Acme;
164158
165159 interface TransformerInterface
166160 {
@@ -197,26 +191,24 @@ subsystem isn't able to find itself the interface implementation to register:
197191
198192 .. code-block :: yaml
199193
200- # app/config/services.yml
201194 services :
202195 rot13_transformer :
203- class : AppBundle \Rot13Transformer
196+ class : Acme \Rot13Transformer
204197
205198 twitter_client :
206- class : AppBundle \TwitterClient
199+ class : Acme \TwitterClient
207200 autowire : true
208201
209202 .. code-block :: xml
210203
211- <!-- app/config/services.xml -->
212204 <?xml version =" 1.0" encoding =" UTF-8" ?>
213205 <container xmlns =" http://symfony.com/schema/dic/services"
214206 xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
215207 xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
216208
217209 <services >
218- <service id =" rot13_transformer" class =" AppBundle \Rot13Transformer" />
219- <service id =" twitter_client" class =" AppBundle \TwitterClient" autowire =" true" />
210+ <service id =" rot13_transformer" class =" Acme \Rot13Transformer" />
211+ <service id =" twitter_client" class =" Acme \TwitterClient" autowire =" true" />
220212 </services >
221213 </container >
222214
@@ -225,10 +217,10 @@ subsystem isn't able to find itself the interface implementation to register:
225217 use Symfony\Component\DependencyInjection\Definition;
226218
227219 // ...
228- $definition1 = new Definition('AppBundle \Rot13Transformer');
220+ $definition1 = new Definition('Acme \Rot13Transformer');
229221 $container->setDefinition('rot13_transformer', $definition1);
230222
231- $definition2 = new Definition('AppBundle \TwitterClient');
223+ $definition2 = new Definition('Acme \TwitterClient');
232224 $definition2->setAutowired(true);
233225 $container->setDefinition('twitter_client', $definition2);
234226
@@ -244,8 +236,7 @@ Last but not least, the autowiring feature allows to specify the default impleme
244236of a given type. Let's introduce a new implementation of the ``TransformerInterface ``
245237returning the result of the ROT13 transformation uppercased::
246238
247- // src/AppBundle/UppercaseRot13Transformer.php
248- namespace AppBundle;
239+ namespace Acme;
249240
250241 class UppercaseTransformer implements TransformerInterface
251242 {
@@ -267,8 +258,7 @@ This class is intended to decorate the any transformer and return its value uppe
267258We can now refactor the controller to add another endpoint leveraging this new
268259transformer::
269260
270- // src/AppBundle/Controller/DefaultController.php
271- namespace AppBundle\Controller;
261+ namespace Acme\Controller;
272262
273263 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
274264 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
@@ -320,39 +310,37 @@ and a Twitter client using it:
320310
321311 .. code-block :: yaml
322312
323- # app/config/services.yml
324313 services :
325314 rot13_transformer :
326- class : AppBundle \Rot13Transformer
327- autowiring_types : AppBundle \TransformerInterface
315+ class : Acme \Rot13Transformer
316+ autowiring_types : Acme \TransformerInterface
328317
329318 twitter_client :
330- class : AppBundle \TwitterClient
319+ class : Acme \TwitterClient
331320 autowire : true
332321
333322 uppercase_rot13_transformer :
334- class : AppBundle \UppercaseRot13Transformer
323+ class : Acme \UppercaseRot13Transformer
335324 autowire : true
336325
337326 uppercase_twitter_client :
338- class : AppBundle \TwitterClient
327+ class : Acme \TwitterClient
339328 arguments : ['@uppercase_rot13_transformer']
340329
341330 .. code-block :: xml
342331
343- <!-- app/config/services.xml -->
344332 <?xml version =" 1.0" encoding =" UTF-8" ?>
345333 <container xmlns =" http://symfony.com/schema/dic/services"
346334 xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
347335 xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
348336
349337 <services >
350- <service id =" rot13_transformer" class =" AppBundle \Rot13Transformer" >
351- <autowiring-type >AppBundle \TransformerInterface</autowiring-type >
338+ <service id =" rot13_transformer" class =" Acme \Rot13Transformer" >
339+ <autowiring-type >Acme \TransformerInterface</autowiring-type >
352340 </service >
353- <service id =" twitter_client" class =" AppBundle \TwitterClient" autowire =" true" />
354- <service id =" uppercase_rot13_transformer" class =" AppBundle \UppercaseRot13Transformer" autowire =" true" />
355- <service id =" uppercase_twitter_client" class =" AppBundle \TwitterClient" >
341+ <service id =" twitter_client" class =" Acme \TwitterClient" autowire =" true" />
342+ <service id =" uppercase_rot13_transformer" class =" Acme \UppercaseRot13Transformer" autowire =" true" />
343+ <service id =" uppercase_twitter_client" class =" Acme \TwitterClient" >
356344 <argument type =" service" id =" uppercase_rot13_transformer" />
357345 </service >
358346 </services >
@@ -364,19 +352,19 @@ and a Twitter client using it:
364352 use Symfony\Component\DependencyInjection\Definition;
365353
366354 // ...
367- $definition1 = new Definition('AppBundle \Rot13Transformer');
368- $definition1->setAutowiringTypes(array('AppBundle \TransformerInterface'));
355+ $definition1 = new Definition('Acme \Rot13Transformer');
356+ $definition1->setAutowiringTypes(array('Acme \TransformerInterface'));
369357 $container->setDefinition('rot13_transformer', $definition1);
370358
371- $definition2 = new Definition('AppBundle \TwitterClient');
359+ $definition2 = new Definition('Acme \TwitterClient');
372360 $definition2->setAutowired(true);
373361 $container->setDefinition('twitter_client', $definition2);
374362
375- $definition3 = new Definition('AppBundle \UppercaseRot13Transformer');
363+ $definition3 = new Definition('Acme \UppercaseRot13Transformer');
376364 $definition3->setAutowired(true);
377365 $container->setDefinition('uppercase_rot13_transformer', $definition3);
378366
379- $definition4 = new Definition('AppBundle \TwitterClient');
367+ $definition4 = new Definition('Acme \TwitterClient');
380368 $definition4->addArgument(new Reference('uppercase_rot13_transformer'));
381369 $container->setDefinition('uppercase_twitter_client', $definition4);
382370
@@ -387,7 +375,7 @@ to use which leads to errors like this:
387375.. code-block :: text
388376
389377 [Symfony\Component\DependencyInjection\Exception\RuntimeException]
390- Unable to autowire argument of type "AppBundle \TransformerInterface" for the service "twitter_client".
378+ Unable to autowire argument of type "Acme \TransformerInterface" for the service "twitter_client".
391379
392380 Fortunately, the ``autowiring_types `` key is here to specify which implementation
393381to use by default. This key can take a list of types if necessary.
0 commit comments