Final Class Yiisoft\Definitions\CallableDefinition
| Inheritance | Yiisoft\Definitions\CallableDefinition |
|---|---|
| Implements | Yiisoft\Definitions\Contract\DefinitionInterface |
Builds an object by executing a callable injecting dependencies based on types used in its signature.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Definitions\CallableDefinition | |
| resolve() | Yiisoft\Definitions\CallableDefinition |
Method Details
| 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;
}
| 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);
}
Signup or Login in order to comment.