Skip to content

Commit

Permalink
support contextual binding on first class callables
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Apr 3, 2023
1 parent fed3ee8 commit a44a9e8
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use LogicException;
use ReflectionClass;
use ReflectionException;
use ReflectionFunction;
use ReflectionParameter;
use TypeError;

Expand Down Expand Up @@ -648,8 +649,8 @@ public function call($callback, array $parameters = [], $defaultMethod = null)
{
$pushedToBuildStack = false;

if (is_array($callback) && ! in_array(
$className = (is_string($callback[0]) ? $callback[0] : get_class($callback[0])),
if (($className = $this->getClassForCallable($callback)) && ! in_array(
$className,
$this->buildStack,
true
)) {
Expand All @@ -667,6 +668,22 @@ public function call($callback, array $parameters = [], $defaultMethod = null)
return $result;
}

/**
* Get the class name for the given callback, if one can be determined.
*
* @param callable|string $callback
* @return string|false
*/
protected function getClassForCallable($callback)
{
if (is_callable($callback) &&
! ($reflector = new ReflectionFunction($callback(...)))->isAnonymous()) {
return $reflector->getClosureScopeClass()->name ?? false;
}

return false;
}

/**
* Get a closure to resolve the given type from the container.
*
Expand Down

0 comments on commit a44a9e8

Please sign in to comment.