@@ -10,12 +10,11 @@ Marking Services as Public / Private
10
10
------------------------------------
11
11
12
12
When defining services, you'll usually want to be able to access these definitions
13
- within your application code. These services are called ``public ``. For
14
- example, the ``doctrine `` service registered with the container when using
15
- the DoctrineBundle is a public service. This means that you can fetch it
16
- from the container using the ``get() `` method::
13
+ within your application code. These services are called *public *. For
14
+ example, the ``doctrine `` service is a public service. This means that you can
15
+ fetch it from the container using the ``get() `` method::
17
16
18
- $doctrine = $container->get('doctrine');
17
+ $doctrine = $container->get('doctrine');
19
18
20
19
In some cases, a service *only * exists to be injected into another service
21
20
and is *not * intended to be fetched directly from the container as shown
@@ -73,7 +72,8 @@ However, if a service has been marked as private, you can still alias it
73
72
74
73
.. note ::
75
74
76
- Services are by default public.
75
+ Services are by default public, but it's considered a good practice to mark
76
+ as much services private as possible.
77
77
78
78
Aliasing
79
79
--------
@@ -87,10 +87,11 @@ services.
87
87
.. code-block :: yaml
88
88
89
89
services :
90
- foo :
91
- class : Example\Foo
92
- bar :
93
- alias : foo
90
+ app.phpmailer :
91
+ class : AppBundle\Mail\PhpMailer
92
+
93
+ app.mailer :
94
+ alias : app.phpmailer
94
95
95
96
.. code-block :: xml
96
97
@@ -100,24 +101,24 @@ services.
100
101
xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
101
102
102
103
<services >
103
- <service id =" foo " class =" Example\Foo " />
104
+ <service id =" app.phpmailer " class =" AppBundle\PhpMailer " />
104
105
105
- <service id =" bar " alias =" foo " />
106
+ <service id =" app.mailer " alias =" app.phpmailer " />
106
107
</services >
107
108
</container >
108
109
109
110
.. code-block :: php
110
111
111
112
use Symfony\Component\DependencyInjection\Definition;
112
113
113
- $container->setDefinition('foo ', new Definition('Example\Foo '));
114
+ $container->setDefinition('app.phpmailer ', new Definition('AppBundle\PhpMailer '));
114
115
115
- $containerBuilder->setAlias('bar ', 'foo ');
116
+ $containerBuilder->setAlias('app.mailer ', 'app.phpmailer ');
116
117
117
- This means that when using the container directly, you can access the `` foo ``
118
- service by asking for the ``bar `` service like this::
118
+ This means that when using the container directly, you can access the
119
+ `` app.phpmailer `` service by asking for the ``app.mailer `` service like this::
119
120
120
- $container->get('bar '); // Would return the foo service
121
+ $container->get('app.mailer '); // Would return a PhpMailer instance
121
122
122
123
.. tip ::
123
124
@@ -126,95 +127,6 @@ service by asking for the ``bar`` service like this::
126
127
.. code-block :: yaml
127
128
128
129
services :
129
- foo :
130
- class : Example\Foo
131
- bar : ' @foo'
132
-
133
- Decorating Services
134
- -------------------
135
-
136
- When overriding an existing definition, the old service is lost:
137
-
138
- .. code-block :: php
139
-
140
- $container->register('foo', 'FooService');
141
-
142
- // this is going to replace the old definition with the new one
143
- // old definition is lost
144
- $container->register('foo', 'CustomFooService');
145
-
146
- Most of the time, that's exactly what you want to do. But sometimes,
147
- you might want to decorate the old one instead. In this case, the
148
- old service should be kept around to be able to reference it in the
149
- new one. This configuration replaces ``foo `` with a new one, but keeps
150
- a reference of the old one as ``bar.inner ``:
151
-
152
- .. configuration-block ::
153
-
154
- .. code-block :: yaml
155
-
156
- bar :
157
- public : false
158
- class : stdClass
159
- decorates : foo
160
- arguments : ["@bar.inner"]
161
-
162
- .. code-block :: xml
163
-
164
- <service id =" bar" class =" stdClass" decorates =" foo" public =" false" >
165
- <argument type =" service" id =" bar.inner" />
166
- </service >
167
-
168
- .. code-block :: php
169
-
170
- use Symfony\Component\DependencyInjection\Reference;
171
-
172
- $container->register('bar', 'stdClass')
173
- ->addArgument(new Reference('bar.inner'))
174
- ->setPublic(false)
175
- ->setDecoratedService('foo');
176
-
177
- Here is what's going on here: the ``setDecoratedService() `` method tells
178
- the container that the ``bar `` service should replace the ``foo `` service,
179
- renaming ``foo `` to ``bar.inner ``.
180
- By convention, the old ``foo `` service is going to be renamed ``bar.inner ``,
181
- so you can inject it into your new service.
182
-
183
- .. note ::
184
- The generated inner id is based on the id of the decorator service
185
- (``bar `` here), not of the decorated service (``foo `` here). This is
186
- mandatory to allow several decorators on the same service (they need to have
187
- different generated inner ids).
188
-
189
- Most of the time, the decorator should be declared private, as you will not
190
- need to retrieve it as ``bar `` from the container. The visibility of the
191
- decorated ``foo `` service (which is an alias for ``bar ``) will still be the
192
- same as the original ``foo `` visibility.
193
-
194
- You can change the inner service name if you want to:
195
-
196
- .. configuration-block ::
197
-
198
- .. code-block :: yaml
199
-
200
- bar :
201
- class : stdClass
202
- public : false
203
- decorates : foo
204
- decoration_inner_name : bar.wooz
205
- arguments : ["@bar.wooz"]
206
-
207
- .. code-block :: xml
208
-
209
- <service id =" bar" class =" stdClass" decorates =" foo" decoration-inner-name =" bar.wooz" public =" false" >
210
- <argument type =" service" id =" bar.wooz" />
211
- </service >
212
-
213
- .. code-block :: php
214
-
215
- use Symfony\Component\DependencyInjection\Reference;
130
+ # ...
216
131
217
- $container->register('bar', 'stdClass')
218
- ->addArgument(new Reference('bar.wooz'))
219
- ->setPublic(false)
220
- ->setDecoratedService('foo', 'bar.wooz');
132
+ app.mailer : ' @app.phpmailer'
0 commit comments