Skip to content
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

[12.x] Bind abstract from concrete's return type #54628

Merged
merged 2 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
72 changes: 65 additions & 7 deletions src/Illuminate/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
use Illuminate\Contracts\Container\CircularDependencyException;
use Illuminate\Contracts\Container\Container as ContainerContract;
use Illuminate\Contracts\Container\ContextualAttribute;
use Illuminate\Support\Collection;
use LogicException;
use ReflectionAttribute;
use ReflectionClass;
use ReflectionException;
use ReflectionFunction;
use ReflectionIntersectionType;
use ReflectionParameter;
use ReflectionUnionType;
use TypeError;

class Container implements ArrayAccess, ContainerContract
Expand Down Expand Up @@ -268,15 +271,22 @@ public function isAlias($name)
/**
* Register a binding with the container.
*
* @param string $abstract
* @param \Closure|string $abstract
* @param \Closure|string|null $concrete
* @param bool $shared
* @return void
*
* @throws \TypeError
* @throws ReflectionException
*/
public function bind($abstract, $concrete = null, $shared = false)
{
if ($abstract instanceof Closure) {
return $this->bindBasedOnClosureReturnTypes(
$abstract, $concrete, $shared
);
}

$this->dropStaleInstances($abstract);

// If no concrete type was given, we will simply set the concrete type to the
Expand Down Expand Up @@ -381,7 +391,7 @@ public function callMethodBinding($method, $instance)
* Add a contextual binding to the container.
*
* @param string $concrete
* @param string $abstract
* @param \Closure|string $abstract
* @param \Closure|string $implementation
* @return void
*/
Expand All @@ -393,7 +403,7 @@ public function addContextualBinding($concrete, $abstract, $implementation)
/**
* Register a binding if it hasn't already been registered.
*
* @param string $abstract
* @param \Closure|string $abstract
* @param \Closure|string|null $concrete
* @param bool $shared
* @return void
Expand All @@ -408,7 +418,7 @@ public function bindIf($abstract, $concrete = null, $shared = false)
/**
* Register a shared binding in the container.
*
* @param string $abstract
* @param \Closure|string $abstract
* @param \Closure|string|null $concrete
* @return void
*/
Expand All @@ -420,7 +430,7 @@ public function singleton($abstract, $concrete = null)
/**
* Register a shared binding if it hasn't already been registered.
*
* @param string $abstract
* @param \Closure|string $abstract
* @param \Closure|string|null $concrete
* @return void
*/
Expand All @@ -434,7 +444,7 @@ public function singletonIf($abstract, $concrete = null)
/**
* Register a scoped binding in the container.
*
* @param string $abstract
* @param \Closure|string $abstract
* @param \Closure|string|null $concrete
* @return void
*/
Expand All @@ -448,7 +458,7 @@ public function scoped($abstract, $concrete = null)
/**
* Register a scoped binding if it hasn't already been registered.
*
* @param string $abstract
* @param \Closure|string $abstract
* @param \Closure|string|null $concrete
* @return void
*/
Expand All @@ -459,6 +469,54 @@ public function scopedIf($abstract, $concrete = null)
}
}

/**
* Register a binding with the container based on the given Closure's return types.
*
* @param \Closure|string $abstract
* @param \Closure|string|null $concrete
* @param bool $shared
* @return void
*/
protected function bindBasedOnClosureReturnTypes($abstract, $concrete = null, $shared = false)
{
$abstracts = $this->closureReturnTypes($abstract);

$concrete = $abstract;

foreach ($abstracts as $abstract) {
$this->bind($abstract, $concrete, $shared);
}
}

/**
* Get the class names / types of the return type of the given Closure.
*
* @param \Closure $closure
* @return list<class-string>
*
* @throws \ReflectionException
*/
protected function closureReturnTypes(Closure $closure)
{
$reflection = new ReflectionFunction($closure);

if ($reflection->getReturnType() === null ||
$reflection->getReturnType() instanceof ReflectionIntersectionType) {
return [];
}

$types = $reflection->getReturnType() instanceof ReflectionUnionType
? $reflection->getReturnType()->getTypes()
: [$reflection->getReturnType()];

return (new Collection($types))
->reject(fn ($type) => $type->isBuiltin())
->reject(fn ($type) => in_array($type->getName(), ['static', 'self']))
->map(fn ($type) => $type->getName())
->values()
->all();
}

/**
* "Extend" an abstract type in the container.
*
Expand Down
18 changes: 9 additions & 9 deletions src/Illuminate/Contracts/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function tagged($tag);
/**
* Register a binding with the container.
*
* @param string $abstract
* @param \Closure|string $abstract
* @param \Closure|string|null $concrete
* @param bool $shared
* @return void
Expand All @@ -75,7 +75,7 @@ public function bindMethod($method, $callback);
/**
* Register a binding if it hasn't already been registered.
*
* @param string $abstract
* @param \Closure|string $abstract
* @param \Closure|string|null $concrete
* @param bool $shared
* @return void
Expand All @@ -85,7 +85,7 @@ public function bindIf($abstract, $concrete = null, $shared = false);
/**
* Register a shared binding in the container.
*
* @param string $abstract
* @param \Closure|string $abstract
* @param \Closure|string|null $concrete
* @return void
*/
Expand All @@ -94,7 +94,7 @@ public function singleton($abstract, $concrete = null);
/**
* Register a shared binding if it hasn't already been registered.
*
* @param string $abstract
* @param \Closure|string $abstract
* @param \Closure|string|null $concrete
* @return void
*/
Expand All @@ -103,7 +103,7 @@ public function singletonIf($abstract, $concrete = null);
/**
* Register a scoped binding in the container.
*
* @param string $abstract
* @param \Closure|string $abstract
* @param \Closure|string|null $concrete
* @return void
*/
Expand All @@ -112,7 +112,7 @@ public function scoped($abstract, $concrete = null);
/**
* Register a scoped binding if it hasn't already been registered.
*
* @param string $abstract
* @param \Closure|string $abstract
* @param \Closure|string|null $concrete
* @return void
*/
Expand All @@ -121,7 +121,7 @@ public function scopedIf($abstract, $concrete = null);
/**
* "Extend" an abstract type in the container.
*
* @param string $abstract
* @param \Closure|string $abstract
* @param \Closure $closure
* @return void
*
Expand All @@ -134,7 +134,7 @@ public function extend($abstract, Closure $closure);
*
* @template TInstance of mixed
*
* @param string $abstract
* @param \Closure|string $abstract
* @param TInstance $instance
* @return TInstance
*/
Expand All @@ -144,7 +144,7 @@ public function instance($abstract, $instance);
* Add a contextual binding to the container.
*
* @param string $concrete
* @param string $abstract
* @param \Closure|string $abstract
* @param \Closure|string $implementation
* @return void
*/
Expand Down
18 changes: 18 additions & 0 deletions tests/Container/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ public function testClosureResolution()
$this->assertSame('Taylor', $container->make('name'));
}

public function testAbstractCanBeBoundFromConcreteReturnType()
{
$container = new Container;
$container->bind(function (): IContainerContractStub|ContainerImplementationStub {
return new ContainerImplementationStub;
});
$container->singleton(function (): ContainerConcreteStub {
return new ContainerConcreteStub;
});

$this->assertInstanceOf(
IContainerContractStub::class,
$container->make(IContainerContractStub::class)
);

$this->assertTrue($container->isShared(ContainerConcreteStub::class));
}

public function testBindIfDoesntRegisterIfServiceAlreadyRegistered()
{
$container = new Container;
Expand Down
Loading