Skip to content

Fixed code examples and format in the DI component documentation #2013

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 3 commits into from
Dec 26, 2012
Merged
Show file tree
Hide file tree
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
91 changes: 35 additions & 56 deletions components/dependency_injection/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;

Expand All @@ -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
{
Expand All @@ -66,14 +60,13 @@ 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')
$container
->register('mailer', 'Mailer')
->addArgument('sendmail');

This class is now much more flexible as you have separated the choice of
Expand All @@ -82,66 +75,56 @@ 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')
$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;
}

// ...
}

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;

$container = new ContainerBuilder();

$container->setParameter('mailer.transport', 'sendmail');
$container->register('mailer', 'Mailer')
$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;
}
Expand All @@ -150,26 +133,24 @@ 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;

$container = new ContainerBuilder();

$container->setParameter('mailer.transport', 'sendmail');
$container->register('mailer', 'Mailer')
$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;
Expand Down Expand Up @@ -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</components/config/introduction>`.

Loading an XML config file:

.. code-block:: php
Loading an XML config file::

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
Expand All @@ -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;
Expand Down Expand Up @@ -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
.. _Packagist: https://packagist.org/packages/symfony/dependency-injection
4 changes: 2 additions & 2 deletions components/dependency_injection/types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ the dependency::
{
protected $mailer;

public function __construct(Mailer $mailer)
public function __construct(\Mailer $mailer)
{
$this->mailer = $mailer;
}
Expand Down Expand Up @@ -101,7 +101,7 @@ accepts the dependency::
{
protected $mailer;

public function setMailer(Mailer $mailer)
public function setMailer(\Mailer $mailer)
{
$this->mailer = $mailer;
}
Expand Down