Skip to content

[DependencyInjection] Add support for #[Autowire(lazy: true)] #49685

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 20, 2023

Conversation

nicolas-grekas
Copy link
Member

@nicolas-grekas nicolas-grekas commented Mar 14, 2023

Q A
Branch? 6.3
Bug fix? no
New feature? yes
Deprecations? no
Tickets -
License MIT
Doc PR -

This is a feature that I anticipated during my talk at SymfonyCon DisneyLand.

This PR adds support for using #[Autowire(lazy: true)] on an argument to tell the container to generate a lazy service and pass it to that argument.

This works by creating a lazy service from the autowiring alias: if the argument targets FooInterface, a lazy service is declared (named .lazy.FooInterface) and injected. The original FooInterface remains unchanged, aka non-lazy.

This allows services that don't always consume their deps to initialize them only when actually needed.

Copy link
Member

@fabpot fabpot left a comment

Choose a reason for hiding this comment

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

Minor comments

@fabpot
Copy link
Member

fabpot commented Mar 19, 2023

Needs a rebase.

@nicolas-grekas
Copy link
Member Author

Rebased + comments addressed, thanks.

@fabpot
Copy link
Member

fabpot commented Mar 20, 2023

Thank you @nicolas-grekas.

@fabpot fabpot merged commit cdb18ad into symfony:6.3 Mar 20, 2023
nicolas-grekas added a commit that referenced this pull request Mar 28, 2023
…: class-string)]` (nicolas-grekas)

This PR was merged into the 6.3 branch.

Discussion
----------

[DependencyInjection] Add support for `#[Autowire(lazy: class-string)]`

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

This PR finishes what started in #49685.

It adds support for defining the interfaces to proxy when using laziness with the Autowire attribute.

```php
    public function __construct(
        #[Autowire(service: 'foo', lazy: FooInterface::class)]
        FooInterface|BarInterface $foo,
    ) {
    }
```

It also adds support for lazy-autowiring of intersection types:

```php
    public function __construct(
        #[Autowire(service: 'foobar', lazy: true)]
        FooInterface&BarInterface $foobar,
    ) {
    }
```

Commits
-------

d127ebf [DependencyInjection] Add support for `#[Autowire(lazy: class-string)]`
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.

3 participants