Skip to content

Commit

Permalink
minor symfony#19506 [DependencyInjection] Add #[Lazy] attribute (Ti…
Browse files Browse the repository at this point in the history
…riel)

This PR was squashed before being merged into the 7.1 branch.

Discussion
----------

[DependencyInjection] Add `#[Lazy]` attribute

Fixes symfony#19486
Documentation for the new `#[Lazy]` attribute.

Commits
-------

6f05767 [DependencyInjection] Add `#[Lazy]` attribute
  • Loading branch information
javiereguiluz committed Mar 21, 2024
2 parents 4f7b018 + 6f05767 commit d1052b2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
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 union 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

0 comments on commit d1052b2

Please sign in to comment.