@@ -643,6 +643,104 @@ The ``#[Autowire]`` attribute can also be used for :ref:`parameters <service-par
643643
644644 The ``param `` and ``env `` arguments were introduced in Symfony 6.3.
645645
646+ .. _autowiring_closures :
647+
648+ Generate Closures With Autowiring
649+ ---------------------------------
650+
651+ A **service closure ** is an anonymous function that returns a service. This type
652+ of instanciation is handy when you are dealing with lazy-loading.
653+
654+ Automatically creating a closure encapsulating the service instanciation can be
655+ done with the
656+ :class: `Symfony\\ Component\\ DependencyInjection\\ Attribute\\ AutowireServiceClosure `
657+ attribute::
658+
659+ // src/Service/Remote/MessageFormatter.php
660+ namespace App\Service\Remote;
661+
662+ use Symfony\Component\DependencyInjection\Attribute\AsAlias;
663+
664+ #[AsAlias('third_party.remote_message_formatter')]
665+ class MessageFormatter
666+ {
667+ public function __construct()
668+ {
669+ // ...
670+ }
671+
672+ public function format(string $message): string
673+ {
674+ // ...
675+ }
676+ }
677+
678+ // src/Service/MessageGenerator.php
679+ namespace App\Service;
680+
681+ use App\Service\Remote\MessageFormatter;
682+ use Symfony\Component\DependencyInjection\Attribute\AutowireServiceClosure;
683+
684+ class MessageGenerator
685+ {
686+ public function __construct(
687+ #[AutowireServiceClosure('third_party.remote_message_formatter')]
688+ \Closure $messageFormatterResolver
689+ ) {
690+ }
691+
692+ public function generate(string $message): void
693+ {
694+ $formattedMessage = ($this->messageFormatterResolver)()->format($message);
695+
696+ // ...
697+ }
698+ }
699+
700+ .. versionadded :: 6.3
701+
702+ The :class: `Symfony\\ Component\\ DependencyInjection\\ Attribute\\ AutowireServiceClosure `
703+ attribute was introduced in Symfony 6.3.
704+
705+ It is common that a service accepts a closure with a specific signature.
706+ In this case, you can use the
707+ :class: `Symfony\C omponent\D ependencyInjection\A ttribute\\ AutowireCallable ` attribute
708+ to generate a closure with the same signature as a specific method of a service. When
709+ this closure is called, it will pass all its arguments to the underlying service
710+ function::
711+
712+ // src/Service/MessageGenerator.php
713+ namespace App\Service;
714+
715+ use Symfony\Component\DependencyInjection\Attribute\AutowireCallable;
716+
717+ class MessageGenerator
718+ {
719+ public function __construct(
720+ #[AutowireCallable(service: 'third_party.remote_message_formatter', method: 'format')]
721+ \Closure $formatCallable
722+ ) {
723+ }
724+
725+ public function generate(string $message): void
726+ {
727+ $formattedMessage = ($this->formatCallable)($message);
728+
729+ // ...
730+ }
731+ }
732+
733+ Finally, you can pass the ``lazy: true `` option to the
734+ :class: `Symfony\C omponent\D ependencyInjection\A ttribute\\ AutowireCallable `
735+ attribute. By doing so, the callable will automatically be lazy, which means
736+ that the encapsulated service will be instantiated **only ** at the
737+ closure's first call.
738+
739+ .. versionadded :: 6.3
740+
741+ The :class: `Symfony\\ Component\\ DependencyInjection\\ Attribute\\ AutowireCallable `
742+ attribute was introduced in Symfony 6.3.
743+
646744.. _autowiring-calls :
647745
648746Autowiring other Methods (e.g. Setters and Public Typed Properties)
0 commit comments