|
| 1 | +.. index:: |
| 2 | + single: DependencyInjection; Service Closures |
| 3 | + |
| 4 | +Service Closures |
| 5 | +================ |
| 6 | + |
| 7 | +This feature wraps the injected service into a closure allowing it to be |
| 8 | +lazily loaded when and if needed. |
| 9 | +This is useful if the service being injected is a bit heavy to instantiate |
| 10 | +or is used only in certain cases. |
| 11 | +The service is instantiated the first time the closure is called, while |
| 12 | +all subsequent calls return the same instance, unless the service is |
| 13 | +:doc:`not shared </service_container/shared>`:: |
| 14 | + |
| 15 | + // src/Service/MyService.php |
| 16 | + namespace App\Service; |
| 17 | + |
| 18 | + use Symfony\Component\Mailer\MailerInterface; |
| 19 | + |
| 20 | + class MyService |
| 21 | + { |
| 22 | + /** |
| 23 | + * @var callable(): MailerInterface |
| 24 | + */ |
| 25 | + private \Closure $mailer; |
| 26 | + |
| 27 | + public function __construct(\Closure $mailer) |
| 28 | + { |
| 29 | + $this->mailer = $mailer; |
| 30 | + } |
| 31 | + |
| 32 | + public function doSomething(): void |
| 33 | + { |
| 34 | + // ... |
| 35 | + |
| 36 | + ($this->mailer)()->send($email); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | +To define a service closure and inject it to another service, create an |
| 41 | +argument of type ``service_closure``: |
| 42 | + |
| 43 | +.. configuration-block:: |
| 44 | + |
| 45 | + .. code-block:: yaml |
| 46 | +
|
| 47 | + # config/services.yaml |
| 48 | + services: |
| 49 | + App\Service\MyService: |
| 50 | + arguments: [!service_closure '@mailer'] |
| 51 | +
|
| 52 | + .. code-block:: xml |
| 53 | +
|
| 54 | + <!-- config/services.xml --> |
| 55 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 56 | + <container xmlns="http://symfony.com/schema/dic/services" |
| 57 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 58 | + xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd"> |
| 59 | +
|
| 60 | + <services> |
| 61 | + <service id="App\Service\MyService"> |
| 62 | + <argument type="service_closure" id="mailer"/> |
| 63 | + </service> |
| 64 | + </services> |
| 65 | + </container> |
| 66 | +
|
| 67 | + .. code-block:: php |
| 68 | +
|
| 69 | + // config/services.php |
| 70 | + namespace Symfony\Component\DependencyInjection\Loader\Configurator; |
| 71 | +
|
| 72 | + use App\Service\MyService; |
| 73 | + use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument; |
| 74 | + use Symfony\Component\DependencyInjection\Reference; |
| 75 | +
|
| 76 | + return function (ContainerConfigurator $configurator) { |
| 77 | + $services = $configurator->services(); |
| 78 | +
|
| 79 | + $services->set(MyService::class) |
| 80 | + ->args([new ServiceClosureArgument(new Reference('mailer'))]); |
| 81 | + }; |
| 82 | +
|
| 83 | +.. seealso:: |
| 84 | + |
| 85 | + Another way to inject services lazily is via a |
| 86 | + :doc:`service locators </service_container/service_subscribers_locators>`. |
0 commit comments