| 1 |
<?php declare(strict_types=1); |
| 2 |
|
| 3 |
namespace Imagify\Dependencies\League\Container\Argument; |
| 4 |
|
| 5 |
use Imagify\Dependencies\League\Container\Container; |
| 6 |
use Imagify\Dependencies\League\Container\Exception\{ContainerException, NotFoundException}; |
| 7 |
use Imagify\Dependencies\League\Container\ReflectionContainer; |
| 8 |
use Imagify\Dependencies\Psr\Container\ContainerInterface; |
| 9 |
use ReflectionFunctionAbstract; |
| 10 |
use ReflectionParameter; |
| 11 |
|
| 12 |
trait ArgumentResolverTrait |
| 13 |
{ |
| 14 |
/** |
| 15 |
* {@inheritdoc} |
| 16 |
*/ |
| 17 |
public function resolveArguments(array $arguments) : array |
| 18 |
{ |
| 19 |
return array_map(function ($argument) { |
| 20 |
$justStringValue = false; |
| 21 |
|
| 22 |
if ($argument instanceof RawArgumentInterface) { |
| 23 |
return $argument->getValue(); |
| 24 |
} elseif ($argument instanceof ClassNameInterface) { |
| 25 |
$id = $argument->getClassName(); |
| 26 |
} elseif (!is_string($argument)) { |
| 27 |
return $argument; |
| 28 |
} else { |
| 29 |
$justStringValue = true; |
| 30 |
$id = $argument; |
| 31 |
} |
| 32 |
|
| 33 |
$container = null; |
| 34 |
|
| 35 |
try { |
| 36 |
$container = $this->getLeagueContainer(); |
| 37 |
} catch (ContainerException $e) { |
| 38 |
if ($this instanceof ReflectionContainer) { |
| 39 |
$container = $this; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
if ($container !== null) { |
| 44 |
try { |
| 45 |
return $container->get($id); |
| 46 |
} catch (NotFoundException $exception) { |
| 47 |
if ($argument instanceof ClassNameWithOptionalValue) { |
| 48 |
return $argument->getOptionalValue(); |
| 49 |
} |
| 50 |
|
| 51 |
if ($justStringValue) { |
| 52 |
return $id; |
| 53 |
} |
| 54 |
|
| 55 |
throw $exception; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
if ($argument instanceof ClassNameWithOptionalValue) { |
| 60 |
return $argument->getOptionalValue(); |
| 61 |
} |
| 62 |
|
| 63 |
// Just a string value. |
| 64 |
return $id; |
| 65 |
}, $arguments); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* {@inheritdoc} |
| 70 |
*/ |
| 71 |
public function reflectArguments(ReflectionFunctionAbstract $method, array $args = []) : array |
| 72 |
{ |
| 73 |
$arguments = array_map(function (ReflectionParameter $param) use ($method, $args) { |
| 74 |
$name = $param->getName(); |
| 75 |
$type = $param->getType(); |
| 76 |
|
| 77 |
if (array_key_exists($name, $args)) { |
| 78 |
return new RawArgument($args[$name]); |
| 79 |
} |
| 80 |
|
| 81 |
if ($type) { |
| 82 |
if (PHP_VERSION_ID >= 70100) { |
| 83 |
$typeName = $type->getName(); |
| 84 |
} else { |
| 85 |
$typeName = (string) $type; |
| 86 |
} |
| 87 |
|
| 88 |
$typeName = ltrim($typeName, '?'); |
| 89 |
|
| 90 |
if ($param->isDefaultValueAvailable()) { |
| 91 |
return new ClassNameWithOptionalValue($typeName, $param->getDefaultValue()); |
| 92 |
} |
| 93 |
|
| 94 |
return new ClassName($typeName); |
| 95 |
} |
| 96 |
|
| 97 |
if ($param->isDefaultValueAvailable()) { |
| 98 |
return new RawArgument($param->getDefaultValue()); |
| 99 |
} |
| 100 |
|
| 101 |
throw new NotFoundException(sprintf( |
| 102 |
'Unable to resolve a value for parameter (%s) in the function/method (%s)', |
| 103 |
$name, |
| 104 |
$method->getName() |
| 105 |
)); |
| 106 |
}, $method->getParameters()); |
| 107 |
|
| 108 |
return $this->resolveArguments($arguments); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* @return ContainerInterface |
| 113 |
*/ |
| 114 |
abstract public function getContainer() : ContainerInterface; |
| 115 |
|
| 116 |
/** |
| 117 |
* @return Container |
| 118 |
*/ |
| 119 |
abstract public function getLeagueContainer() : Container; |
| 120 |
} |
| 121 |
|