8000 [DI] Add "lazy collection" type · Issue #20875 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
[DI] Add "lazy collection" type #20875
Closed
Closed
@nicolas-grekas

Description

@nicolas-grekas

I was looking at #12521 then #20039. Both deal with lazyness in the event dispatcher.
The issue is having event listeners/dispatchers be instantiated only when actually needed.
ContainerAwareEventDispatcher partially works around the issue, but in a non generic way (bound to Symfony's DI container and not compatible with private services).

But let's imagine we add a new lazy collection type to our lovely DI component, usable this way:

<service id="foo" class="Foo">
    <argument type="iterator">
        <argument type="service" id="bar" />
        <argument type="service" id="buz" />
        <argument key="key" type="service" id="zob" />
    </argument>
</service>

Generating this code when compiled (simplified example):

protected function getFooService()
{
    return new Foo((function () {
        yield $this->get('bar');
        yield $this->get('buz');
        yield 'key' => $this->get('zob');
    })());
}

Then the referenced services would be instantiated only when the generator reaches the corresponding yield statement. This would allow building e.g. a generic lazy event dispatcher.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DependencyInjectionRFCRFC = Request For Comments (proposals about features that you want to be discussed)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0