| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Imagify\Dependencies\League\Container\Argument; |
| 6 |
|
| 7 |
use Imagify\Dependencies\League\Container\DefinitionContainerInterface; |
| 8 |
use Imagify\Dependencies\League\Container\Exception\{ContainerException, NotFoundException}; |
| 9 |
use Imagify\Dependencies\League\Container\ReflectionContainer; |
| 10 |
use Imagify\Dependencies\Psr\Container\ContainerInterface; |
| 11 |
use ReflectionFunctionAbstract; |
| 12 |
use ReflectionNamedType; |
| 13 |
|
| 14 |
trait ArgumentResolverTrait |
| 15 |
{ |
| 16 |
public function resolveArguments(array $arguments): array |
| 17 |
{ |
| 18 |
try { |
| 19 |
$container = $this->getContainer(); |
| 20 |
} catch (ContainerException $e) { |
| 21 |
$container = ($this instanceof ReflectionContainer) ? $this : null; |
| 22 |
} |
| 23 |
|
| 24 |
foreach ($arguments as &$arg) { |
| 25 |
// if we have a literal, we don't want to do anything more with it |
| 26 |
if ($arg instanceof LiteralArgumentInterface) { |
| 27 |
$arg = $arg->getValue(); |
| 28 |
continue; |
| 29 |
} |
| 30 |
|
| 31 |
if ($arg instanceof ArgumentInterface) { |
| 32 |
$argValue = $arg->getValue(); |
| 33 |
} else { |
| 34 |
$argValue = $arg; |
| 35 |
} |
| 36 |
|
| 37 |
if (!is_string($argValue)) { |
| 38 |
continue; |
| 39 |
} |
| 40 |
|
| 41 |
// resolve the argument from the container, if it happens to be another |
| 42 |
// argument wrapper, use that value |
| 43 |
if ($container instanceof ContainerInterface && $container->has($argValue)) { |
| 44 |
try { |
| 45 |
$arg = $container->get($argValue); |
| 46 |
|
| 47 |
if ($arg instanceof ArgumentInterface) { |
| 48 |
$arg = $arg->getValue(); |
| 49 |
} |
| 50 |
|
| 51 |
continue; |
| 52 |
} catch (NotFoundException $e) { |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
// if we have a default value, we use that, no more resolution as |
| 57 |
// we expect a default/optional argument value to be literal |
| 58 |
if ($arg instanceof DefaultValueInterface) { |
| 59 |
$arg = $arg->getDefaultValue(); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
return $arguments; |
| 64 |
} |
| 65 |
|
| 66 |
public function reflectArguments(ReflectionFunctionAbstract $method, array $args = []): array |
| 67 |
{ |
| 68 |
$params = $method->getParameters(); |
| 69 |
$arguments = []; |
| 70 |
|
| 71 |
foreach ($params as $param) { |
| 72 |
$name = $param->getName(); |
| 73 |
|
| 74 |
// if we've been given a value for the argument, treat as literal |
| 75 |
if (array_key_exists($name, $args)) { |
| 76 |
$arguments[] = new LiteralArgument($args[$name]); |
| 77 |
continue; |
| 78 |
} |
| 79 |
|
| 80 |
$type = $param->getType(); |
| 81 |
|
| 82 |
if ($type instanceof ReflectionNamedType) { |
| 83 |
// in PHP 8, nullable arguments have "?" prefix |
| 84 |
$typeHint = ltrim($type->getName(), '?'); |
| 85 |
|
| 86 |
if ($param->isDefaultValueAvailable()) { |
| 87 |
$arguments[] = new DefaultValueArgument($typeHint, $param->getDefaultValue()); |
| 88 |
continue; |
| 89 |
} |
| 90 |
|
| 91 |
$arguments[] = new ResolvableArgument($typeHint); |
| 92 |
continue; |
| 93 |
} |
| 94 |
|
| 95 |
if ($param->isDefaultValueAvailable()) { |
| 96 |
$arguments[] = new LiteralArgument($param->getDefaultValue()); |
| 97 |
continue; |
| 98 |
} |
| 99 |
|
| 100 |
throw new NotFoundException(sprintf( |
| 101 |
'Unable to resolve a value for parameter (%s) in the function/method (%s)', |
| 102 |
$name, |
| 103 |
$method->getName() |
| 104 |
)); |
| 105 |
} |
| 106 |
|
| 107 |
return $this->resolveArguments($arguments); |
| 108 |
} |
| 109 |
|
| 110 |
abstract public function getContainer(): DefinitionContainerInterface; |
| 111 |
} |
| 112 |
|