diff --git a/reference/attributes.rst b/reference/attributes.rst index 015b8751834..a1e8532c347 100644 --- a/reference/attributes.rst +++ b/reference/attributes.rst @@ -35,6 +35,7 @@ Dependency Injection * :doc:`AutowireDecorated ` * :doc:`AutowireIterator ` * :ref:`AutowireLocator ` +* :ref:`AutowireMethodOf ` * :ref:`AutowireServiceClosure ` * :ref:`Exclude ` * :ref:`Lazy ` diff --git a/service_container/autowiring.rst b/service_container/autowiring.rst index 3c7a2eba52b..8b2b1c4d51d 100644 --- a/service_container/autowiring.rst +++ b/service_container/autowiring.rst @@ -737,6 +737,36 @@ attribute. By doing so, the callable will automatically be lazy, which means that the encapsulated service will be instantiated **only** at the closure's first call. +:class:`Symfony\Component\DependencyInjection\Attribute\\AutowireMethodOf` +provides a simpler way of specifying the name of the service method by using +the property name as method name:: + + // src/Service/MessageGenerator.php + namespace App\Service; + + use Symfony\Component\DependencyInjection\Attribute\AutowireMethodOf; + + class MessageGenerator + { + public function __construct( + #[AutowireMethodOf('third_party.remote_message_formatter')] + private \Closure $format, + ) { + } + + public function generate(string $message): void + { + $formattedMessage = ($this->format)($message); + + // ... + } + } + +.. versionadded:: 7.1 + + :class:`Symfony\Component\DependencyInjection\Attribute\\AutowireMethodOf` + was introduced in Symfony 7.1. + .. _autowiring-calls: Autowiring other Methods (e.g. Setters and Public Typed Properties)