Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Add section about anonymous services in YAML #10041

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 11, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 69 additions & 18 deletions 87 service_container/alias_private.rst
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ Anonymous Services

.. note::

Anonymous services are only supported by the XML configuration format.
Anonymous services are only supported by the XML and YAML configuration formats.

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

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

.. code-block:: xml
.. configuration-block::

.. code-block:: yaml

# app/config/services.yml
services:
_defaults:
autowire: true

AppBundle\Foo:
arguments:
- !service
class: AppBundle\AnonymousBar
autowire: true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed given that the default for this file is true too?


.. code-block:: xml

<!-- app/config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<defaults autowire="true" />

<service id="foo" class="AppBundle\Foo">
<argument type="service">
<service class="AppBundle\AnonymousBar" />
</argument>
</service>
</services>
</container>

<!-- app/config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">
Using an anonymous service as a factory looks like this:

<services>
<service id="foo" class="AppBundle\Foo">
<argument type="service">
<service class="AppBundle\AnonymousBar" />
</argument>
</service>
</services>
</container>
.. configuration-block::

.. code-block:: yaml

# app/config/services.yml
services:
_defaults:
autowire: true

AppBundle\Foo:
factory: [ !service { class: AppBundle\FooFactory }, 'constructFoo' ]

.. code-block:: xml

<!-- app/config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<defaults autowire="true" />

<service id="foo" class="AppBundle\Foo">
<factory method="constructFoo">
<service class="App\FooFactory"/>
</factory>
</service>
</services>
</container>

Deprecating Services
--------------------
Expand All @@ -186,8 +237,8 @@ or you decided not to maintain it anymore), you can deprecate its definition:

.. code-block:: yaml

AppBundle\Service\OldService:
deprecated: The "%service_id%" service is deprecated since 2.8 and will be removed in 3.0.
AppBundle\Service\OldService:
deprecated: The "%service_id%" service is deprecated since 2.8 and will be removed in 3.0.

.. code-block:: xml

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.