8000 minor #10041 Add section about anonymous services in YAML (Tim Goudri… · symfony/symfony-docs@407fe1d · GitHub
[go: up one dir, main page]

Skip to content

Commit 407fe1d

Browse files
committed
minor #10041 Add section about anonymous services in YAML (Tim Goudriaan)
This PR was merged into the 3.4 branch. Discussion ---------- Add section about anonymous services in YAML Fixes #7694. This is a continuation of #9909 which added some information about anonymous services in XML to the docs, next will be a commit that describes configuring anonymous services in PHP for the `master` branch. For further information about the issue, see #5854. Commits ------- 8a83664 Add section about anonymous services in YAML
2 parents 9470788 + 8a83664 commit 407fe1d

File tree

1 file changed

+69
-18
lines changed

1 file changed

+69
-18
lines changed

service_container/alias_private.rst

Lines changed: 69 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ Anonymous Services
149149

150150
.. note::
151151

152-
Anonymous services are only supported by the XML configuration format.
152+
Anonymous services are only supported by the XML and YAML configuration formats.
153153

154154
In some cases, you may want to prevent a service being used as a dependency of
155155
other services. This can be achieved by creating an anonymous service. These
@@ -158,23 +158,74 @@ created where they are used.
158158

159159
The following example shows how to inject an anonymous service into another service:
160160

161-
.. code-block:: xml
161+
.. configuration-block::
162+
163+
.. code-block:: yaml
164+
165+
# app/config/services.yml
166+
services:
167+
_defaults:
168+
autowire: true
169+
170+
AppBundle\Foo:
171+
arguments:
172+
- !service
173+
class: AppBundle\AnonymousBar
174+
autowire: true
175+
176+
.. code-block:: xml
177+
178+
<!-- app/config/services.xml -->
179+
<?xml version="1.0" encoding="UTF-8" ?>
180+
<container xmlns="http://symfony.com/schema/dic/services"
181+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
182+
xsi:schemaLocation="http://symfony.com/schema/dic/services
183+
http://symfony.com/schema/dic/services/services-1.0.xsd">
184+
185+
<services>
186+
<defaults autowire="true" />
187+
188+
<service id="foo" class="AppBundle\Foo">
189+
<argument type="service">
190+
<service class="AppBundle\AnonymousBar" />
191+
</argument>
192+
</service>
193+
</services>
194+
</container>
162195
163-
<!-- app/config/services.xml -->
164-
<?xml version="1.0" encoding="UTF-8" ?>
165-
<container xmlns="http://symfony.com/schema/dic/services"
166-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
167-
xsi:schemaLocation="http://symfony.com/schema/dic/services
168-
http://symfony.com/schema/dic/services/services-1.0.xsd">
196+
Using an anonymous service as a factory looks like this:
169197

170-
<services>
171-
<service id="foo" class="AppBundle\Foo">
172-
<argument type="service">
173-
<service class="AppBundle\AnonymousBar" />
174-
</argument>
175-
</service>
176-
</services>
177-
</container>
198+
.. configuration-block::
199+
200+
.. code-block:: yaml
201+
202+
# app/config/services.yml
203+
services:
204+
_defaults:
205+
autowire: true
206+
207+
AppBundle\Foo:
208+
factory: [ !service { class: AppBundle\FooFactory }, 'constructFoo' ]
209+
210+
.. code-block:: xml
211+
212+
<!-- app/config/services.xml -->
213+
<?xml version="1.0" encoding="UTF-8" ?>
214+
<container xmlns="http://symfony.com/schema/dic/services"
215+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
216+
xsi:schemaLocation="http://symfony.com/schema/dic/services
217+
http://symfony.com/schema/dic/services/services-1.0.xsd">
218+
219+
<services>
220+
<defaults autowire="true" />
221+
222+
<service id="foo" class="AppBundle\Foo">
223+
<factory method="constructFoo">
224+
<service class="App\FooFactory"/>
225+
</factory>
226+
</service>
227+
</services>
228+
</container>
178229
179230
Deprecating Services
180231
--------------------
@@ -186,8 +237,8 @@ or you decided not to maintain it anymore), you can deprecate its definition:
186237

187238
.. code-block:: yaml
188239
189-
AppBundle\Service\OldService:
190-
deprecated: The "%service_id%" service is deprecated since 2.8 and will be removed in 3.0.
240+
AppBundle\Service\OldService:
241+
deprecated: The "%service_id%" service is deprecated since 2.8 and will be removed in 3.0.
191242
192243
.. code-block:: xml
193244

0 commit comments

Comments
 (0)
0