From 555f7f35a3cd6f377f433d60328f88f24430c138 Mon Sep 17 00:00:00 2001 From: "hubert.lenoir" Date: Thu, 21 Apr 2022 22:53:01 +0200 Subject: [PATCH] [DependencyInjection] Add as decorator and inner service --- service_container/service_decoration.rst | 87 ++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/service_container/service_decoration.rst b/service_container/service_decoration.rst index 3b369373072..0ef5ed08fc7 100644 --- a/service_container/service_decoration.rst +++ b/service_container/service_decoration.rst @@ -62,6 +62,20 @@ but keeps a reference of the old one as ``.inner``: .. configuration-block:: + .. code-block:: php-attributes + + // src/DecoratingMailer.php + namespace App; + + // ... + use Symfony\Component\DependencyInjection\Attribute\AsDecorator; + + #[AsDecorator(decorates: Mailer::class)] + class DecoratingMailer + { + // ... + } + .. code-block:: yaml # config/services.yaml @@ -125,6 +139,28 @@ automatically changed to ``'.inner'``): .. configuration-block:: + .. code-block:: php-attributes + + // src/DecoratingMailer.php + namespace App; + + // ... + use Symfony\Component\DependencyInjection\Attribute\AsDecorator; + use Symfony\Component\DependencyInjection\Attribute\MapDecorated; + + #[AsDecorator(decorates: Mailer::class)] + class DecoratingMailer + { + private $inner; + + public function __construct(#[MapDecorated] $inner) + { + $this->inner = $inner; + } + + // ... + } + .. code-block:: yaml # config/services.yaml @@ -249,6 +285,37 @@ the ``decoration_priority`` option. Its value is an integer that defaults to .. configuration-block:: + .. code-block:: php-attributes + + // ... + use Symfony\Component\DependencyInjection\Attribute\AsDecorator; + use Symfony\Component\DependencyInjection\Attribute\MapDecorated; + + #[AsDecorator(decorates: Foo::class, priority: 5)] + class Bar + { + private $inner; + + public function __construct(#[MapDecorated] $inner) + { + $this->inner = $inner; + } + // ... + } + + #[AsDecorator(decorates: Foo::class, priority: 1)] + class Baz + { + private $inner; + + public function __construct(#[MapDecorated] $inner) + { + $this->inner = $inner; + } + + // ... + } + .. code-block:: yaml # config/services.yaml @@ -324,6 +391,26 @@ Three different behaviors are available: .. configuration-block:: + .. code-block:: php-attributes + + // ... + use Symfony\Component\DependencyInjection\Attribute\AsDecorator; + use Symfony\Component\DependencyInjection\Attribute\MapDecorated; + use Symfony\Component\DependencyInjection\ContainerInterface; + + #[AsDecorator(decorates: Mailer::class, onInvalid: ContainerInterface::IGNORE_ON_INVALID_REFERENCE)] + class Bar + { + private $inner; + + public function __construct(#[MapDecorated] $inner) + { + $this->inner = $inner; + } + + // ... + } + .. code-block:: yaml # config/services.yaml