diff --git a/Container.php b/Container.php index 519fcdf..d21029d 100755 --- a/Container.php +++ b/Container.php @@ -11,6 +11,7 @@ use LogicException; use ReflectionClass; use ReflectionException; +use ReflectionFunction; use ReflectionParameter; use TypeError; @@ -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 )) { @@ -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. *