How to Decorate Services
Warning: You are browsing the documentation for Symfony 4.x, which is no longer maintained.
Read the updated version of this page for Symfony 7.1 (the current stable version).
When overriding an existing definition, the original service is lost:
1 2 3 4 5 6 7 8
# config/services.yaml
services:
App\Mailer: ~
# this replaces the old App\Mailer definition with the new one, the
# old definition is lost
App\Mailer:
class: App\NewMailer
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
# config/services.yaml
services:
App\Mailer: ~
App\DecoratingMailer:
# overrides the App\Mailer service
# but that service is still available as App\DecoratingMailer.inner
decorates: App\Mailer
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'
):
1 2 3 4 5 6 7 8
# config/services.yaml
services:
App\Mailer: ~
App\DecoratingMailer:
decorates: App\Mailer
# pass the old service as an argument
arguments: ['@App\DecoratingMailer.inner']
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:
1 2 3 4 5 6
# config/services.yaml
services:
App\DecoratingMailer:
# ...
decoration_inner_name: App\DecoratingMailer.wooz
arguments: ['@App\DecoratingMailer.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
# config/services.yaml
Foo: ~
Bar:
decorates: Foo
decoration_priority: 5
arguments: ['@Bar.inner']
Baz:
decorates: Foo
decoration_priority: 1
arguments: ['@Baz.inner']
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
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
: AServiceNotFoundException
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 tonull
.
1 2 3 4 5 6 7
# config/services.yaml
Foo: ~
Bar:
decorates: Foo
decoration_on_invalid: ignore
arguments: ['@Bar.inner']
Caution
When using null
, you may have to update the decorator constructor in
order to make decorated dependency nullable:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// src/Service/DecoratorService.php
namespace App\Service;
use Acme\OptionalBundle\Service\OptionalService;
class DecoratorService
{
private $decorated;
public function __construct(?OptionalService $decorated)
{
$this->decorated = $decorated;
}
public function tellInterestingStuff(): string
{
if (!$this->decorated) {
return 'Just one interesting thing';
}
return $this->decorated->tellInterestingStuff().' + one more interesting thing';
}
}
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.