AliasResolver.php
5 years ago
ArrayResolver.php
5 years ago
DecoratorResolver.php
5 years ago
DefinitionResolver.php
5 years ago
EnvironmentVariableResolver.php
5 years ago
FactoryResolver.php
5 years ago
InstanceInjector.php
5 years ago
ObjectCreator.php
5 years ago
ParameterResolver.php
5 years ago
ResolverDispatcher.php
5 years ago
StringResolver.php
5 years ago
ValueResolver.php
5 years ago
ParameterResolver.php
139 lines
| 1 | <?php |
| 2 | /** |
| 3 | * PHP-DI |
| 4 | * |
| 5 | * @link http://mnapoli.github.com/PHP-DI/ |
| 6 | * @copyright Matthieu Napoli (http://mnapoli.fr/) |
| 7 | * @license http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file) |
| 8 | */ |
| 9 | |
| 10 | namespace Cybot\Dependencies\DI\Definition\Resolver; |
| 11 | |
| 12 | use Cybot\Dependencies\DI\Definition\AbstractFunctionCallDefinition; |
| 13 | use Cybot\Dependencies\DI\Definition\ObjectDefinition; |
| 14 | use Cybot\Dependencies\DI\Definition\Exception\DefinitionException; |
| 15 | use Cybot\Dependencies\DI\Definition\Helper\DefinitionHelper; |
| 16 | |
| 17 | /** |
| 18 | * Resolves parameters for a function call. |
| 19 | * |
| 20 | * @since 4.2 |
| 21 | * @author Matthieu Napoli <matthieu@mnapoli.fr> |
| 22 | */ |
| 23 | class ParameterResolver |
| 24 | { |
| 25 | /** |
| 26 | * @var DefinitionResolver |
| 27 | */ |
| 28 | private $definitionResolver; |
| 29 | |
| 30 | /** |
| 31 | * @param DefinitionResolver $definitionResolver Will be used to resolve nested definitions. |
| 32 | */ |
| 33 | public function __construct(DefinitionResolver $definitionResolver) |
| 34 | { |
| 35 | $this->definitionResolver = $definitionResolver; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @param AbstractFunctionCallDefinition $definition |
| 40 | * @param \ReflectionFunctionAbstract $functionReflection |
| 41 | * @param array $parameters |
| 42 | * |
| 43 | * @throws DefinitionException A parameter has no value defined or guessable. |
| 44 | * @return array Parameters to use to call the function. |
| 45 | */ |
| 46 | public function resolveParameters( |
| 47 | AbstractFunctionCallDefinition $definition = null, |
| 48 | \ReflectionFunctionAbstract $functionReflection = null, |
| 49 | array $parameters = [] |
| 50 | ) { |
| 51 | $args = []; |
| 52 | |
| 53 | if (! $functionReflection) { |
| 54 | return $args; |
| 55 | } |
| 56 | |
| 57 | foreach ($functionReflection->getParameters() as $index => $parameter) { |
| 58 | if (array_key_exists($parameter->getName(), $parameters)) { |
| 59 | // Look in the $parameters array |
| 60 | $value = $parameters[$parameter->getName()]; |
| 61 | } elseif ($definition && $definition->hasParameter($index)) { |
| 62 | // Look in the definition |
| 63 | $value = $definition->getParameter($index); |
| 64 | } else { |
| 65 | // If the parameter is optional and wasn't specified, we take its default value |
| 66 | if ($parameter->isOptional()) { |
| 67 | $args[] = $this->getParameterDefaultValue($parameter, $functionReflection); |
| 68 | continue; |
| 69 | } |
| 70 | |
| 71 | throw new DefinitionException(sprintf( |
| 72 | "The parameter '%s' of %s has no value defined or guessable", |
| 73 | $parameter->getName(), |
| 74 | $this->getFunctionName($functionReflection) |
| 75 | )); |
| 76 | } |
| 77 | |
| 78 | if ($value instanceof DefinitionHelper) { |
| 79 | $nestedDefinition = $value->getDefinition(''); |
| 80 | |
| 81 | // If the container cannot produce the entry, we can use the default parameter value |
| 82 | if (!$this->definitionResolver->isResolvable($nestedDefinition) && $parameter->isOptional()) { |
| 83 | $value = $this->getParameterDefaultValue($parameter, $functionReflection); |
| 84 | } else { |
| 85 | $value = $this->definitionResolver->resolve($nestedDefinition); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | $args[] = $value; |
| 90 | } |
| 91 | |
| 92 | return $args; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Returns the default value of a function parameter. |
| 97 | * |
| 98 | * @param \ReflectionParameter $parameter |
| 99 | * @param \ReflectionFunctionAbstract $function |
| 100 | * |
| 101 | * @throws DefinitionException Can't get default values from PHP internal classes and functions |
| 102 | * @return mixed |
| 103 | */ |
| 104 | private function getParameterDefaultValue( |
| 105 | \ReflectionParameter $parameter, |
| 106 | \ReflectionFunctionAbstract $function |
| 107 | ) { |
| 108 | try { |
| 109 | return $parameter->getDefaultValue(); |
| 110 | } catch (\ReflectionException $e) { |
| 111 | throw new DefinitionException(sprintf( |
| 112 | "The parameter '%s' of %s has no type defined or guessable. It has a default value, " |
| 113 | . "but the default value can't be read through Reflection because it is a PHP internal class.", |
| 114 | $parameter->getName(), |
| 115 | $this->getFunctionName($function) |
| 116 | )); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | private function getFunctionName(\ReflectionFunctionAbstract $reflectionFunction) |
| 121 | { |
| 122 | if ($reflectionFunction instanceof \ReflectionMethod) { |
| 123 | return sprintf( |
| 124 | '%s::%s', |
| 125 | $reflectionFunction->getDeclaringClass()->getName(), |
| 126 | $reflectionFunction->getName() |
| 127 | ); |
| 128 | } elseif ($reflectionFunction->isClosure()) { |
| 129 | return sprintf( |
| 130 | 'closure defined in %s at line %d', |
| 131 | $reflectionFunction->getFileName(), |
| 132 | $reflectionFunction->getStartLine() |
| 133 | ); |
| 134 | } |
| 135 | |
| 136 | return $reflectionFunction->getName(); |
| 137 | } |
| 138 | } |
| 139 |