-
Notifications
You must be signed in to change notification settings - Fork 11.4k
[13.x] RFC: Lazy Services - New #[Lazy] Attribute #55645
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
Draft
olekjs
wants to merge
2
commits into
laravel:master
Choose a base branch
from
olekjs:lazy-services
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Illuminate\Container\Attributes; | ||
|
||
use Attribute; | ||
|
||
#[Attribute] | ||
class Lazy | ||
{ | ||
// | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -5,6 +5,7 @@ | |||||||||
use ArrayAccess; | ||||||||||
use Closure; | ||||||||||
use Exception; | ||||||||||
use Illuminate\Container\Attributes\Lazy; | ||||||||||
use Illuminate\Contracts\Container\BindingResolutionException; | ||||||||||
use Illuminate\Contracts\Container\CircularDependencyException; | ||||||||||
use Illuminate\Contracts\Container\Container as ContainerContract; | ||||||||||
|
@@ -812,13 +813,14 @@ public function makeWith($abstract, array $parameters = []) | |||||||||
* | ||||||||||
* @param string|class-string<TClass> $abstract | ||||||||||
* @param array $parameters | ||||||||||
* @param ReflectionAttribute<TClass>[] $attributes | ||||||||||
* @return ($abstract is class-string<TClass> ? TClass : mixed) | ||||||||||
* | ||||||||||
* @throws \Illuminate\Contracts\Container\BindingResolutionException | ||||||||||
*/ | ||||||||||
public function make($abstract, array $parameters = []) | ||||||||||
public function make($abstract, array $parameters = [], array $attributes = []) | ||||||||||
{ | ||||||||||
return $this->resolve($abstract, $parameters); | ||||||||||
return $this->resolve($abstract, $parameters, attributes: $attributes); | ||||||||||
} | ||||||||||
|
||||||||||
/** | ||||||||||
|
@@ -850,12 +852,14 @@ public function get(string $id) | |||||||||
* @param string|class-string<TClass>|callable $abstract | ||||||||||
* @param array $parameters | ||||||||||
* @param bool $raiseEvents | ||||||||||
* @param ReflectionAttribute<TClass>[] $attributes | ||||||||||
* | ||||||||||
* @return ($abstract is class-string<TClass> ? TClass : mixed) | ||||||||||
* | ||||||||||
* @throws \Illuminate\Contracts\Container\BindingResolutionException | ||||||||||
* @throws \Illuminate\Contracts\Container\CircularDependencyException | ||||||||||
*/ | ||||||||||
protected function resolve($abstract, $parameters = [], $raiseEvents = true) | ||||||||||
protected function resolve($abstract, $parameters = [], $raiseEvents = true, array $attributes = []) | ||||||||||
{ | ||||||||||
$abstract = $this->getAlias($abstract); | ||||||||||
|
||||||||||
|
@@ -887,7 +891,7 @@ protected function resolve($abstract, $parameters = [], $raiseEvents = true) | |||||||||
// the binding. This will instantiate the types, as well as resolve any of | ||||||||||
// its "nested" dependencies recursively until all have gotten resolved. | ||||||||||
$object = $this->isBuildable($concrete, $abstract) | ||||||||||
? $this->build($concrete) | ||||||||||
? $this->build($concrete, $attributes) | ||||||||||
: $this->make($concrete); | ||||||||||
|
||||||||||
// If we defined any extenders for this type, we'll need to spin through them | ||||||||||
|
@@ -993,12 +997,13 @@ protected function isBuildable($concrete, $abstract) | |||||||||
* @template TClass of object | ||||||||||
* | ||||||||||
* @param \Closure(static, array): TClass|class-string<TClass> $concrete | ||||||||||
* @param ReflectionAttribute<TClass>[] $attributes | ||||||||||
* @return TClass | ||||||||||
* | ||||||||||
* @throws \Illuminate\Contracts\Container\BindingResolutionException | ||||||||||
* @throws \Illuminate\Contracts\Container\CircularDependencyException | ||||||||||
*/ | ||||||||||
public function build($concrete) | ||||||||||
public function build($concrete, array $attributes = []) | ||||||||||
{ | ||||||||||
// If the concrete type is actually a Closure, we will just execute it and | ||||||||||
// hand back the results of the functions, which allows functions to be | ||||||||||
|
@@ -1058,13 +1063,39 @@ public function build($concrete) | |||||||||
|
||||||||||
array_pop($this->buildStack); | ||||||||||
|
||||||||||
$isLazy = $this->isLazy(array_merge($attributes, $reflector->getAttributes(Lazy::class))); | ||||||||||
|
||||||||||
if ($isLazy) { | ||||||||||
$instance = $reflector->newLazyProxy(function () use ($concrete, $instances) { | ||||||||||
return new $concrete(...$instances); | ||||||||||
}); | ||||||||||
Comment on lines
+1069
to
+1071
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could do just
Suggested change
|
||||||||||
} else { | ||||||||||
$instance = $reflector->newInstanceArgs($instances); | ||||||||||
} | ||||||||||
|
||||||||||
$this->fireAfterResolvingAttributeCallbacks( | ||||||||||
$reflector->getAttributes(), $instance = $reflector->newInstanceArgs($instances) | ||||||||||
$reflector->getAttributes(), $instance | ||||||||||
); | ||||||||||
|
||||||||||
return $instance; | ||||||||||
} | ||||||||||
|
||||||||||
/** | ||||||||||
* @template TClass of object | ||||||||||
* | ||||||||||
* @param ReflectionAttribute<TClass>[] $attributes | ||||||||||
* | ||||||||||
* @return bool | ||||||||||
*/ | ||||||||||
protected function isLazy(array $attributes): bool | ||||||||||
{ | ||||||||||
if (empty($attributes)) { | ||||||||||
return false; | ||||||||||
} | ||||||||||
|
||||||||||
return array_any($attributes, static fn($attribute) => $attribute->getName() === Lazy::class); | ||||||||||
} | ||||||||||
|
||||||||||
/** | ||||||||||
* Resolve all of the dependencies from the ReflectionParameters. | ||||||||||
* | ||||||||||
|
@@ -1199,7 +1230,7 @@ protected function resolveClass(ReflectionParameter $parameter) | |||||||||
try { | ||||||||||
return $parameter->isVariadic() | ||||||||||
? $this->resolveVariadicClass($parameter) | ||||||||||
: $this->make($className); | ||||||||||
: $this->make($className, $parameter->getAttributes(), $parameter->getAttributes()); | ||||||||||
} | ||||||||||
|
||||||||||
// If we can not resolve the class instance, we will check to see if the value | ||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Container; | ||
|
||
use Illuminate\Container\Attributes\Lazy; | ||
use Illuminate\Container\Container; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ContainerLazyTest extends TestCase | ||
{ | ||
protected function tearDown(): void | ||
{ | ||
Container::setInstance(); | ||
} | ||
|
||
public function testConcreteDoesNotThrowsExceptionWithAttribute() | ||
{ | ||
$container = new Container; | ||
$lazy = $container->make(LazyWithAttributeStub::class); | ||
|
||
// No RuntimeException has occurred | ||
// LazyWithAttributeStub behaves like a Lazy Object, but this is not obvious from its type | ||
$this->assertInstanceOf(LazyWithAttributeStub::class, $lazy); | ||
} | ||
|
||
public function testConcreteThrowsExceptionWithoutAttribute() | ||
{ | ||
$this->expectException(\RuntimeException::class); | ||
$this->expectExceptionMessage('Lazy call'); | ||
|
||
$container = new Container; | ||
$container->make(LazyWithoutAttributeStub::class); | ||
} | ||
|
||
public function testConcreteDoesNotThrowsExceptionWithNoLogicConstructor() | ||
{ | ||
$container = new Container; | ||
$lazy = $container->make(LazyWithAttributeStub::class); | ||
|
||
$this->assertInstanceOf(LazyWithAttributeStub::class, $lazy); | ||
|
||
$this->assertSame('work', $lazy->work()); | ||
} | ||
|
||
public function testConcreteDoesThrowsExceptionWithConstructorWithLogic() | ||
{ | ||
$container = new Container; | ||
$lazy = $container->make(LazyWithAttributeLogicStub::class); | ||
|
||
// No RuntimeException has occurred so far | ||
$this->assertInstanceOf(LazyWithAttributeLogicStub::class, $lazy); | ||
|
||
$this->expectException(\RuntimeException::class); | ||
$this->expectExceptionMessage('Lazy call'); | ||
|
||
// Only the call to number() causes a RuntimeException | ||
$lazy->number(); | ||
} | ||
|
||
public function testConcreteThrowsExceptionButNotLazyDependency() | ||
{ | ||
$this->expectException(\RuntimeException::class); | ||
$this->expectExceptionMessage('Parent call'); | ||
|
||
$container = new Container; | ||
$container->make(LazyDependencyWithAttributeStub::class); | ||
} | ||
|
||
public function testConcreteNotLazyDependencyThrowsException() | ||
{ | ||
$this->expectException(\RuntimeException::class); | ||
$this->expectExceptionMessage('Lazy call'); | ||
|
||
$container = new Container; | ||
$container->make(LazyDependencyWithoutAttributeStub::class); | ||
} | ||
} | ||
|
||
#[Lazy] | ||
class LazyWithAttributeStub | ||
{ | ||
public function __construct() | ||
{ | ||
throw new \RuntimeException('Lazy call'); | ||
} | ||
|
||
public function work() | ||
{ | ||
return 'work'; | ||
} | ||
} | ||
|
||
class LazyWithTestRenameAttributeStub | ||
{ | ||
public function __construct() | ||
{ | ||
throw new \RuntimeException('Lazy call'); | ||
} | ||
|
||
public function work() | ||
{ | ||
return 'work'; | ||
} | ||
} | ||
|
||
#[Lazy] | ||
class LazyWithAttributeLogicStub | ||
{ | ||
public $number; | ||
|
||
public function __construct() | ||
{ | ||
$this->number = 10; | ||
|
||
throw new \RuntimeException('Lazy call'); | ||
} | ||
|
||
public function number() | ||
{ | ||
$this->number += 10; | ||
} | ||
} | ||
|
||
class LazyWithoutAttributeStub | ||
{ | ||
public function __construct() | ||
{ | ||
throw new \RuntimeException('Lazy call'); | ||
} | ||
} | ||
|
||
class LazyDependencyWithAttributeStub | ||
{ | ||
public function __construct(#[Lazy] LazyWithTestRenameAttributeStub $stub) | ||
{ | ||
throw new \RuntimeException('Parent call'); | ||
} | ||
} | ||
|
||
class LazyDependencyWithoutAttributeStub | ||
{ | ||
public function __construct(LazyWithoutAttributeStub $stub) | ||
{ | ||
throw new \RuntimeException('Parent call'); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Laravel 13 will depends on PHP 8.3 as minimum.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alrighty, in that case we can consider version 14.x, but any potential merging should wait until 13.x is released - is that correct? :)