0 follower

Final Class Yiisoft\Definitions\Helpers\DefinitionExtractor

InheritanceYiisoft\Definitions\Helpers\DefinitionExtractor

This class extracts dependency definitions from type hints of a function or a class constructor parameters.

Note that service names need not match the parameter names, parameter names are ignored.

Public Methods

Hide inherited methods

Method Description Defined By
fromClassName() Extract dependency definitions from type hints of a class constructor parameters. Yiisoft\Definitions\Helpers\DefinitionExtractor
fromFunction() Extract dependency definitions from type hints of a function. Yiisoft\Definitions\Helpers\DefinitionExtractor

Method Details

Hide inherited methods

fromClassName() public static method

Extract dependency definitions from type hints of a class constructor parameters.

public static Yiisoft\Definitions\ParameterDefinition[] fromClassName ( string $class )
$class string
throws Yiisoft\Definitions\Exception\NotInstantiableException

                public static function fromClassName(string $class): array
{
    if (isset(self::$dependencies[$class])) {
        return self::$dependencies[$class];
    }
    try {
        $reflectionClass = new ReflectionClass($class);
    } catch (ReflectionException) {
        throw new NotInstantiableClassException($class);
    }
    if (!$reflectionClass->isInstantiable()) {
        throw new NotInstantiableClassException($class);
    }
    $constructor = $reflectionClass->getConstructor();
    $dependencies = $constructor === null ? [] : self::fromFunction($constructor);
    self::$dependencies[$class] = $dependencies;
    return $dependencies;
}

            
fromFunction() public static method

Extract dependency definitions from type hints of a function.

public static Yiisoft\Definitions\ParameterDefinition[] fromFunction ( ReflectionFunctionAbstract $reflectionFunction )
$reflectionFunction ReflectionFunctionAbstract

                public static function fromFunction(ReflectionFunctionAbstract $reflectionFunction): array
{
    $result = [];
    foreach ($reflectionFunction->getParameters() as $parameter) {
        $result[$parameter->getName()] = new ParameterDefinition($parameter);
    }
    return $result;
}