Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Service Container
  4. How to Decorate Services
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
  • Decoration Priority

How to Decorate Services

Edit this page

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

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

How to Decorate Services

When overriding an existing definition, the original service is lost:

1
2
3
4
5
6
7
8
# config/services.yaml
services:
    AppBundle\Mailer: ~

    # this replaces the old AppBundle\Mailer definition with the new one, the
    # old definition is lost
    AppBundle\Mailer:
        class: AppBundle\NewMailer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!-- config/services.xml -->
<?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 https://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>
        <service id="AppBundle\Mailer"/>

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

$container->register(Mailer::class);

// this replaces the old AppBundle\Mailer definition with the new one, the
// old definition is lost
$container->register(Mailer::class, 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 (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\DecoratingMailer.inner:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# config/services.yaml
services:
    AppBundle\Mailer: ~

    AppBundle\DecoratingMailer:
        # overrides the AppBundle\Mailer service
        # but that service is still available as AppBundle\DecoratingMailer.inner
        decorates: AppBundle\Mailer

        # pass the old service as an argument
        arguments: ['@AppBundle\DecoratingMailer.inner']

        # private, because usually you do not need to fetch AppBundle\DecoratingMailer directly
        public:    false
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!-- config/services.xml -->
<?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 https://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>
        <service id="AppBundle\Mailer"/>

        <!-- overrides the AppBundle\Mailer service
             but that service is still available as AppBundle\DecoratingMailer.inner
             private, because usually you do not need to fetch AppBundle\DecoratingMailer directly -->
        <service id="AppBundle\DecoratingMailer"
            decorates="AppBundle\Mailer"
            public="false"
        >
            <!-- pass the old service as an argument -->
            <argument type="service" id="AppBundle\DecoratingMailer.inner"/>
        </service>

    </services>
</container>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// config/services.php
use AppBundle\DecoratingMailer;
use AppBundle\Mailer;
use Symfony\Component\DependencyInjection\Reference;

$container->register(Mailer::class);

// overrides the AppBundle\Mailer service
// but that service is still available as AppBundle\DecoratingMailer.inner
$container->register(DecoratingMailer::class)
    ->setDecoratedService(Mailer::class)
    // pass the old service as an argument
    ->addArgument(new Reference(DecoratingMailer::class.'.inner'))
    // private, because usually you do not need to fetch AppBundle\DecoratingMailer directly
    ->setPublic(false)
;

The decorates option tells the container that the AppBundle\DecoratingMailer service replaces the AppBundle\Mailer service. The old AppBundle\Mailer service is renamed to AppBundle\DecoratingMailer.inner so you can inject it into your new service.

Tip

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

Note

The generated inner id is based on the id of the decorator service (AppBundle\DecoratingMailer here), not of the decorated service (AppBundle\Mailer here). You can control the inner service name via the decoration_inner_name option:

1
2
3
4
5
6
# config/services.yaml
services:
    AppBundle\DecoratingMailer:
        # ...
        decoration_inner_name: AppBundle\DecoratingMailer.wooz
        arguments: ['@AppBundle\DecoratingMailer.wooz']
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!-- config/services.xml -->
<?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 https://symfony.com/schema/dic/services/services-1.0.xsd">

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

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

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

$container->register(DecoratingMailer::class)
    ->setDecoratedService(AppBundle\Mailer, DecoratingMailer::class.'.wooz')
    ->addArgument(new Reference(DecoratingMailer::class.'.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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# config/services.yaml
Foo: ~

Bar:
    public: false
    decorates: Foo
    decoration_priority: 5
    arguments: ['@Bar.inner']

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
19
<!-- 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 https://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>
        <service id="Foo"/>

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

        <service id="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
14
// config/services.php
use Symfony\Component\DependencyInjection\Reference;

$container->register(Foo::class)

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

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

The generated code will be the following:

1
$this->services[Foo::class] = new Baz(new Bar(new Foo()));
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    Version:
    Be safe against critical risks to your projects and businesses

    Be safe against critical risks to your projects and businesses

    Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

    Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

    Symfony footer

    Avatar of VosKoen, a Symfony contributor

    Thanks VosKoen for being a Symfony contributor

    1 commit • 2 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