Skip to content

Commit b282902

Browse files
committed
minor #9392 Use FQCN as service ID (slootjes)
This PR was submitted for the 3.3 branch but it was merged into the 3.4 branch instead (closes #9392). Discussion ---------- Use FQCN as service ID I was looking at this page in 4.0 and it seemed like a good idea to show the examples with the FQCN rather than names service IDs instead. If accepted I can also apply the same change to 4.0 branch if needed. Commits ------- 222b07f Use FQCN as service ID
2 parents e5015c2 + 222b07f commit b282902

File tree

1 file changed

+72
-68
lines changed

1 file changed

+72
-68
lines changed

service_container/service_decoration.rst

Lines changed: 72 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,43 @@ the original service is lost:
1111

1212
.. code-block:: yaml
1313
14+
# config/services.yaml
1415
services:
15-
app.mailer:
16-
class: AppBundle\Mailer
16+
AppBundle\Mailer: ~
1717
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
1919
# old definition is lost
20-
app.mailer:
20+
AppBundle\Mailer:
2121
class: AppBundle\DecoratingMailer
2222
2323
.. code-block:: xml
2424
25+
<!-- config/services.xml -->
2526
<?xml version="1.0" encoding="UTF-8" ?>
2627
<container xmlns="http://symfony.com/schema/dic/services"
2728
xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
2829
xsd:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
2930
3031
<services>
31-
<service id="app.mailer" class="AppBundle\Mailer" />
32+
<service id="AppBundle\Mailer" />
3233
33-
<!-- this replaces the old app.mailer definition with the new
34+
<!-- this replaces the old AppBundle\Mailer definition with the new
3435
one, the old definition is lost -->
35-
<service id="app.mailer" class="AppBundle\DecoratingMailer" />
36+
<service id="AppBundle\Mailer" class="AppBundle\DecoratingMailer" />
3637
</services>
3738
</container>
3839
3940
.. code-block:: php
4041
42+
// config/services.php
4143
use AppBundle\Mailer;
4244
use AppBundle\DecoratingMailer;
4345
44-
$container->register('app.mailer', Mailer::class);
46+
$container->register(Mailer::class);
4547
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
4749
// old definition is lost
48-
$container->register('app.mailer', DecoratingMailer::class);
50+
$container->register(Mailer::class, DecoratingMailer::class);
4951
5052
Most of the time, that's exactly what you want to do. But sometimes,
5153
you might want to decorate the old service instead and keep the old service so
@@ -55,86 +57,88 @@ that you can reference it:
5557

5658
.. code-block:: yaml
5759
60+
# config/services.yaml
5861
services:
59-
app.mailer:
60-
class: AppBundle\Mailer
62+
AppBundle\Mailer: ~
6163
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
6768
6869
# pass the old service as an argument
69-
arguments: ['@app.decorating_mailer.inner']
70+
arguments: ['@AppBundle\DecoratingMailer.inner']
7071
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
7273
public: false
7374
7475
.. code-block:: xml
7576
77+
<!-- config/services.xml -->
7678
<?xml version="1.0" encoding="UTF-8" ?>
7779
<container xmlns="http://symfony.com/schema/dic/services"
7880
xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
7981
xsd:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
8082
8183
<services>
82-
<service id="app.mailer" class="AppBundle\Mailer" />
84+
<service id="AppBundle\Mailer" />
8385
84-
<service id="app.decorating_mailer"
85-
class="AppBundle\DecoratingMailer"
86-
decorates="app.mailer"
86+
<service id="AppBundle\DecoratingMailer"
87+
decorates="AppBundle\Mailer"
8788
public="false"
8889
>
89-
<argument type="service" id="app.decorating_mailer.inner" />
90+
<argument type="service" id="AppBundle\DecoratingMailer.inner" />
9091
</service>
9192
9293
</services>
9394
</container>
9495
9596
.. code-block:: php
9697
98+
// config/services.php
9799
use AppBundle\DecoratingMailer;
98100
use AppBundle\Mailer;
99101
use Symfony\Component\DependencyInjection\Reference;
100102
101-
$container->register('app.mailer', Mailer::class);
103+
$container->register(Mailer::class);
102104
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'))
106108
->setPublic(false)
107109
;
108110
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.
112114

113115
.. tip::
114116

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``
117119
visibility.
118120

119121
.. note::
120122

121123
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``
123125
here). You can control the inner service name via the ``decoration_inner_name``
124126
option:
125127

126128
.. configuration-block::
127129

128130
.. code-block:: yaml
129131
132+
# config/services.yaml
130133
services:
131-
app.decorating_mailer:
134+
AppBundle\DecoratingMailer:
132135
# ...
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']
135138
136139
.. code-block:: xml
137140
141+
<!-- config/services.xml -->
138142
<?xml version="1.0" encoding="UTF-8" ?>
139143
<container xmlns="http://symfony.com/schema/dic/services"
140144
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
144148
<!-- ... -->
145149
146150
<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"
151154
public="false"
152155
>
153-
<argument type="service" id="app.decorating_mailer.wooz" />
156+
<argument type="service" id="AppBundle\DecoratingMailer.wooz" />
154157
</service>
155158
156159
</services>
157160
</container>
158161
159162
.. code-block:: php
160163
164+
// config/services.php
161165
use AppBundle\DecoratingMailer;
162166
use Symfony\Component\DependencyInjection\Reference;
163167
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'))
167171
// ...
168172
;
169173
@@ -178,62 +182,62 @@ the ``decoration_priority`` option. Its value is an integer that defaults to
178182

179183
.. code-block:: yaml
180184
181-
foo:
182-
class: Foo
185+
# config/services.yaml
186+
Foo: ~
183187
184-
bar:
185-
class: Bar
188+
Bar:
186189
public: false
187-
decorates: foo
190+
decorates: Foo
188191
decoration_priority: 5
189-
arguments: ['@bar.inner']
192+
arguments: ['@Bar.inner']
190193
191-
baz:
192-
class: Baz
194+
Baz:
193195
public: false
194-
decorates: foo
196+
decorates: Foo
195197
decoration_priority: 1
196-
arguments: ['@baz.inner']
198+
arguments: ['@Baz.inner']
197199
198200
.. code-block:: xml
199201
202+
<!-- config/services.xml -->
200203
<?xml version="1.0" encoding="UTF-8" ?>
201204
202205
<container xmlns="http://symfony.com/schema/dic/services"
203206
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
204207
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
205208
206209
<services>
207-
<service id="foo" class="Foo" />
210+
<service id="Foo" />
208211
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" />
211214
</service>
212215
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" />
215218
</service>
216219
</services>
217220
</container>
218221
219222
.. code-block:: php
220223
224+
// config/services.php
221225
use Symfony\Component\DependencyInjection\Reference;
222226
223-
$container->register('foo', 'Foo')
227+
$container->register(Foo:class)
224228
225-
$container->register('bar', 'Bar')
226-
->addArgument(new Reference('bar.inner'))
229+
$container->register(Bar:class)
230+
->addArgument(new Reference(Bar:class.'inner'))
227231
->setPublic(false)
228-
->setDecoratedService('foo', null, 5);
232+
->setDecoratedService(Foo:class, null, 5);
229233
230-
$container->register('baz', 'Baz')
231-
->addArgument(new Reference('baz.inner'))
234+
$container->register(Baz:class)
235+
->addArgument(new Reference(Baz:class.'inner'))
232236
->setPublic(false)
233-
->setDecoratedService('foo', null, 1);
237+
->setDecoratedService(Foo:class, null, 1);
234238
235239
The generated code will be the following::
236240

237-
$this->services['foo'] = new Baz(new Bar(new Foo()));
241+
$this->services[Foo:class] = new Baz(new Bar(new Foo()));
238242

239243
.. _decorator pattern: https://en.wikipedia.org/wiki/Decorator_pattern

0 commit comments

Comments
 (0)