Final Class Yiisoft\Hydrator\Attribute\Parameter\DiResolver
| Inheritance | Yiisoft\ |
|---|---|
| Implements | Yiisoft\ |
Resolver for {@see Di} attribute. Obtains dependency from container by ID specified or auto-resolved ID by PHP type.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\ |
|
| getParameterValue() | Yiisoft\ |
Method Details
| public mixed __construct ( \ | ||
| $container | \ |
Container to obtain dependency from. |
public function __construct(
private ContainerInterface $container,
) {}
| public Yiisoft\ | ||
| $attribute | Yiisoft\ |
|
| $context | Yiisoft\ |
|
| throws | \ |
|
|---|---|---|
| throws | Yiisoft\ |
When an object is not found in a container or object ID auto-resolving fails. |
public function getParameterValue(ParameterAttributeInterface $attribute, ParameterAttributeResolveContext $context): Result
{
if (!$attribute instanceof Di) {
throw new UnexpectedAttributeException(Di::class, $attribute);
}
$parameter = $context->getParameter();
$id = $attribute->getId();
if ($id !== null) {
try {
return Result::success(
$this->container->get($id),
);
} catch (NotFoundExceptionInterface $e) {
throw new DiNotFoundException($parameter, $e);
}
}
$type = $parameter->getType();
if ($type instanceof ReflectionNamedType) {
if (!$type->isBuiltin()) {
try {
return Result::success(
$this->container->get($type->getName()),
);
} catch (NotFoundExceptionInterface $e) {
throw new DiNotFoundException($parameter, $e);
}
}
} elseif ($type instanceof ReflectionUnionType) {
foreach ($type->getTypes() as $type) {
/** @psalm-suppress RedundantConditionGivenDocblockType Needed for PHP less than 8.2 */
if ($type instanceof ReflectionNamedType && !$type->isBuiltin()) {
try {
return Result::success(
$this->container->get($type->getName()),
);
} catch (NotFoundExceptionInterface) {
}
}
}
}
throw new DiNotFoundException($parameter);
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.