@@ -316,9 +316,14 @@ The generated code will be the following::
316
316
Control the Behavior When the Decorated Service Does Not Exist
317
317
--------------------------------------------------------------
318
318
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 ``.
322
327
323
328
.. configuration-block ::
324
329
@@ -328,7 +333,6 @@ option will allow you to handle the lack of the decorated service.
328
333
Foo : ~
329
334
330
335
Bar :
331
- public : false
332
336
decorates : Foo
333
337
decoration_on_invalid : ignore
334
338
arguments : ['@Bar.inner']
@@ -365,62 +369,54 @@ option will allow you to handle the lack of the decorated service.
365
369
366
370
$services->set(Bar::class)
367
371
->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
+ ;
374
374
};
375
375
376
- Three different behaviors are available:
376
+ .. versionadded :: 4.4
377
377
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.
384
379
385
- .. tip ::
380
+ .. caution ::
386
381
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.
390
384
391
- .. code -block :: yaml
385
+ .. configuration -block ::
392
386
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
397
388
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']
399
393
400
- namespace App\Service;
394
+ .. code-block :: php
401
395
402
- use Acme\OptionalBundle\ Service\OptionalService ;
396
+ namespace App\ Service;
403
397
404
- class DecoratorService
405
- {
406
- private $decorated;
398
+ use Acme\OptionalBundle\Service\OptionalService;
407
399
408
- public function __construct(?OptionalService $decorated)
400
+ class DecoratorService
409
401
{
410
- $this->decorated = $decorated;
411
- }
402
+ private $decorated;
412
403
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;
417
407
}
418
408
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
+ }
420
417
}
421
- }
422
418
423
- .. tip ::
419
+ .. note ::
424
420
425
421
Sometimes, you may want to add a compiler pass that creates service
426
422
definitions on the fly. If you want to decorate such a service,
0 commit comments