Skip to content

Commit 46883da

Browse files
committed
Review from @HeahDude
1 parent 601f0bc commit 46883da

File tree

1 file changed

+38
-42
lines changed

1 file changed

+38
-42
lines changed

service_container/service_decoration.rst

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,14 @@ The generated code will be the following::
316316
Control the Behavior When the Decorated Service Does Not Exist
317317
--------------------------------------------------------------
318318

319-
Let's imagine you have a decorator that decorates a service defined by another bundle.
320-
If for some reasons, that bundle is missing, the ``decoration_on_invalid``
321-
option will allow you to handle the lack of the decorated service.
319+
When you decorate a service that doesn't exist, the ``decoration_on_invalid``
320+
option allows you to choose the behavior to adopt.
321+
322+
Three different behaviors are available:
323+
324+
* ``exception``: A ``ServiceNotFoundException`` will be thrown telling that decorator's dependency is missing. (default)
325+
* ``ignore``: The container will remove the decorator.
326+
* ``null``: The container will keep the decorator service and will set the decorated one to ``null``.
322327

323328
.. configuration-block::
324329

@@ -328,7 +333,6 @@ option will allow you to handle the lack of the decorated service.
328333
Foo: ~
329334
330335
Bar:
331-
public: false
332336
decorates: Foo
333337
decoration_on_invalid: ignore
334338
arguments: ['@Bar.inner']
@@ -365,62 +369,54 @@ option will allow you to handle the lack of the decorated service.
365369
366370
$services->set(Bar::class)
367371
->decorate(Foo::class, null, 0, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)
368-
->args([ref(Bar::class.'.inner')]);
369-
370-
$services->set(Baz::class)
371-
->private()
372-
->decorate(Foo::class, null, 1)
373-
->args([ref(Baz::class.'.inner')]);
372+
->args([ref(Bar::class.'.inner')])
373+
;
374374
};
375375
376-
Three different behaviors are available:
376+
.. versionadded:: 4.4
377377

378-
* ``exception``: When used, a ``ServiceNotFoundException``
379-
will be thrown telling that decorator's dependency is missing. (default)
380-
* ``ignore``: Using that behavior, the container will remove either decorator
381-
and decorated services.
382-
* ``null``: When used, the container will keep the decorator
383-
service and will set the decorated one to ``null``.
378+
The ``decoration_on_invalid`` option has been introduced in Symfony 4.4.
384379

385-
.. tip::
380+
.. caution::
386381

387-
When using ``null``, you may update the decorator's constructor in
388-
order to make decorated dependency nullable. In this way, you will be able to
389-
check the presence of the decorated service in other methods.
382+
When using ``null``, you may have to update the decorator constructor in
383+
order to make decorated dependency nullable.
390384

391-
.. code-block:: yaml
385+
.. configuration-block::
392386

393-
App\Service\DecoratorService:
394-
decorates: Acme\OptionalBundle\Service\OptionalService
395-
decoration_on_invalid: null
396-
arguments: ['@App\Service\DecoratorService.inner']
387+
.. code-block:: yaml
397388
398-
.. code-block: php
389+
App\Service\DecoratorService:
390+
decorates: Acme\OptionalBundle\Service\OptionalService
391+
decoration_on_invalid: null
392+
arguments: ['@App\Service\DecoratorService.inner']
399393
400-
namespace App\Service;
394+
.. code-block:: php
401395
402-
use Acme\OptionalBundle\Service\OptionalService;
396+
namespace App\Service;
403397
404-
class DecoratorService
405-
{
406-
private $decorated;
398+
use Acme\OptionalBundle\Service\OptionalService;
407399
408-
public function __construct(?OptionalService $decorated)
400+
class DecoratorService
409401
{
410-
$this->decorated = $decorated;
411-
}
402+
private $decorated;
412403
413-
public function tellInterestingStuff(): string
414-
{
415-
if (!$this->decorated) {
416-
return 'Just one interesting thing';
404+
public function __construct(?OptionalService $decorated)
405+
{
406+
$this->decorated = $decorated;
417407
}
418408
419-
return $this->decorated->tellInterestingStuff().' + one more interesting thing';
409+
public function tellInterestingStuff(): string
410+
{
411+
if (!$this->decorated) {
412+
return 'Just one interesting thing';
413+
}
414+
415+
return $this->decorated->tellInterestingStuff().' + one more interesting thing';
416+
}
420417
}
421-
}
422418
423-
.. tip::
419+
.. note::
424420

425421
Sometimes, you may want to add a compiler pass that creates service
426422
definitions on the fly. If you want to decorate such a service,

0 commit comments

Comments
 (0)