Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
olekjs wants to merge2 commits intolaravel:master
base:master
Choose a base branch
Loading
fromolekjs:lazy-services
Draft
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
Lazy attribute as parameter - problem preview
  • Loading branch information
@olekjs
olekjs committedMay 26, 2025
commit19bdded51732b0db844680ae44bf05244e150020
36 changes: 29 additions & 7 deletionssrc/Illuminate/Container/Container.php
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -813,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);
}

/**
Expand DownExpand Up@@ -851,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);

Expand DownExpand Up@@ -888,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
Expand DownExpand Up@@ -994,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
Expand DownExpand Up@@ -1059,7 +1063,9 @@ public function build($concrete)

array_pop($this->buildStack);

if (!empty($reflector->getAttributes(Lazy::class))) {
$isLazy = $this->isLazy(array_merge($attributes, $reflector->getAttributes(Lazy::class)));

if ($isLazy) {
$instance = $reflector->newLazyProxy(function () use ($concrete, $instances) {
return new $concrete(...$instances);
});
Expand All@@ -1074,6 +1080,22 @@ public function build($concrete)
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.
*
Expand DownExpand Up@@ -1208,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
Expand Down
15 changes: 14 additions & 1 deletiontests/Container/ContainerLazyTest.php
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -90,6 +90,19 @@ public function work()
}
}

class LazyWithTestRenameAttributeStub
{
public function __construct()
{
throw new \RuntimeException('Lazy call');
}

public function work()
{
return 'work';
}
}

#[Lazy]
class LazyWithAttributeLogicStub
{
Expand DownExpand Up@@ -118,7 +131,7 @@ public function __construct()

class LazyDependencyWithAttributeStub
{
public function __construct(LazyWithAttributeStub $stub)
public function __construct(#[Lazy] LazyWithTestRenameAttributeStub $stub)
{
throw new \RuntimeException('Parent call');
}
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp