Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1cd54be

Browse files
committed
minor #16746 [DependencyInjection] Add as decorator and inner service (Jean-Beru)
This PR was squashed before being merged into the 6.1 branch. Discussion ---------- [DependencyInjection] Add as decorator and inner service Issue #16731 Commits ------- 555f7f3 [DependencyInjection] Add as decorator and inner service
2 parents 7f1fc25 + 555f7f3 commit 1cd54be

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

service_container/service_decoration.rst

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@ but keeps a reference of the old one as ``.inner``:
6262

6363
.. configuration-block::
6464

65+
.. code-block:: php-attributes
66+
67+
// src/DecoratingMailer.php
68+
namespace App;
69+
70+
// ...
71+
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
72+
73+
#[AsDecorator(decorates: Mailer::class)]
74+
class DecoratingMailer
75+
{
76+
// ...
77+
}
78+
6579
.. code-block:: yaml
6680
6781
# config/services.yaml
@@ -125,6 +139,28 @@ automatically changed to ``'.inner'``):
125139

126140
.. configuration-block::
127141

142+
.. code-block:: php-attributes
143+
144+
// src/DecoratingMailer.php
145+
namespace App;
146+
147+
// ...
148+
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
149+
use Symfony\Component\DependencyInjection\Attribute\MapDecorated;
150+
151+
#[AsDecorator(decorates: Mailer::class)]
152+
class DecoratingMailer
153+
{
154+
private $inner;
155+
156+
public function __construct(#[MapDecorated] $inner)
157+
{
158+
$this->inner = $inner;
159+
}
160+
161+
// ...
162+
}
163+
128164
.. code-block:: yaml
129165
130166
# config/services.yaml
@@ -249,6 +285,37 @@ the ``decoration_priority`` option. Its value is an integer that defaults to
249285

250286
.. configuration-block::
251287

288+
.. code-block:: php-attributes
289+
290+
// ...
291+
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
292+
use Symfony\Component\DependencyInjection\Attribute\MapDecorated;
293+
294+
#[AsDecorator(decorates: Foo::class, priority: 5)]
295+
class Bar
296+
{
297+
private $inner;
298+
299+
public function __construct(#[MapDecorated] $inner)
300+
{
301+
$this->inner = $inner;
302+
}
303+
// ...
304+
}
305+
306+
#[AsDecorator(decorates: Foo::class, priority: 1)]
307+
class Baz
308+
{
309+
private $inner;
310+
311+
public function __construct(#[MapDecorated] $inner)
312+
{
313+
$this->inner = $inner;
314+
}
315+
316+
// ...
317+
}
318+
252319
.. code-block:: yaml
253320
254321
# config/services.yaml
@@ -324,6 +391,26 @@ Three different behaviors are available:
324391

325392
.. configuration-block::
326393

394+
.. code-block:: php-attributes
395+
396+
// ...
397+
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
398+
use Symfony\Component\DependencyInjection\Attribute\MapDecorated;
399+
use Symfony\Component\DependencyInjection\ContainerInterface;
400+
401+
#[AsDecorator(decorates: Mailer::class, onInvalid: ContainerInterface::IGNORE_ON_INVALID_REFERENCE)]
402+
class Bar
403+
{
404+
private $inner;
405+
406+
public function __construct(#[MapDecorated] $inner)
407+
{
408+
$this->inner = $inner;
409+
}
410+
411+
// ...
412+
}
413+
327414
.. code-block:: yaml
328415
329416
# config/services.yaml

0 commit comments

Comments
 (0)