Skip to content

[DependencyInjection] Add #[Lazy] attribute #19506

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
Mar 21, 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 @@ -37,6 +37,7 @@ Dependency Injection
* :ref:`AutowireLocator <service-locator_autowire-locator>`
* :ref:`AutowireServiceClosure <autowiring_closures>`
* :ref:`Exclude <service-psr4-loader>`
* :ref:`Lazy <lazy-services_configuration>`
* :ref:`TaggedIterator <tags_reference-tagged-services>`
* :ref:`TaggedLocator <service-subscribers-locators_defining-service-locator>`
* :ref:`Target <autowiring-multiple-implementations-same-type>`
Expand Down
26 changes: 26 additions & 0 deletions service_container/lazy_services.rst
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,32 @@ laziness, and supports lazy-autowiring of intersection types::
) {
}

Another possibility is to use the :class:`Symfony\\Component\\DependencyInjection\\Attribute\\Lazy` attribute::

namespace App\Twig;

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

#[Lazy]
class AppExtension implements ExtensionInterface
{
// ...
}

This attribute can be used on a class or on a parameter which should be lazy-loaded, and has a parameter
that also supports defining interfaces to proxy and intersection types::

public function __construct(
#[Lazy(FooInterface::class)]
FooInterface|BarInterface $foo,
) {
}

.. versionadded:: 7.1

The ``#[Lazy]`` attribute was introduced in Symfony 7.1.

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

Expand Down