Skip to content

[DependencyInjection] Add as decorator and inner service #16746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions service_container/service_decoration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down