Skip to content

[DependencyInjection] [LazyServices] Add #[Autoconfigure] attribute mention #17784

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
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
43 changes: 43 additions & 0 deletions service_container/lazy_services.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ In order to use the lazy service instantiation, you will need to install the

$ composer require symfony/proxy-manager-bridge

.. _lazy-services_configuration:

Configuration
-------------

Expand Down Expand Up @@ -101,6 +103,26 @@ To check if your proxy works you can check the interface of the received object:
over the ``lazy`` flag and directly instantiate the service as it would
normally do.

You can also configure your service's laziness thanks to the
:class:`Symfony\\Component\\DependencyInjection\\Attribute\\Autoconfigure` attribute.
The attribute has to be used like this::

namespace App\Twig;

use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
use Twig\Extension\ExtensionInterface;

#[Autoconfigure(lazy: true)]
class AppExtension implements ExtensionInterface
{
// ...
}

.. versionadded:: 5.4

The :class:`Symfony\\Component\\DependencyInjection\\Attribute\\Autoconfigure` attribute
was introduced in Symfony 5.4.

Interface Proxifying
--------------------

Expand Down Expand Up @@ -159,6 +181,27 @@ specific interfaces.
;
};

Just like in the :ref:`Configuration <lazy-services_configuration>` section, you can
use the :class:`Symfony\\Component\\DependencyInjection\\Attribute\\Autoconfigure`
attribute to configure the interface to proxify by passing its FQCN as the ``lazy``
parameter value::

namespace App\Twig;

use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
use Twig\Extension\ExtensionInterface;

#[Autoconfigure(lazy: ExtensionInterface::class)]
class AppExtension implements ExtensionInterface
{
// ...
}

.. versionadded:: 5.4

The :class:`Symfony\\Component\\DependencyInjection\\Attribute\\Autoconfigure` attribute
was introduced in Symfony 5.4.

The virtual `proxy`_ injected into other services will only implement the
specified interfaces and will not extend the original service class, allowing to
lazy load services using `final`_ classes. You can configure the proxy to
Expand Down