Skip to content

Update service_decoration.rst #10654

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

Closed
wants to merge 2 commits into from
Closed
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
19 changes: 9 additions & 10 deletions service_container/service_decoration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
How to Decorate Services
========================

When overriding an existing definition (e.g. when applying the `Decorator pattern`_),
the original service is lost:
When overriding an existing definition, the original service is lost:

.. configuration-block::

Expand All @@ -18,7 +17,7 @@ the original service is lost:
# this replaces the old app.mailer definition with the new one, the
# old definition is lost
app.mailer:
class: AppBundle\DecoratingMailer
class: AppBundle\NewMailer

.. code-block:: xml

Expand All @@ -32,26 +31,26 @@ the original service is lost:

<!-- this replaces the old app.mailer definition with the new
one, the old definition is lost -->
<service id="app.mailer" class="AppBundle\DecoratingMailer" />
<service id="app.mailer" class="AppBundle\NewMailer" />
</services>
</container>

.. code-block:: php

use AppBundle\Mailer;
use AppBundle\DecoratingMailer;
use AppBundle\NewMailer;

$container->register('app.mailer', Mailer::class);

// this replaces the old app.mailer definition with the new one, the
// old definition is lost
$container->register('app.mailer', DecoratingMailer::class);
$container->register('app.mailer', NewMailer::class);

Most of the time, that's exactly what you want to do. But sometimes,
you might want to decorate the old one instead. In this case, the
old service should be kept around to be able to reference it in the
new one. This configuration replaces ``app.mailer`` with a new one, but keeps
a reference of the old one as ``app.decorating_mailer.inner``:
you might want to decorate the old one instead (i.e. apply the `Decorator pattern`_).
In this case, the old service should be kept around to be able to reference
it in the new one. This configuration replaces ``app.mailer`` with a new one,
but keeps a reference of the old one as ``app.decorating_mailer.inner``:

.. configuration-block::

Expand Down