From 6f05767d02593ebf2d47cfced08fabee1e0763b3 Mon Sep 17 00:00:00 2001 From: Benjamin Zaslavsky Date: Thu, 25 Jan 2024 09:54:48 +0100 Subject: [PATCH] [DependencyInjection] Add `#[Lazy]` attribute --- reference/attributes.rst | 1 + service_container/lazy_services.rst | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/reference/attributes.rst b/reference/attributes.rst index 08667e2a06f..015b8751834 100644 --- a/reference/attributes.rst +++ b/reference/attributes.rst @@ -37,6 +37,7 @@ Dependency Injection * :ref:`AutowireLocator ` * :ref:`AutowireServiceClosure ` * :ref:`Exclude ` +* :ref:`Lazy ` * :ref:`TaggedIterator ` * :ref:`TaggedLocator ` * :ref:`Target ` diff --git a/service_container/lazy_services.rst b/service_container/lazy_services.rst index 46e939d4fac..4d21837e4ae 100644 --- a/service_container/lazy_services.rst +++ b/service_container/lazy_services.rst @@ -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 --------------------