Skip to content

Commit f2dbd6b

Browse files
committed
Merge pull request #2013 from WouterJ/patch-22
Fixed code examples and format in the DI component documentation
2 parents a67d3aa + 2777dee commit f2dbd6b

File tree

2 files changed

+37
-58
lines changed

2 files changed

+37
-58
lines changed

components/dependency_injection/introduction.rst

Lines changed: 35 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ Basic Usage
2323
-----------
2424

2525
You might have a simple class like the following ``Mailer`` that
26-
you want to make available as a service:
27-
28-
.. code-block:: php
26+
you want to make available as a service::
2927

3028
class Mailer
3129
{
@@ -39,9 +37,7 @@ you want to make available as a service:
3937
// ...
4038
}
4139

42-
You can register this in the container as a service:
43-
44-
.. code-block:: php
40+
You can register this in the container as a service::
4541

4642
use Symfony\Component\DependencyInjection\ContainerBuilder;
4743

@@ -50,9 +46,7 @@ You can register this in the container as a service:
5046

5147
An improvement to the class to make it more flexible would be to allow
5248
the container to set the ``transport`` used. If you change the class
53-
so this is passed into the constructor:
54-
55-
.. code-block:: php
49+
so this is passed into the constructor::
5650

5751
class Mailer
5852
{
@@ -66,14 +60,13 @@ so this is passed into the constructor:
6660
// ...
6761
}
6862

69-
Then you can set the choice of transport in the container:
70-
71-
.. code-block:: php
63+
Then you can set the choice of transport in the container::
7264

7365
use Symfony\Component\DependencyInjection\ContainerBuilder;
7466

7567
$container = new ContainerBuilder();
76-
$container->register('mailer', 'Mailer')
68+
$container
69+
->register('mailer', 'Mailer')
7770
->addArgument('sendmail');
7871

7972
This class is now much more flexible as you have separated the choice of
@@ -82,66 +75,56 @@ transport out of the implementation and into the container.
8275
Which mail transport you have chosen may be something other services need to
8376
know about. You can avoid having to change it in multiple places by making
8477
it a parameter in the container and then referring to this parameter for the
85-
``Mailer`` service's constructor argument:
86-
87-
88-
.. code-block:: php
78+
``Mailer`` service's constructor argument::
8979

9080
use Symfony\Component\DependencyInjection\ContainerBuilder;
9181

9282
$container = new ContainerBuilder();
9383
$container->setParameter('mailer.transport', 'sendmail');
94-
$container->register('mailer', 'Mailer')
84+
$container
85+
->register('mailer', 'Mailer')
9586
->addArgument('%mailer.transport%');
9687

9788
Now that the ``mailer`` service is in the container you can inject it as
9889
a dependency of other classes. If you have a ``NewsletterManager`` class
99-
like this:
100-
101-
.. code-block:: php
102-
103-
use Mailer;
90+
like this::
10491

10592
class NewsletterManager
10693
{
10794
private $mailer;
10895

109-
public function __construct(Mailer $mailer)
96+
public function __construct(\Mailer $mailer)
11097
{
11198
$this->mailer = $mailer;
11299
}
113100

114101
// ...
115102
}
116103

117-
Then you can register this as a service as well and pass the ``mailer`` service into it:
118-
119-
.. code-block:: php
104+
Then you can register this as a service as well and pass the ``mailer`` service into it::
120105

121106
use Symfony\Component\DependencyInjection\ContainerBuilder;
122107
use Symfony\Component\DependencyInjection\Reference;
123108

124109
$container = new ContainerBuilder();
125110

126111
$container->setParameter('mailer.transport', 'sendmail');
127-
$container->register('mailer', 'Mailer')
112+
$container
113+
->register('mailer', 'Mailer')
128114
->addArgument('%mailer.transport%');
129115

130-
$container->register('newsletter_manager', 'NewsletterManager')
131-
->addArgument(new Reference('mailer');
116+
$container
117+
->register('newsletter_manager', 'NewsletterManager')
118+
->addArgument(new Reference('mailer'));
132119

133120
If the ``NewsletterManager`` did not require the ``Mailer`` and injecting
134-
it was only optional then you could use setter injection instead:
135-
136-
.. code-block:: php
137-
138-
use Mailer;
121+
it was only optional then you could use setter injection instead::
139122

140123
class NewsletterManager
141124
{
142125
private $mailer;
143126

144-
public function setMailer(Mailer $mailer)
127+
public function setMailer(\Mailer $mailer)
145128
{
146129
$this->mailer = $mailer;
147130
}
@@ -150,26 +133,24 @@ it was only optional then you could use setter injection instead:
150133
}
151134

152135
You can now choose not to inject a ``Mailer`` into the ``NewsletterManager``.
153-
If you do want to though then the container can call the setter method:
154-
155-
.. code-block:: php
136+
If you do want to though then the container can call the setter method::
156137

157138
use Symfony\Component\DependencyInjection\ContainerBuilder;
158139
use Symfony\Component\DependencyInjection\Reference;
159140

160141
$container = new ContainerBuilder();
161142

162143
$container->setParameter('mailer.transport', 'sendmail');
163-
$container->register('mailer', 'Mailer')
144+
$container
145+
->register('mailer', 'Mailer')
164146
->addArgument('%mailer.transport%');
165147

166-
$container->register('newsletter_manager', 'NewsletterManager')
167-
->addMethodCall('setMailer', new Reference('mailer');
148+
$container
149+
->register('newsletter_manager', 'NewsletterManager')
150+
->addMethodCall('setMailer', new Reference('mailer'));
168151

169152
You could then get your ``newsletter_manager`` service from the container
170-
like this:
171-
172-
.. code-block:: php
153+
like this::
173154

174155
use Symfony\Component\DependencyInjection\ContainerBuilder;
175156
use Symfony\Component\DependencyInjection\Reference;
@@ -201,9 +182,7 @@ Setting Up the Container with Configuration Files
201182
As well as setting up the services using PHP as above you can also use configuration
202183
files. To do this you also need to install :doc:`the Config Component</components/config/introduction>`.
203184

204-
Loading an XML config file:
205-
206-
.. code-block:: php
185+
Loading an XML config file::
207186

208187
use Symfony\Component\DependencyInjection\ContainerBuilder;
209188
use Symfony\Component\Config\FileLocator;
@@ -213,9 +192,7 @@ Loading an XML config file:
213192
$loader = new XmlFileLoader($container, new FileLocator(__DIR__));
214193
$loader->load('services.xml');
215194

216-
Loading a YAML config file:
217-
218-
.. code-block:: php
195+
Loading a YAML config file::
219196

220197
use Symfony\Component\DependencyInjection\ContainerBuilder;
221198
use Symfony\Component\Config\FileLocator;
@@ -276,10 +253,12 @@ The ``newsletter_manager`` and ``mailer`` services can be set up using config fi
276253
277254
// ...
278255
$container->setParameter('mailer.transport', 'sendmail');
279-
$container->register('mailer', 'Mailer')
280-
->addArgument('%mailer.transport%');
256+
$container
257+
->register('mailer', 'Mailer')
258+
->addArgument('%mailer.transport%');
281259
282-
$container->register('newsletter_manager', 'NewsletterManager')
283-
->addMethodCall('setMailer', new Reference('mailer');
260+
$container
261+
->register('newsletter_manager', 'NewsletterManager')
262+
->addMethodCall('setMailer', new Reference('mailer'));
284263
285-
.. _Packagist: https://packagist.org/packages/symfony/dependency-injection
264+
.. _Packagist: https://packagist.org/packages/symfony/dependency-injection

components/dependency_injection/types.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ the dependency::
2323
{
2424
protected $mailer;
2525

26-
public function __construct(Mailer $mailer)
26+
public function __construct(\Mailer $mailer)
2727
{
2828
$this->mailer = $mailer;
2929
}
@@ -101,7 +101,7 @@ accepts the dependency::
101101
{
102102
protected $mailer;
103103

104-
public function setMailer(Mailer $mailer)
104+
public function setMailer(\Mailer $mailer)
105105
{
106106
$this->mailer = $mailer;
107107
}

0 commit comments

Comments
 (0)