From 93e3f72e66ecdf29292783d543dcd3ef0ec4272e Mon Sep 17 00:00:00 2001 From: Wouter J Date: Sun, 9 Dec 2012 15:59:40 +0100 Subject: [PATCH 1/3] Fixed code examples in components/di/intro --- .../dependency_injection/introduction.rst | 97 ++++++++----------- 1 file changed, 38 insertions(+), 59 deletions(-) diff --git a/components/dependency_injection/introduction.rst b/components/dependency_injection/introduction.rst index 428fd531177..86d105f9b62 100644 --- a/components/dependency_injection/introduction.rst +++ b/components/dependency_injection/introduction.rst @@ -23,9 +23,7 @@ Basic Usage ----------- You might have a simple class like the following ``Mailer`` that -you want to make available as a service: - -.. code-block:: php +you want to make available as a service:: class Mailer { @@ -39,9 +37,7 @@ you want to make available as a service: // ... } -You can register this in the container as a service: - -.. code-block:: php +You can register this in the container as a service:: use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -50,9 +46,7 @@ You can register this in the container as a service: An improvement to the class to make it more flexible would be to allow the container to set the ``transport`` used. If you change the class -so this is passed into the constructor: - -.. code-block:: php +so this is passed into the constructor:: class Mailer { @@ -66,15 +60,14 @@ so this is passed into the constructor: // ... } -Then you can set the choice of transport in the container: - -.. code-block:: php +Then you can set the choice of transport in the container:: use Symfony\Component\DependencyInjection\ContainerBuilder; $container = new ContainerBuilder(); - $container->register('mailer', 'Mailer') - ->addArgument('sendmail'); + $container + ->register('mailer', 'Mailer') + ->addArgument('sendmail'); This class is now much more flexible as you have separated the choice of transport out of the implementation and into the container. @@ -82,31 +75,25 @@ transport out of the implementation and into the container. Which mail transport you have chosen may be something other services need to know about. You can avoid having to change it in multiple places by making it a parameter in the container and then referring to this parameter for the -``Mailer`` service's constructor argument: - - -.. code-block:: php +``Mailer`` service's constructor argument:: use Symfony\Component\DependencyInjection\ContainerBuilder; $container = new ContainerBuilder(); $container->setParameter('mailer.transport', 'sendmail'); - $container->register('mailer', 'Mailer') - ->addArgument('%mailer.transport%'); + $container + ->register('mailer', 'Mailer') + ->addArgument('%mailer.transport%'); Now that the ``mailer`` service is in the container you can inject it as a dependency of other classes. If you have a ``NewsletterManager`` class -like this: - -.. code-block:: php - - use Mailer; +like this:: class NewsletterManager { private $mailer; - public function __construct(Mailer $mailer) + public function __construct(\Mailer $mailer) { $this->mailer = $mailer; } @@ -114,9 +101,7 @@ like this: // ... } -Then you can register this as a service as well and pass the ``mailer`` service into it: - -.. code-block:: php +Then you can register this as a service as well and pass the ``mailer`` service into it:: use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; @@ -124,24 +109,22 @@ Then you can register this as a service as well and pass the ``mailer`` service $container = new ContainerBuilder(); $container->setParameter('mailer.transport', 'sendmail'); - $container->register('mailer', 'Mailer') - ->addArgument('%mailer.transport%'); + $container + ->register('mailer', 'Mailer') + ->addArgument('%mailer.transport%'); - $container->register('newsletter_manager', 'NewsletterManager') - ->addArgument(new Reference('mailer'); + $container + ->register('newsletter_manager', 'NewsletterManager') + ->addArgument(new Reference('mailer')); If the ``NewsletterManager`` did not require the ``Mailer`` and injecting -it was only optional then you could use setter injection instead: - -.. code-block:: php - - use Mailer; +it was only optional then you could use setter injection instead:: class NewsletterManager { private $mailer; - public function setMailer(Mailer $mailer) + public function setMailer(\Mailer $mailer) { $this->mailer = $mailer; } @@ -150,9 +133,7 @@ it was only optional then you could use setter injection instead: } You can now choose not to inject a ``Mailer`` into the ``NewsletterManager``. -If you do want to though then the container can call the setter method: - -.. code-block:: php +If you do want to though then the container can call the setter method:: use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; @@ -160,16 +141,16 @@ If you do want to though then the container can call the setter method: $container = new ContainerBuilder(); $container->setParameter('mailer.transport', 'sendmail'); - $container->register('mailer', 'Mailer') - ->addArgument('%mailer.transport%'); + $container + ->register('mailer', 'Mailer') + ->addArgument('%mailer.transport%'); - $container->register('newsletter_manager', 'NewsletterManager') - ->addMethodCall('setMailer', new Reference('mailer'); + $container + ->register('newsletter_manager', 'NewsletterManager') + ->addMethodCall('setMailer', new Reference('mailer')); You could then get your ``newsletter_manager`` service from the container -like this: - -.. code-block:: php +like this:: use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; @@ -201,9 +182,7 @@ Setting Up the Container with Configuration Files As well as setting up the services using PHP as above you can also use configuration files. To do this you also need to install :doc:`the Config Component`. -Loading an XML config file: - -.. code-block:: php +Loading an XML config file:: use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\Config\FileLocator; @@ -213,9 +192,7 @@ Loading an XML config file: $loader = new XmlFileLoader($container, new FileLocator(__DIR__)); $loader->load('services.xml'); -Loading a YAML config file: - -.. code-block:: php +Loading a YAML config file:: use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\Config\FileLocator; @@ -276,10 +253,12 @@ The ``newsletter_manager`` and ``mailer`` services can be set up using config fi // ... $container->setParameter('mailer.transport', 'sendmail'); - $container->register('mailer', 'Mailer') - ->addArgument('%mailer.transport%'); + $container + ->register('mailer', 'Mailer') + ->addArgument('%mailer.transport%'); - $container->register('newsletter_manager', 'NewsletterManager') - ->addMethodCall('setMailer', new Reference('mailer'); + $container + ->register('newsletter_manager', 'NewsletterManager') + ->addMethodCall('setMailer', new Reference('mailer')); .. _Packagist: https://packagist.org/packages/symfony/dependency-injection \ No newline at end of file From c0afd630b40fba4b2d02a93121992884df13b11e Mon Sep 17 00:00:00 2001 From: Wouter J Date: Sun, 9 Dec 2012 16:03:29 +0100 Subject: [PATCH 2/3] Fixed code examples in components/di/types --- components/dependency_injection/types.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/dependency_injection/types.rst b/components/dependency_injection/types.rst index 9cbfa03d88a..dd1aba49358 100644 --- a/components/dependency_injection/types.rst +++ b/components/dependency_injection/types.rst @@ -23,7 +23,7 @@ the dependency:: { protected $mailer; - public function __construct(Mailer $mailer) + public function __construct(\Mailer $mailer) { $this->mailer = $mailer; } @@ -101,7 +101,7 @@ accepts the dependency:: { protected $mailer; - public function setMailer(Mailer $mailer) + public function setMailer(\Mailer $mailer) { $this->mailer = $mailer; } From 2777deefe93b6453e7357335f2926bfd89725e81 Mon Sep 17 00:00:00 2001 From: Wouter J Date: Tue, 18 Dec 2012 14:44:17 +0100 Subject: [PATCH 3/3] Fixed method chaining indentation --- components/dependency_injection/introduction.rst | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/components/dependency_injection/introduction.rst b/components/dependency_injection/introduction.rst index 86d105f9b62..4ad1a23d593 100644 --- a/components/dependency_injection/introduction.rst +++ b/components/dependency_injection/introduction.rst @@ -67,7 +67,7 @@ Then you can set the choice of transport in the container:: $container = new ContainerBuilder(); $container ->register('mailer', 'Mailer') - ->addArgument('sendmail'); + ->addArgument('sendmail'); This class is now much more flexible as you have separated the choice of transport out of the implementation and into the container. @@ -83,7 +83,7 @@ it a parameter in the container and then referring to this parameter for the $container->setParameter('mailer.transport', 'sendmail'); $container ->register('mailer', 'Mailer') - ->addArgument('%mailer.transport%'); + ->addArgument('%mailer.transport%'); Now that the ``mailer`` service is in the container you can inject it as a dependency of other classes. If you have a ``NewsletterManager`` class @@ -111,11 +111,11 @@ Then you can register this as a service as well and pass the ``mailer`` service $container->setParameter('mailer.transport', 'sendmail'); $container ->register('mailer', 'Mailer') - ->addArgument('%mailer.transport%'); + ->addArgument('%mailer.transport%'); $container ->register('newsletter_manager', 'NewsletterManager') - ->addArgument(new Reference('mailer')); + ->addArgument(new Reference('mailer')); If the ``NewsletterManager`` did not require the ``Mailer`` and injecting it was only optional then you could use setter injection instead:: @@ -143,11 +143,11 @@ If you do want to though then the container can call the setter method:: $container->setParameter('mailer.transport', 'sendmail'); $container ->register('mailer', 'Mailer') - ->addArgument('%mailer.transport%'); + ->addArgument('%mailer.transport%'); $container ->register('newsletter_manager', 'NewsletterManager') - ->addMethodCall('setMailer', new Reference('mailer')); + ->addMethodCall('setMailer', new Reference('mailer')); You could then get your ``newsletter_manager`` service from the container like this:: @@ -261,4 +261,4 @@ The ``newsletter_manager`` and ``mailer`` services can be set up using config fi ->register('newsletter_manager', 'NewsletterManager') ->addMethodCall('setMailer', new Reference('mailer')); -.. _Packagist: https://packagist.org/packages/symfony/dependency-injection \ No newline at end of file +.. _Packagist: https://packagist.org/packages/symfony/dependency-injection