0 follower

Final Class Yiisoft\Definitions\CallableDefinition

InheritanceYiisoft\Definitions\CallableDefinition
ImplementsYiisoft\Definitions\Contract\DefinitionInterface

Builds an object by executing a callable injecting dependencies based on types used in its signature.

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( array|callable $callable )
$callable array|callable

Callable to be used for building an object. Dependencies are determined and passed based on the types of arguments in the callable signature.

                public function __construct(array|callable $callable)
{
    $this->callable = $callable;
}

            
resolve() public method

public mixed resolve ( \Psr\Container\ContainerInterface $container )
$container \Psr\Container\ContainerInterface

                public function resolve(ContainerInterface $container): mixed
{
    try {
        $reflection = new ReflectionFunction(
            $this->prepareClosure($this->callable, $container),
        );
    } catch (ReflectionException) {
        throw new NotInstantiableException(
            'Can not instantiate callable definition. Got ' . var_export($this->callable, true),
        );
    }
    $dependencies = DefinitionExtractor::fromFunction($reflection);
    $arguments = DefinitionResolver::resolveArray($container, null, $dependencies);
    return $reflection->invokeArgs($arguments);
}