Skip to content

[DependencyInjection] Add #[AutowireMethodOf] attribute #19819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions reference/attributes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Dependency Injection
* :doc:`AutowireDecorated </service_container/service_decoration>`
* :doc:`AutowireIterator <service-locator_autowire-iterator>`
* :ref:`AutowireLocator <service-locator_autowire-locator>`
* :ref:`AutowireMethodOf <autowiring_closures>`
* :ref:`AutowireServiceClosure <autowiring_closures>`
* :ref:`Exclude <service-psr4-loader>`
* :ref:`Lazy <lazy-services_configuration>`
Expand Down
30 changes: 30 additions & 0 deletions service_container/autowiring.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading