framework/di/Container.php
public function resolveCallableDependencies(callable $callback, $params = [])
    {
        if (is_array($callback)) {
            $reflection = new \ReflectionMethod($callback[0], $callback[1]);
        } elseif (is_object($callback) && !$callback instanceof \Closure) {
            $reflection = new \ReflectionMethod($callback, '__invoke');
        } else {
            $reflection = new \ReflectionFunction($callback);
        }

        $args = [];

        $associative = ArrayHelper::isAssociative($params);

        foreach ($reflection->getParameters() as $param) {
            $name = $param->getName();
            if (($class = $param->getClass()) !== null) {
                $className = $class->getName();
                if (version_compare(PHP_VERSION, '5.6.0', '>=') && $param->isVariadic()) {
                    $args = array_merge($args, array_values($params));
                    break;
                } elseif ($associative && isset($params[$name]) && $params[$name] instanceof $className) {
                    $args[] = $params[$name];
                    unset($params[$name]);
                } elseif (!$associative && isset($params[0]) && $params[0] instanceof $className) {
                    $args[] = array_shift($params);
                } elseif (isset(Yii::$app) && Yii::$app->has($name) && ($obj = Yii::$app->get($name)) instanceof $className) {
                    $args[] = $obj;
                } else {
                    // If the argument is optional we catch not instantiable exceptions
                    try {
                        $args[] = $this->get($className);
                    } catch (NotInstantiableException $e) {
                        if ($param->isDefaultValueAvailable()) {
                            $args[] = $param->getDefaultValue();
                        } else {
                            throw $e;
                        }
                    }
                }
            } elseif ($associative && isset($params[$name])) {
                $args[] = $params[$name];
                unset($params[$name]);
            } elseif (!$associative && count($params)) {
                $args[] = array_shift($params);
            } elseif ($param->isDefaultValueAvailable()) {
                $args[] = $param->getDefaultValue();
            } elseif (!$param->isOptional()) {
                $funcName = $reflection->getName();
                throw new InvalidConfigException("Missing required parameter \"$name\" when calling \"$funcName\".");
            }
        }

        foreach ($params as $value) {
            $args[] = $value;
        }

        return $args;
    }
public function resolveCallableDependencies(callable $callback, $params = [])
    {
        if (is_array($callback)) {
            $reflection = new \ReflectionMethod($callback[0], $callback[1]);
        } elseif (is_object($callback) && !$callback instanceof \Closure) {
            $reflection = new \ReflectionMethod($callback, '__invoke');
        } else {
            $reflection = new \ReflectionFunction($callback);
        }

        $args = [];

        $associative = ArrayHelper::isAssociative($params);

        foreach ($reflection->getParameters() as $param) {
            $name = $param->getName();
            if (($class = $param->getClass()) !== null) {
                $className = $class->getName();
                if (version_compare(PHP_VERSION, '5.6.0', '>=') && $param->isVariadic()) {
                    $args = array_merge($args, array_values($params));
                    break;
                } elseif ($associative && isset($params[$name]) && $params[$name] instanceof $className) {
                    $args[] = $params[$name];
                    unset($params[$name]);
                } elseif (!$associative && isset($params[0]) && $params[0] instanceof $className) {
                    $args[] = array_shift($params);
                } elseif (isset(Yii::$app) && Yii::$app->has($name) && ($obj = Yii::$app->get($name)) instanceof $className) {
                    $args[] = $obj;
                } else {
                    // If the argument is optional we catch not instantiable exceptions
                    try {
                        $args[] = $this->get($className);
                    } catch (NotInstantiableException $e) {
                        if ($param->isDefaultValueAvailable()) {
                            $args[] = $param->getDefaultValue();
                        } else {
                            throw $e;
                        }
                    }
                }
            } elseif ($associative && isset($params[$name])) {
                $args[] = $params[$name];
                unset($params[$name]);
            } elseif (!$associative && count($params)) {
                $args[] = array_shift($params);
            } elseif ($param->isDefaultValueAvailable()) {
                $args[] = $param->getDefaultValue();
            } elseif (!$param->isOptional()) {
                $funcName = $reflection->getName();
                throw new InvalidConfigException("Missing required parameter \"$name\" when calling \"$funcName\".");
            }
        }

        foreach ($params as $value) {
            $args[] = $value;
        }

        return $args;
    }
public function resolveCallableDependencies ( callable $ callback , $ params = [ ] ) { if ( is_array ( $ callback ) ) { $ reflection = new \ ReflectionMethod ( $ callback [ 0 ] , $ callback [ 1 ] ) ; } elseif ( is_object ( $ callback ) && ! $ callback instanceof \ Closure ) { $ reflection = new \ ReflectionMethod ( $ callback , '__invoke' ) ; } else { $ reflection = new \ ReflectionFunction ( $ callback ) ; } $ args = [ ] ; $ associative = ArrayHelper :: isAssociative ( $ params ) ; foreach ( $ reflection -> getParameters ( ) as $ param ) { $ name = $ param -> getName ( ) ; if ( ( $ class = $ param -> getClass ( ) ) !== null ) { $ className = $ class -> getName ( ) ; if ( version_compare ( PHP_VERSION , '5.6.0' , '>=' ) && $ param -> isVariadic ( ) ) { $ args = array_merge ( $ args , array_values ( $ params ) ) ; break ; } elseif ( $ associative && isset ( $ params [ $ name ] ) && $ params [ $ name ] instanceof $ className ) { $ args [ ] = $ params [ $ name ] ; unset ( $ params [ $ name ] ) ; } elseif ( ! $ associative && isset ( $ params [ 0 ] ) && $ params [ 0 ] instanceof $ className ) { $ args [ ] = array_shift ( $ params ) ; } elseif ( isset ( Yii :: $ app ) && Yii :: $ app -> has ( $ name ) && ( $ obj = Yii :: $ app -> get ( $ name ) ) instanceof $ className ) { $ args [ ] = $ obj ; } else { // If the argument is optional we catch not instantiable exceptions try { $ args [ ] = $ this -> get ( $ className ) ; } catch ( NotInstantiableException $ e ) { if ( $ param -> isDefaultValueAvailable ( ) ) { $ args [ ] = $ param -> getDefaultValue ( ) ; } else { throw $ e ; } } } } elseif ( $ associative && isset ( $ params [ $ name ] ) ) { $ args [ ] = $ params [ $ name ] ; unset ( $ params [ $ name ] ) ; } elseif ( ! $ associative && count ( $ params ) ) { $ args [ ] = array_shift ( $ params ) ; } elseif ( $ param -> isDefaultValueAvailable ( ) ) { $ args [ ] = $ param -> getDefaultValue ( ) ; } elseif ( ! $ param -> isOptional ( ) ) { $ funcName = $ reflection -> getName ( ) ; throw new InvalidConfigException ( "Missing required parameter \"$name\" when calling \"$funcName\"." ) ; } } foreach ( $ params as $ value ) { $ args [ ] = $ value ; } return $ args ; }
Resolve dependencies for a function.

This method can be used to implement similar functionality as provided by [[invoke()]] in other
components.

@param callable $callback callable to be invoked.
@param array $params The array of parameters for the function, can be either numeric or associative.
@return array The resolved dependencies.
@throws InvalidConfigException if a dependency cannot be resolved or if a dependency cannot be fulfilled.
@throws NotInstantiableException If resolved to an abstract class or an interface (since 2.0.9)
@since 2.0.7
Resolve dependencies for a function .