Skip to content

Commit 53a1966

Browse files
committed
feature #19819 [DependencyInjection] Add #[AutowireMethodOf] attribute (mttsch)
This PR was merged into the 7.1 branch. Discussion ---------- [DependencyInjection] Add `#[AutowireMethodOf]` attribute Fix #19676 <!-- If your pull request fixes a BUG, use the oldest maintained branch that contains the bug (see https://symfony.com/releases for the list of maintained branches). If your pull request documents a NEW FEATURE, use the same Symfony branch where the feature was introduced (and `7.x` for features of unreleased versions). --> Commits ------- 6e19233 [DependencyInjection] Add `#[AutowireMethodOf]` attribute
2 parents 03114e1 + 6e19233 commit 53a1966

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
@@ -738,6 +738,36 @@ attribute. By doing so, the callable will automatically be lazy, which means
738738
that the encapsulated service will be instantiated **only** at the
739739
closure's first call.
740740

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

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

0 commit comments

Comments
 (0)