Skip to content

Conversation

@X-Coder264
Copy link
Contributor

@X-Coder264 X-Coder264 commented Dec 25, 2018

Currently the container immediately resolves all services which are tagged with a tag. The resolving of a service from the container doesn't need to happen until the service is actually needed so by deferring the resolving of the service until it's actually needed we potentially don't have to instantiate a bunch of objects which are never gonna be used.

public function __construct(iterable $taggedServices)
{
   $this->taggedServices = $taggedServices;
}

public function foo()
{
    foreach($this->taggedServices as $service) {
        $service->doSomething();
        if (someTrueCondition) {
            break;
        }
    }
}

Here only the first service would be resolved from the container as opposed to the current behavior where all services (potentially hundreds) would be resolved immediately.

Breaking changes:

  1. The container tagged method now returns an iterable instead of an explicit array which means that doing $container->tagged('foo')[0] is not possible anymore (but I don't see why would somebody use that in a real application).
  2. If somebody type-hinted the tagged services variable with array it will now break, either the array type-hint should be removed or it should be replaced with iterable.

@taylorotwell taylorotwell merged commit 32ac2c7 into laravel:master Jan 10, 2019
@X-Coder264 X-Coder264 deleted the container-lazy-load-tagged-services branch January 10, 2019 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants