8000 [DependencyInjection] Add as decorator and inner service by Jean-Beru · Pull Request #16746 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection] Add as decorator and inner service #16746

8000 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
< 8000 form data-turbo="false" action="/symfony/symfony-docs/pull/16746/diffview" accept-charset="UTF-8" method="post">
Diff view
[DependencyInjection] Add as decorator and inner service
  • Loading branch information
Jean-Beru authored and javiereguiluz committed Apr 26, 2022
commit 555f7f35a3cd6f377f433d60328f88f24430c138
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
0