@@ -11,41 +11,43 @@ the original service is lost:
11
11
12
12
.. code-block :: yaml
13
13
14
+ # config/services.yaml
14
15
services :
15
- app.mailer :
16
- class : AppBundle\Mailer
16
+ AppBundle\Mailer : ~
17
17
18
- # this replaces the old app.mailer definition with the new one, the
18
+ # this replaces the old AppBundle\Mailer definition with the new one, the
19
19
# old definition is lost
20
- app.mailer :
20
+ AppBundle\Mailer :
21
21
class : AppBundle\DecoratingMailer
22
22
23
23
.. code-block :: xml
24
24
25
+ <!-- config/services.xml -->
25
26
<?xml version =" 1.0" encoding =" UTF-8" ?>
26
27
<container xmlns =" http://symfony.com/schema/dic/services"
27
28
xmlns : xsd =" http://www.w3.org/2001/XMLSchema-instance"
28
29
xsd : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
29
30
30
31
<services >
31
- <service id =" app.mailer " class = " AppBundle\Mailer" />
32
+ <service id =" AppBundle\Mailer" />
32
33
33
- <!-- this replaces the old app.mailer definition with the new
34
+ <!-- this replaces the old AppBundle\Mailer definition with the new
34
35
one, the old definition is lost -->
35
- <service id =" app.mailer " class =" AppBundle\DecoratingMailer" />
36
+ <service id =" AppBundle\Mailer " class =" AppBundle\DecoratingMailer" />
36
37
</services >
37
38
</container >
38
39
39
40
.. code-block :: php
40
41
42
+ // config/services.php
41
43
use AppBundle\Mailer;
42
44
use AppBundle\DecoratingMailer;
43
45
44
- $container->register('app.mailer', Mailer::class);
46
+ $container->register(Mailer::class);
45
47
46
- // this replaces the old app.mailer definition with the new one, the
48
+ // this replaces the old AppBundle\Mailer definition with the new one, the
47
49
// old definition is lost
48
- $container->register('app.mailer' , DecoratingMailer::class);
50
+ $container->register(Mailer::class , DecoratingMailer::class);
49
51
50
52
Most of the time, that's exactly what you want to do. But sometimes,
51
53
you might want to decorate the old service instead and keep the old service so
@@ -55,86 +57,88 @@ that you can reference it:
55
57
56
58
.. code-block :: yaml
57
59
60
+ # config/services.yaml
58
61
services :
59
- app.mailer :
60
- class : AppBundle\Mailer
62
+ AppBundle\Mailer : ~
61
63
62
- app.decorating_mailer :
63
- class : AppBundle\DecoratingMailer
64
- # overrides the app.mailer service
65
- # but that service is still available as app.decorating_mailer.inner
66
- decorates : app.mailer
64
+ AppBundle\DecoratingMailer :
65
+ # overrides the AppBundle\Mailer service
66
+ # but that service is still available as AppBundle\Mailer.inner
67
+ decorates : AppBundle\Mailer
67
68
68
69
# pass the old service as an argument
69
- arguments : ['@app.decorating_mailer .inner']
70
+ arguments : ['@AppBundle\DecoratingMailer .inner']
70
71
71
- # private, because usually you do not need to fetch app.decorating_mailer directly
72
+ # private, because usually you do not need to fetch AppBundle\DecoratingMailer directly
72
73
public : false
73
74
74
75
.. code-block :: xml
75
76
77
+ <!-- config/services.xml -->
76
78
<?xml version =" 1.0" encoding =" UTF-8" ?>
77
79
<container xmlns =" http://symfony.com/schema/dic/services"
78
80
xmlns : xsd =" http://www.w3.org/2001/XMLSchema-instance"
79
81
xsd : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
80
82
81
83
<services >
82
- <service id =" app.mailer " class = " AppBundle\Mailer" />
84
+ <service id =" AppBundle\Mailer" />
83
85
84
- <service id =" app.decorating_mailer"
85
- class =" AppBundle\DecoratingMailer"
86
- decorates =" app.mailer"
86
+ <service id =" AppBundle\DecoratingMailer"
87
+ decorates =" AppBundle\Mailer"
87
88
public =" false"
88
89
>
89
- <argument type =" service" id =" app.decorating_mailer .inner" />
90
+ <argument type =" service" id =" AppBundle\DecoratingMailer .inner" />
90
91
</service >
91
92
92
93
</services >
93
94
</container >
94
95
95
96
.. code-block :: php
96
97
98
+ // config/services.php
97
99
use AppBundle\DecoratingMailer;
98
100
use AppBundle\Mailer;
99
101
use Symfony\Component\DependencyInjection\Reference;
100
102
101
- $container->register('app.mailer', Mailer::class);
103
+ $container->register(Mailer::class);
102
104
103
- $container->register('app.decorating_mailer', DecoratingMailer::class)
104
- ->setDecoratedService('app.mailer' )
105
- ->addArgument(new Reference('app.decorating_mailer .inner'))
105
+ $container->register(DecoratingMailer::class)
106
+ ->setDecoratedService(Mailer::class )
107
+ ->addArgument(new Reference(DecoratingMailer::class.' .inner'))
106
108
->setPublic(false)
107
109
;
108
110
109
- The ``decorates `` option tells the container that the ``app.decorating_mailer `` service
110
- replaces the ``app.mailer `` service. The old ``app.mailer `` service is renamed to
111
- ``app.decorating_mailer .inner `` so you can inject it into your new service.
111
+ The ``decorates `` option tells the container that the ``AppBundle\DecoratingMailer `` service
112
+ replaces the ``AppBundle\Mailer `` service. The old ``AppBundle\Mailer `` service is renamed to
113
+ ``AppBundle\DecoratingMailer .inner `` so you can inject it into your new service.
112
114
113
115
.. tip ::
114
116
115
- The visibility (public) of the decorated ``app.mailer `` service (which is an alias
116
- for the new service) will still be the same as the original ``app.mailer ``
117
+ The visibility (public) of the decorated ``AppBundle\Mailer `` service (which is an alias
118
+ for the new service) will still be the same as the original ``AppBundle\Mailer ``
117
119
visibility.
118
120
119
121
.. note ::
120
122
121
123
The generated inner id is based on the id of the decorator service
122
- (``app.decorating_mailer `` here), not of the decorated service (``app.mailer ``
124
+ (``AppBundle\DecoratingMailer `` here), not of the decorated service (``AppBundle\Mailer ``
123
125
here). You can control the inner service name via the ``decoration_inner_name ``
124
126
option:
125
127
126
128
.. configuration-block ::
127
129
128
130
.. code-block :: yaml
129
131
132
+ # config/services.yaml
130
133
services :
131
- app.decorating_mailer :
134
+ AppBundle\DecoratingMailer :
132
135
# ...
133
- decoration_inner_name : app.decorating_mailer .wooz
134
- arguments : ['@app.decorating_mailer .wooz']
136
+ decoration_inner_name : AppBundle\DecoratingMailer .wooz
137
+ arguments : ['@AppBundle\DecoratingMailer .wooz']
135
138
136
139
.. code-block :: xml
137
140
141
+ <!-- config/services.xml -->
138
142
<?xml version =" 1.0" encoding =" UTF-8" ?>
139
143
<container xmlns =" http://symfony.com/schema/dic/services"
140
144
xmlns : xsd =" http://www.w3.org/2001/XMLSchema-instance"
@@ -144,26 +148,26 @@ replaces the ``app.mailer`` service. The old ``app.mailer`` service is renamed t
144
148
<!-- ... -->
145
149
146
150
<service
147
- id =" app.decorating_mailer"
148
- class =" AppBundle\DecoratingMailer"
149
- decorates =" app.mailer"
150
- decoration-inner-name =" app.decorating_mailer.wooz"
151
+ id =" AppBundle\DecoratingMailer"
152
+ decorates =" AppBundle\Mailer"
153
+ decoration-inner-name =" AppBundle\DecoratingMailer.wooz"
151
154
public =" false"
152
155
>
153
- <argument type =" service" id =" app.decorating_mailer .wooz" />
156
+ <argument type =" service" id =" AppBundle\DecoratingMailer .wooz" />
154
157
</service >
155
158
156
159
</services >
157
160
</container >
158
161
159
162
.. code-block :: php
160
163
164
+ // config/services.php
161
165
use AppBundle\DecoratingMailer;
162
166
use Symfony\Component\DependencyInjection\Reference;
163
167
164
- $container->register('app.decorating_mailer', DecoratingMailer::class)
165
- ->setDecoratedService('app.mailer', 'app.decorating_mailer .wooz')
166
- ->addArgument(new Reference('app.decorating_mailer .wooz'))
168
+ $container->register(DecoratingMailer::class)
169
+ ->setDecoratedService(AppBundle\Mailer, DecoratingMailer::class.' .wooz')
170
+ ->addArgument(new Reference(DecoratingMailer::class.' .wooz'))
167
171
// ...
168
172
;
169
173
@@ -178,62 +182,62 @@ the ``decoration_priority`` option. Its value is an integer that defaults to
178
182
179
183
.. code-block :: yaml
180
184
181
- foo :
182
- class : Foo
185
+ # config/services.yaml
186
+ Foo : ~
183
187
184
- bar :
185
- class : Bar
188
+ Bar :
186
189
public : false
187
- decorates : foo
190
+ decorates : Foo
188
191
decoration_priority : 5
189
- arguments : ['@bar .inner']
192
+ arguments : ['@Bar .inner']
190
193
191
- baz :
192
- class : Baz
194
+ Baz :
193
195
public : false
194
- decorates : foo
196
+ decorates : Foo
195
197
decoration_priority : 1
196
- arguments : ['@baz .inner']
198
+ arguments : ['@Baz .inner']
197
199
198
200
.. code-block :: xml
199
201
202
+ <!-- config/services.xml -->
200
203
<?xml version =" 1.0" encoding =" UTF-8" ?>
201
204
202
205
<container xmlns =" http://symfony.com/schema/dic/services"
203
206
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
204
207
xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
205
208
206
209
<services >
207
- <service id =" foo " class = " Foo" />
210
+ <service id =" Foo" />
208
211
209
- <service id =" bar " class = " Bar" decorates =" foo " decoration-priority =" 5" public =" false" >
210
- <argument type =" service" id =" bar .inner" />
212
+ <service id =" Bar" decorates =" Foo " decoration-priority =" 5" public =" false" >
213
+ <argument type =" service" id =" Bar .inner" />
211
214
</service >
212
215
213
- <service id =" baz " class = " Baz" decorates =" foo " decoration-priority =" 1" public =" false" >
214
- <argument type =" service" id =" baz .inner" />
216
+ <service id =" Baz" decorates =" Foo " decoration-priority =" 1" public =" false" >
217
+ <argument type =" service" id =" Baz .inner" />
215
218
</service >
216
219
</services >
217
220
</container >
218
221
219
222
.. code-block :: php
220
223
224
+ // config/services.php
221
225
use Symfony\Component\DependencyInjection\Reference;
222
226
223
- $container->register('foo', ' Foo' )
227
+ $container->register(Foo:class )
224
228
225
- $container->register('bar', ' Bar' )
226
- ->addArgument(new Reference('bar. inner'))
229
+ $container->register(Bar:class )
230
+ ->addArgument(new Reference(Bar:class.' inner'))
227
231
->setPublic(false)
228
- ->setDecoratedService('foo' , null, 5);
232
+ ->setDecoratedService(Foo:class , null, 5);
229
233
230
- $container->register('baz', ' Baz' )
231
- ->addArgument(new Reference('baz. inner'))
234
+ $container->register(Baz:class )
235
+ ->addArgument(new Reference(Baz:class.' inner'))
232
236
->setPublic(false)
233
- ->setDecoratedService('foo' , null, 1);
237
+ ->setDecoratedService(Foo:class , null, 1);
234
238
235
239
The generated code will be the following::
236
240
237
- $this->services['foo' ] = new Baz(new Bar(new Foo()));
241
+ $this->services[Foo:class ] = new Baz(new Bar(new Foo()));
238
242
239
243
.. _decorator pattern : https://en.wikipedia.org/wiki/Decorator_pattern
0 commit comments