Skip to content

Commit 6e19233

Browse files
mttschOskarStark
andcommitted
[DependencyInjection] Add #[AutowireMethodOf] attribute
Co-Authored-By: Oskar Stark <oskarstark@googlemail.com>
1 parent f7225d9 commit 6e19233

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

reference/attributes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Dependency Injection
3535
* :doc:`AutowireDecorated </service_container/service_decoration>`
3636
* :doc:`AutowireIterator <service-locator_autowire-iterator>`
3737
* :ref:`AutowireLocator <service-locator_autowire-locator>`
38+
* :ref:`AutowireMethodOf <autowiring_closures>`
3839
* :ref:`AutowireServiceClosure <autowiring_closures>`
3940
* :ref:`Exclude <service-psr4-loader>`
4041
* :ref:`Lazy <lazy-services_configuration>`

service_container/autowiring.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,36 @@ attribute. By doing so, the callable will automatically be lazy, which means
737737
that the encapsulated service will be instantiated **only** at the
738738
closure's first call.
739739

740+
:class:`Symfony\Component\DependencyInjection\Attribute\\AutowireMethodOf`
741+
provides a simpler way of specifying the name of the service method by using
742+
the property name as method name::
743+
744+
// src/Service/MessageGenerator.php
745+
namespace App\Service;
746+
747+
use Symfony\Component\DependencyInjection\Attribute\AutowireMethodOf;
748+
749+
class MessageGenerator
750+
{
751+
public function __construct(
752+
#[AutowireMethodOf('third_party.remote_message_formatter')]
753+
private \Closure $format,
754+
) {
755+
}
756+
757+
public function generate(string $message): void
758+
{
759+
$formattedMessage = ($this->format)($message);
760+
761+
// ...
762+
}
763+
}
764+
765+
.. versionadded:: 7.1
766+
767+
:class:`Symfony\Component\DependencyInjection\Attribute\\AutowireMethodOf`
768+
was introduced in Symfony 7.1.
769+
740770
.. _autowiring-calls:
741771

742772
Autowiring other Methods (e.g. Setters and Public Typed Properties)

0 commit comments

Comments
 (0)