Symfony
sponsored by SensioLabs
Menu
  • About
  • Documentation
  • Screencasts
  • Cloud
  • Certification
  • Community
  • Businesses
  • News
  • Download
  1. Home
  2. Documentation
  3. Service Container
  4. How to Decorate Services
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
Search by Algolia
  • Decoration Priority

How to Decorate Services

Edit this page

Warning: You are browsing the documentation for Symfony 3.3, which is no longer maintained.

Read the updated version of this page for Symfony 6.2 (the current stable version).

How to Decorate Services

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

  • YAML
  • XML
  • PHP
1
2
3
4
5
6
7
8
services:
    app.mailer:
        class: AppBundle\Mailer

    # this replaces the old app.mailer definition with the new one, the
    # old definition is lost
    app.mailer:
        class: AppBundle\DecoratingMailer
1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
    xsd:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>
        <service id="app.mailer" class="AppBundle\Mailer" />

        <!-- this replaces the old app.mailer definition with the new
             one, the old definition is lost -->
        <service id="app.mailer" class="AppBundle\DecoratingMailer" />
    </services>
</container>
1
2
3
4
5
6
7
8
use AppBundle\Mailer;
use AppBundle\DecoratingMailer;

$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);

Most of the time, that's exactly what you want to do. But sometimes, you might want to decorate the old service instead and keep the old service so that you can reference it:

  • YAML
  • XML
  • PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
services:
    app.mailer:
        class: AppBundle\Mailer

    app.decorating_mailer:
        class:     AppBundle\DecoratingMailer
        # overrides the app.mailer service
        # but that service is still available as app.decorating_mailer.inner
        decorates: app.mailer

        # pass the old service as an argument
        arguments: ['@app.decorating_mailer.inner']

        # private, because usually you do not need to fetch app.decorating_mailer directly
        public:    false
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
    xsd:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>
        <service id="app.mailer" class="AppBundle\Mailer" />

        <service id="app.decorating_mailer"
            class="AppBundle\DecoratingMailer"
            decorates="app.mailer"
            public="false"
        >
            <argument type="service" id="app.decorating_mailer.inner" />
        </service>

    </services>
</container>
1
2
3
4
5
6
7
8
9
10
11
use AppBundle\DecoratingMailer;
use AppBundle\Mailer;
use Symfony\Component\DependencyInjection\Reference;

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

$container->register('app.decorating_mailer', DecoratingMailer::class)
    ->setDecoratedService('app.mailer')
    ->addArgument(new Reference('app.decorating_mailer.inner'))
    ->setPublic(false)
;

The decorates option tells the container that the app.decorating_mailer service replaces the app.mailer service. The old app.mailer service is renamed to app.decorating_mailer.inner so you can inject it into your new service.

Tip

The visibility (public) of the decorated app.mailer service (which is an alias for the new service) will still be the same as the original app.mailer visibility.

Note

The generated inner id is based on the id of the decorator service (app.decorating_mailer here), not of the decorated service (app.mailer here). You can control the inner service name via the decoration_inner_name option:

  • YAML
  • XML
  • PHP
1
2
3
4
5
services:
    app.decorating_mailer:
        # ...
        decoration_inner_name: app.decorating_mailer.wooz
        arguments: ['@app.decorating_mailer.wooz']
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
    xsd:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>
        <!-- ... -->

        <service
            id="app.decorating_mailer"
            class="AppBundle\DecoratingMailer"
            decorates="app.mailer"
            decoration-inner-name="app.decorating_mailer.wooz"
            public="false"
        >
            <argument type="service" id="app.decorating_mailer.wooz" />
        </service>

    </services>
</container>
1
2
3
4
5
6
7
8
use AppBundle\DecoratingMailer;
use Symfony\Component\DependencyInjection\Reference;

$container->register('app.decorating_mailer', DecoratingMailer::class)
    ->setDecoratedService('app.mailer', 'app.decorating_mailer.wooz')
    ->addArgument(new Reference('app.decorating_mailer.wooz'))
    // ...
;

Decoration Priority

When applying multiple decorators to a service, you can control their order with the decoration_priority option. Its value is an integer that defaults to 0 and higher priorities mean that decorators will be applied earlier.

  • YAML
  • XML
  • PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
foo:
    class: Foo

bar:
    class: Bar
    public: false
    decorates: foo
    decoration_priority: 5
    arguments: ['@bar.inner']

baz:
    class: Baz
    public: false
    decorates: foo
    decoration_priority: 1
    arguments: ['@baz.inner']
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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>
        <service id="foo" class="Foo" />

        <service id="bar" class="Bar" decorates="foo" decoration-priority="5" public="false">
            <argument type="service" id="bar.inner" />
        </service>

        <service id="baz" class="Baz" decorates="foo" decoration-priority="1" public="false">
            <argument type="service" id="baz.inner" />
        </service>
    </services>
</container>
1
2
3
4
5
6
7
8
9
10
11
12
13
use Symfony\Component\DependencyInjection\Reference;

$container->register('foo', 'Foo')

$container->register('bar', 'Bar')
    ->addArgument(new Reference('bar.inner'))
    ->setPublic(false)
    ->setDecoratedService('foo', null, 5);

$container->register('baz', 'Baz')
    ->addArgument(new Reference('baz.inner'))
    ->setPublic(false)
    ->setDecoratedService('foo', null, 1);

The generated code will be the following:

1
$this->services['foo'] = new Baz(new Bar(new Foo()));
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
We stand with Ukraine.
Version:
Symfony Code Performance Profiling

Symfony Code Performance Profiling

Code consumes server resources. Blackfire tells you how

Code consumes server resources. Blackfire tells you how

↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

Avatar of Beau Simensen, a Symfony contributor

Thanks Beau Simensen (@simensen) for being a Symfony contributor

6 commits • 483 lines changed

View all contributors that help us make Symfony

Become a Symfony contributor

Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

Learn how to contribute

Symfony™ is a trademark of Symfony SAS. All rights reserved.

  • What is Symfony?
    • Symfony at a Glance
    • Symfony Components
    • Case Studies
    • Symfony Releases
    • Security Policy
    • Logo & Screenshots
    • Trademark & Licenses
    • symfony1 Legacy
  • Learn Symfony
    • Symfony Docs
    • Symfony Book
    • Reference
    • Bundles
    • Best Practices
    • Training
    • eLearning Platform
    • Certification
  • Screencasts
    • Learn Symfony
    • Learn PHP
    • Learn JavaScript
    • Learn Drupal
    • Learn RESTful APIs
  • Community
    • SymfonyConnect
    • Support
    • How to be Involved
    • Code of Conduct
    • Events & Meetups
    • Projects using Symfony
    • Downloads Stats
    • Contributors
    • Backers
  • Blog
    • Events & Meetups
    • A week of symfony
    • Case studies
    • Cloud
    • Community
    • Conferences
    • Diversity
    • Documentation
    • Living on the edge
    • Releases
    • Security Advisories
    • SymfonyInsight
    • Twig
    • SensioLabs
  • Services
    • SensioLabs services
    • Train developers
    • Manage your project quality
    • Improve your project performance
    • Host Symfony projects
    Deployed on
Follow Symfony
Search by Algolia