Skip to content

[DependencyInjection] Add Lazy attribute for classes and arguments #52922

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
Feb 3, 2024

Conversation

Tiriel
Copy link
Contributor

@Tiriel Tiriel commented Dec 7, 2023

Q A
Branch? 7.1
Bug fix? no
New feature? yes
Deprecations? no
Issues #
License MIT

This commit adds a Lazy attribute shortcut for service definitions. The goal here is simply to improve the DX with a single attribute for lazy loaded services, while adding the strict minimum amount of new code.
This attribute can be used as a replacement for the Autowire(lazy: bool|string) attribute on any autowired argument or for the Autoconfigure(lazy: bool|string) attribute on an class definition. Both use case support an optional parameter to specify with class/interface should be used for the proxy.

Usage on a class:

use Symfony\Component\DependencyInjection\Attribute\Lazy;

#[Lazy]
class LazyLoaded
{
    // ...
}

On a method argument:

use Symfony\Component\DependencyInjection\Attribute\Lazy;

class SuperAwesomeService
{
    public function __construct(#[Lazy] SomeOtherService $service)
    {
        // ...
    }
}

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This made me wonder whether this blurs any lines but I think I'm good with the proposal.
The implementation should be updated to forbid using both Lazy and Autowire since on the attribute is not repeatable.

@Tiriel
Copy link
Contributor Author

Tiriel commented Jan 8, 2024

The implementation should be updated to forbid using both Lazy and Autowire since on the attribute is not repeatable.

Going to work on this asap, thanks!

@Tiriel Tiriel force-pushed the lazy-attribute branch 2 times, most recently from 25cc0f4 to 091395a Compare January 11, 2024 16:56
@Tiriel Tiriel requested a review from nicolas-grekas January 16, 2024 15:03
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but CS, see also fabbot

@fabpot
Copy link
Member

fabpot commented Feb 3, 2024

Thank you @Tiriel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants