How to Decorate Services

How to Decorate Services

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

  • YAML

    1. # config/services.yaml
    2. services:
    3. App\Mailer: ~
    4. # this replaces the old App\Mailer definition with the new one, the
    5. # old definition is lost
    6. App\Mailer:
    7. class: App\NewMailer
  • XML

    1. <!-- config/services.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
    5. xsd:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
    6. <services>
    7. <service id="App\Mailer"/>
    8. <!-- this replaces the old App\Mailer definition with the new
    9. one, the old definition is lost -->
    10. <service id="App\Mailer" class="App\NewMailer"/>
    11. </services>
    12. </container>
  • PHP

    1. // config/services.php
    2. namespace Symfony\Component\DependencyInjection\Loader\Configurator;
    3. use App\Mailer;
    4. use App\NewMailer;
    5. return function(ContainerConfigurator $configurator) {
    6. $services = $configurator->services();
    7. $services->set(Mailer::class);
    8. // this replaces the old App\Mailer definition with the new one, the
    9. // old definition is lost
    10. $services->set(Mailer::class, NewMailer::class);
    11. };

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:

  • YAML

    1. # config/services.yaml
    2. services:
    3. App\Mailer: ~
    4. App\DecoratingMailer:
    5. # overrides the App\Mailer service
    6. # but that service is still available as App\DecoratingMailer.inner
    7. decorates: App\Mailer
  • XML

    1. <!-- config/services.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
    5. xsd:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
    6. <services>
    7. <service id="App\Mailer"/>
    8. <service id="App\DecoratingMailer"
    9. decorates="App\Mailer"
    10. />
    11. </services>
    12. </container>
  • PHP

    1. // config/services.php
    2. namespace Symfony\Component\DependencyInjection\Loader\Configurator;
    3. use App\DecoratingMailer;
    4. use App\Mailer;
    5. return function(ContainerConfigurator $configurator) {
    6. $services = $configurator->services();
    7. $services->set(Mailer::class);
    8. $services->set(DecoratingMailer::class)
    9. // overrides the App\Mailer service
    10. // but that service is still available as App\DecoratingMailer.inner
    11. ->decorate(Mailer::class);
    12. };

The decorates option tells the container that the App\DecoratingMailer service replaces the App\Mailer service. If you’re using the default services.yaml configuration, the decorated service is automatically injected when the constructor of the decorating service has one argument type-hinted with the decorated service class.

If you are not using autowiring or the decorating service has more than one constructor argument type-hinted with the decorated service class, you must inject the decorated service explicitly (the ID of the decorated service is automatically changed to decorating_service_id + '.inner'):

  • YAML

    1. # config/services.yaml
    2. services:
    3. App\Mailer: ~
    4. App\DecoratingMailer:
    5. decorates: App\Mailer
    6. # pass the old service as an argument
    7. arguments: ['@App\DecoratingMailer.inner']
  • XML

    1. <!-- config/services.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
    5. xsd:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
    6. <services>
    7. <service id="App\Mailer"/>
    8. <service id="App\DecoratingMailer"
    9. decorates="App\Mailer"
    10. >
    11. <!-- pass the old service as an argument -->
    12. <argument type="service" id="App\DecoratingMailer.inner"/>
    13. </service>
    14. </services>
    15. </container>
  • PHP

    1. // config/services.php
    2. namespace Symfony\Component\DependencyInjection\Loader\Configurator;
    3. use App\DecoratingMailer;
    4. use App\Mailer;
    5. return function(ContainerConfigurator $configurator) {
    6. $services = $configurator->services();
    7. $services->set(Mailer::class);
    8. $services->set(DecoratingMailer::class)
    9. ->decorate(Mailer::class)
    10. // pass the old service as an argument
    11. ->args([ref(DecoratingMailer::class.'.inner')]);
    12. };

Tip

The visibility 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\DecoratingMailer here), not of the decorated service (App\Mailer here). You can control the inner service name via the decoration_inner_name option:

  • YAML

    1. # config/services.yaml
    2. services:
    3. App\DecoratingMailer:
    4. # ...
    5. decoration_inner_name: App\DecoratingMailer.wooz
    6. arguments: ['@App\DecoratingMailer.wooz']
  • XML

    1. <!-- config/services.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
    5. xsd:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
    6. <services>
    7. <!-- ... -->
    8. <service
    9. id="App\DecoratingMailer"
    10. decorates="App\Mailer"
    11. decoration-inner-name="App\DecoratingMailer.wooz"
    12. public="false"
    13. >
    14. <argument type="service" id="App\DecoratingMailer.wooz"/>
    15. </service>
    16. </services>
    17. </container>
  • PHP

    1. // config/services.php
    2. namespace Symfony\Component\DependencyInjection\Loader\Configurator;
    3. use App\DecoratingMailer;
    4. use App\Mailer;
    5. return function(ContainerConfigurator $configurator) {
    6. $services = $configurator->services();
    7. $services->set(Mailer::class);
    8. $services->set(DecoratingMailer::class)
    9. ->decorate(Mailer::class, DecoratingMailer::class.'.wooz')
    10. ->args([ref(DecoratingMailer::class.'.wooz')]);
    11. };

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

    1. # config/services.yaml
    2. Foo: ~
    3. Bar:
    4. decorates: Foo
    5. decoration_priority: 5
    6. arguments: ['@Bar.inner']
    7. Baz:
    8. decorates: Foo
    9. decoration_priority: 1
    10. arguments: ['@Baz.inner']
  • XML

    1. <!-- config/services.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
    6. <services>
    7. <service id="Foo"/>
    8. <service id="Bar" decorates="Foo" decoration-priority="5">
    9. <argument type="service" id="Bar.inner"/>
    10. </service>
    11. <service id="Baz" decorates="Foo" decoration-priority="1">
    12. <argument type="service" id="Baz.inner"/>
    13. </service>
    14. </services>
    15. </container>
  • PHP

    1. // config/services.php
    2. namespace Symfony\Component\DependencyInjection\Loader\Configurator;
    3. return function(ContainerConfigurator $configurator) {
    4. $services = $configurator->services();
    5. $services->set(Foo::class);
    6. $services->set(Bar::class)
    7. ->decorate(Foo::class, null, 5)
    8. ->args([ref(Bar::class.'.inner')]);
    9. $services->set(Baz::class)
    10. ->decorate(Foo::class, null, 1)
    11. ->args([ref(Baz::class.'.inner')]);
    12. };

The generated code will be the following:

  1. $this->services[Foo::class] = new Baz(new Bar(new Foo()));

Control the Behavior When the Decorated Service Does Not Exist

New in version 4.4: The decoration_on_invalid option has been introduced in Symfony 4.4. In previous versions, a ServiceNotFoundException was always thrown.

When you decorate a service that doesn’t exist, the decoration_on_invalid option allows you to choose the behavior to adopt.

Three different behaviors are available:

  • exception: A ServiceNotFoundException will be thrown telling that decorator’s dependency is missing. (default)
  • ignore: The container will remove the decorator.
  • null: The container will keep the decorator service and will set the decorated one to null.

  • YAML

    1. # config/services.yaml
    2. Foo: ~
    3. Bar:
    4. decorates: Foo
    5. decoration_on_invalid: ignore
    6. arguments: ['@Bar.inner']
  • XML

    1. <!-- config/services.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
    6. <services>
    7. <service id="Foo"/>
    8. <service id="Bar" decorates="Foo" decoration-on-invalid="ignore">
    9. <argument type="service" id="Bar.inner"/>
    10. </service>
    11. </services>
    12. </container>
  • PHP

    1. // config/services.php
    2. namespace Symfony\Component\DependencyInjection\Loader\Configurator;
    3. use Symfony\Component\DependencyInjection\ContainerInterface;
    4. return function(ContainerConfigurator $configurator) {
    5. $services = $configurator->services();
    6. $services->set(Foo::class);
    7. $services->set(Bar::class)
    8. ->decorate(Foo::class, null, 0, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)
    9. ->args([ref(Bar::class.'.inner')])
    10. ;
    11. };

Caution

When using null, you may have to update the decorator constructor in order to make decorated dependency nullable:

  1. // src/Service/DecoratorService.php
  2. namespace App\Service;
  3. use Acme\OptionalBundle\Service\OptionalService;
  4. class DecoratorService
  5. {
  6. private $decorated;
  7. public function __construct(?OptionalService $decorated)
  8. {
  9. $this->decorated = $decorated;
  10. }
  11. public function tellInterestingStuff(): string
  12. {
  13. if (!$this->decorated) {
  14. return 'Just one interesting thing';
  15. }
  16. return $this->decorated->tellInterestingStuff().' + one more interesting thing';
  17. }
  18. }

Note

Sometimes, you may want to add a compiler pass that creates service definitions on the fly. If you want to decorate such a service, be sure that your compiler pass is registered with PassConfig::TYPE_BEFORE_OPTIMIZATION type so that the decoration pass will be able to find the created services.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.