Resolve Arguments Matched With Callable Arguments

@param callable           $callable
@param array|\Traversable $parameters Params to match with function arguments

@return \Closure
@throws \InvalidArgumentException
344195
static function resolveCallableWithArgs(callable $callable, $parameters)
    {
        if ($parameters instanceof \Traversable)
            $parameters = \Poirot\Std\cast($parameters)->toArray();


        $reflection       = self::makeReflectFromCallable($callable);
        $matchedArguments = self::resolveArgsForReflection($reflection, $parameters);

        if (!empty($parameters) && empty($matchedArguments))
            // In Case That Fun Has No Any Argument.
            // exp. func() { $args = func_get_args() ..
            $matchedArguments = $parameters;

        $callbackResolved = function() use ($callable, $matchedArguments) {
            return call_user_func_array($callable, $matchedArguments);
        };

        return $callbackResolved;
    }
901453
protected function reflectCallable($callable)
	{
		if (is_string($callable) && strpos($callable, '::')) {
			$callable = explode('::', $callable, 2);
		}

		if (is_a($callable, 'Closure')) {
			$reflection = new \ReflectionFunction($callable);
		} else if (is_object($callable)) {
			$reflection = new \ReflectionMethod(get_class($callable), '__invoke');
		} else if (is_array($callable) && count($callable) == 2) {
			$reflection = new \ReflectionMethod((is_object($callable[0]) ? get_class($callable[0]) : $callable[0]), $callable[1]);
		} else if (is_string($callable) && function_exists($callable)) {
			$reflection = new \ReflectionFunction($callable);
		}

		return $reflection;
	}
1068371
protected function callHandler(
        callable $callable,
        ServerRequestInterface $request,
        ResponseInterface $response,
        array $routeArguments
    ) {
        # Action is a closure
        if ($callable instanceof Closure) {
            array_unshift($routeArguments, $request, $response);
            $result = call_user_func_array($callable, $routeArguments);
        } else {
            # Action is a method of an Action class
            if (is_array($callable) && $callable[0] instanceof Action\ActionInterface) {
                $callable[0]->setResponse($response);
                $callable[0]->setRequest($request);
            }

            # Call Action method
            $result   = $this->callAction($callable, $request, $response, $routeArguments);
            /** @var Action\ActionInterface $action */
            $action   = $callable[0];
            $response = $action->getResponse();
        }

        if (($result instanceof ResponseInterface)) {
            $response = $result;
        } else {
            $response = $this->buildResponse($result, $response, $request);
        }

        return $response;
    }
1064567
public static function fromCallable($callable)
    {
        if (!is_callable($callable))
        {
            throw new \InvalidArgumentException('You must provide a vaild PHP callable.');
        }
        elseif (is_string($callable) && strpos($callable, '::') > 0)
        {
            $callable = explode('::', $callable);
        }

        if (is_array($callable))
        {
            list($class, $method) = $callable;
            $reflection = new \ReflectionMethod($class, $method);
        }
        else
        {
            $reflection = new \ReflectionFunction($callable);
        }

        return new static($reflection);
    }
416039
public function alter(\Closure $callable)
    {
        $schema = new Schema\AlterSchema($this->table);
        $callable = $callable->bindTo($schema);

        $callable($this->database);

        return $this->execSchema($schema);
    }
165719
public function extend(string $key, Closure $callable)
    {
        if (!isset($this->stack[$key])) {
            throw new InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $key));
        }

        if (!$callable instanceof Closure) {
            throw new InvalidArgumentException(
                sprintf('Identifier "%s" is not Closure Object.', $callable)
            );
        }

        $binding = $this->offsetExists($key) ? $this->stack[$key] : null;

        $extended = function () use ($callable, $binding) {
            if (!is_object($binding)) {
                throw new ContainerException(sprintf('"%s" must be Closure object.', $binding));
            }

            return $callable($binding($this), $this);
        };

        return $this[$key] = $extended;
    }
111967
public function map($callable)
    {
        if (!$this->isCallable($callable)) {
            throw new ArrayObjectException(ArrayObjectException::MSG_INVALID_PARAM, [
                '$callable',
                'a callable function or method'
            ]);
        }

        $this->val(array_map($callable, $this->val()));

        return $this;
    }
522247
public function filter(callable $callable)
    {
        $result = [];

        foreach ($this->value as $key => $value) {
            if ($callable($value, $key, $this)) {
                $result[$key] = $value;
            }
        }

        return static::of($result);
    }
896874
public function invoke(Callable $callable)
	{
		$reflection = $this->reflectCallable($callable);

		$args = [];

		foreach($reflection->getParameters() as $param) {
			$type = $param->getClass()->getName();

			if (in_array($type, $this->resolving)) {
				throw new \LogicException("Recursive dependency: $type is currently instatiating.");
			}

			$args[] = ($param->allowsNull() && !isset($this[$type])) ? null : $this[$type];
		}

		return call_user_func_array($callable, $args);
	}
204157
public function options($callable = null)
    {
        if (null === $callable) {
            if (is_callable($this->options)) {
                return call_user_func($this->options);
            }

            $rep = new $this->options;

            return $rep->options();
        }

        $this->options = $callable;

        return $this;
    }
1275156
public function search(callable $callable)
    {
        foreach ($this as $key => $row) {
            if (call_user_func_array($callable, [$row, $key])) {
                return $row;
            }
        }

        return null;
    }
938181
public function traverseOrders(callable $callable)
    {
        foreach ($this->orders() as $expr) {
            /**
             * @param  OrderInterface           $expr The iterated order expression object.
             * @param  OrderCollectionInterface $this The context of the traversal.
             * @return void
             */
            $callable($expr, $this);
            if ($expr instanceof OrderCollectionInterface) {
                $expr->traverseOrders($callable);
            }
        }

        return $this;
    }
389424
public function addResolver($name, $callable) {
        $resolver = new RouteResolver($name, $callable);
        $this->getRoutesMatcher()->getParameterResolver()
            ->addResolver($resolver);

        return $this;
    }
1073567
public function resolve($callable)
    {
        if (is_string($callable) && strpos($callable, '::') !== false) {
            $callable = explode('::', $callable, 2);
        }

        $callable = $this->resolveFromContainer($callable);

        if (! is_callable($callable)) {
            throw NotCallableException::fromInvalidCallable($callable, true);
        }

        return $callable;
    }
774085
public function addBaselineCallable($name, $callable)
    {
        if (isset($this->callables[$name])) {
            throw new \InvalidArgumentException(sprintf(
                'Baseline callable "%s" has already been registered.',
                $name
            ));
        }

        if (!is_callable($callable)) {
            throw new \InvalidArgumentException(sprintf(
                'Given baseline "%s" callable "%s" is not callable.',
                $name, is_string($callable) ? $callable : gettype($callable)
            ));
        }

        $this->callables[$name] = $callable;
    }
804448
private function getReflectionParameters(callable $callable)
    {
        $reflectionParams = [];

        if (is_array($callable)) {
            // Callable array
            $reflector = new \ReflectionMethod($callable[0], $callable[1]);
            $reflectionParams = $reflector->getParameters();
        } else if (is_string($callable)) {
            // Callable function string
            $reflector = new \ReflectionFunction($callable);
            $reflectionParams = $reflector->getParameters();
        } else if (is_a($callable, 'Closure') || is_callable($callable, '__invoke')) {
            /** @var \Closure $callable */
            $reflector = new \ReflectionObject($callable);
            $reflectionParams = $reflector->getMethod('__invoke')->getParameters();
        }

        return $reflectionParams;
    }
182688
public function getCallable(?ContainerInterface $container = null) : callable
    {
        $callable = $this->handler;

        if (is_string($callable) && strpos($callable, '::') !== false) {
            $callable = explode('::', $callable);
        }

        if (is_array($callable) && isset($callable[0]) && is_object($callable[0])) {
            $callable = [$callable[0], $callable[1]];
        }

        if (is_array($callable) && isset($callable[0]) && is_string($callable[0])) {
            $callable = [$this->resolveClass($container, $callable[0]), $callable[1]];
        }

        if (is_string($callable) && method_exists($callable, '__invoke')) {
            $callable = $this->resolveClass($container, $callable);
        }

        if (! is_callable($callable)) {
            throw new InvalidArgumentException('Could not resolve a callable for this route');
        }

        return $callable;
    }