Final Class Yiisoft\Definitions\ParameterDefinition
| Inheritance | Yiisoft\Definitions\ParameterDefinition |
|---|---|
| Implements | Yiisoft\Definitions\Contract\DefinitionInterface |
Parameter definition resolves an object based on information from ReflectionParameter instance.
Public Methods
Method Details
| public mixed __construct ( ReflectionParameter $parameter ) | ||
| $parameter | ReflectionParameter | |
public function __construct(
private readonly ReflectionParameter $parameter,
) {}
| public ReflectionParameter getReflection ( ) |
public function getReflection(): ReflectionParameter
{
return $this->parameter;
}
| public boolean hasValue ( ) |
public function hasValue(): bool
{
return $this->parameter->isDefaultValueAvailable();
}
| public boolean isOptional ( ) |
public function isOptional(): bool
{
return $this->parameter->isOptional();
}
| public boolean isVariadic ( ) |
public function isVariadic(): bool
{
return $this->parameter->isVariadic();
}
| public mixed resolve ( \Psr\Container\ContainerInterface $container ) | ||
| $container | \Psr\Container\ContainerInterface | |
public function resolve(ContainerInterface $container): mixed
{
$type = $this->parameter->getType();
if ($type instanceof ReflectionUnionType) {
return $this->resolveUnionType($type, $container);
}
if (!$type instanceof ReflectionNamedType
|| $this->isVariadic()
|| $type->isBuiltin()
) {
return $this->resolveVariadicOrBuiltinOrIntersectionOrNonTyped();
}
$typeName = $type->getName();
/**
* @psalm-suppress TypeDoesNotContainType
* @see https://github.com/vimeo/psalm/issues/6756
*/
if ($typeName === 'self') {
// If type name is "self", it means that called class and
// $parameter->getDeclaringClass() returned instance of `ReflectionClass`.
/** @psalm-suppress PossiblyNullReference */
$typeName = $this->parameter->getDeclaringClass()->getName();
}
try {
$result = $container->get($typeName);
} catch (Throwable $t) {
if (
$this->parameter->isOptional()
&& (
$t instanceof CircularReferenceException
|| !$container->has($typeName)
)
) {
return $this->parameter->getDefaultValue();
}
throw $t;
}
if (!$result instanceof $typeName) {
$actualType = get_debug_type($result);
throw new InvalidConfigException(
"Container returned incorrect type \"$actualType\" for service \"{$type->getName()}\".",
);
}
return $result;
}
Signup or Login in order to comment.