@@ -23,9 +23,7 @@ Basic Usage
23
23
-----------
24
24
25
25
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::
29
27
30
28
class Mailer
31
29
{
@@ -39,9 +37,7 @@ you want to make available as a service:
39
37
// ...
40
38
}
41
39
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::
45
41
46
42
use Symfony\Component\DependencyInjection\ContainerBuilder;
47
43
@@ -50,9 +46,7 @@ You can register this in the container as a service:
50
46
51
47
An improvement to the class to make it more flexible would be to allow
52
48
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::
56
50
57
51
class Mailer
58
52
{
@@ -66,14 +60,13 @@ so this is passed into the constructor:
66
60
// ...
67
61
}
68
62
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::
72
64
73
65
use Symfony\Component\DependencyInjection\ContainerBuilder;
74
66
75
67
$container = new ContainerBuilder();
76
- $container->register('mailer', 'Mailer')
68
+ $container
69
+ ->register('mailer', 'Mailer')
77
70
->addArgument('sendmail');
78
71
79
72
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.
82
75
Which mail transport you have chosen may be something other services need to
83
76
know about. You can avoid having to change it in multiple places by making
84
77
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::
89
79
90
80
use Symfony\Component\DependencyInjection\ContainerBuilder;
91
81
92
82
$container = new ContainerBuilder();
93
83
$container->setParameter('mailer.transport', 'sendmail');
94
- $container->register('mailer', 'Mailer')
84
+ $container
85
+ ->register('mailer', 'Mailer')
95
86
->addArgument('%mailer.transport%');
96
87
97
88
Now that the ``mailer `` service is in the container you can inject it as
98
89
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::
104
91
105
92
class NewsletterManager
106
93
{
107
94
private $mailer;
108
95
109
- public function __construct(Mailer $mailer)
96
+ public function __construct(\ Mailer $mailer)
110
97
{
111
98
$this->mailer = $mailer;
112
99
}
113
100
114
101
// ...
115
102
}
116
103
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::
120
105
121
106
use Symfony\Component\DependencyInjection\ContainerBuilder;
122
107
use Symfony\Component\DependencyInjection\Reference;
123
108
124
109
$container = new ContainerBuilder();
125
110
126
111
$container->setParameter('mailer.transport', 'sendmail');
127
- $container->register('mailer', 'Mailer')
112
+ $container
113
+ ->register('mailer', 'Mailer')
128
114
->addArgument('%mailer.transport%');
129
115
130
- $container->register('newsletter_manager', 'NewsletterManager')
131
- ->addArgument(new Reference('mailer');
116
+ $container
117
+ ->register('newsletter_manager', 'NewsletterManager')
118
+ ->addArgument(new Reference('mailer'));
132
119
133
120
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::
139
122
140
123
class NewsletterManager
141
124
{
142
125
private $mailer;
143
126
144
- public function setMailer(Mailer $mailer)
127
+ public function setMailer(\ Mailer $mailer)
145
128
{
146
129
$this->mailer = $mailer;
147
130
}
@@ -150,26 +133,24 @@ it was only optional then you could use setter injection instead:
150
133
}
151
134
152
135
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::
156
137
157
138
use Symfony\Component\DependencyInjection\ContainerBuilder;
158
139
use Symfony\Component\DependencyInjection\Reference;
159
140
160
141
$container = new ContainerBuilder();
161
142
162
143
$container->setParameter('mailer.transport', 'sendmail');
163
- $container->register('mailer', 'Mailer')
144
+ $container
145
+ ->register('mailer', 'Mailer')
164
146
->addArgument('%mailer.transport%');
165
147
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'));
168
151
169
152
You could then get your ``newsletter_manager `` service from the container
170
- like this:
171
-
172
- .. code-block :: php
153
+ like this::
173
154
174
155
use Symfony\Component\DependencyInjection\ContainerBuilder;
175
156
use Symfony\Component\DependencyInjection\Reference;
@@ -201,9 +182,7 @@ Setting Up the Container with Configuration Files
201
182
As well as setting up the services using PHP as above you can also use configuration
202
183
files. To do this you also need to install :doc: `the Config Component</components/config/introduction> `.
203
184
204
- Loading an XML config file:
205
-
206
- .. code-block :: php
185
+ Loading an XML config file::
207
186
208
187
use Symfony\Component\DependencyInjection\ContainerBuilder;
209
188
use Symfony\Component\Config\FileLocator;
@@ -213,9 +192,7 @@ Loading an XML config file:
213
192
$loader = new XmlFileLoader($container, new FileLocator(__DIR__));
214
193
$loader->load('services.xml');
215
194
216
- Loading a YAML config file:
217
-
218
- .. code-block :: php
195
+ Loading a YAML config file::
219
196
220
197
use Symfony\Component\DependencyInjection\ContainerBuilder;
221
198
use Symfony\Component\Config\FileLocator;
@@ -276,10 +253,12 @@ The ``newsletter_manager`` and ``mailer`` services can be set up using config fi
276
253
277
254
// ...
278
255
$container->setParameter('mailer.transport', 'sendmail');
279
- $container->register('mailer', 'Mailer')
280
- ->addArgument('%mailer.transport%');
256
+ $container
257
+ ->register('mailer', 'Mailer')
258
+ ->addArgument('%mailer.transport%');
281
259
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'));
284
263
285
- .. _Packagist : https://packagist.org/packages/symfony/dependency-injection
264
+ .. _Packagist : https://packagist.org/packages/symfony/dependency-injection
0 commit comments