/** * @param MethodInjection $definition * @param \ReflectionFunctionAbstract $functionReflection * @param array $parameters * * @throws DefinitionException A parameter has no value defined or guessable. * @return array Parameters to use to call the function. */ public function resolveParameters(MethodInjection $definition = null, \ReflectionFunctionAbstract $functionReflection = null, array $parameters = []) { $args = []; if (!$functionReflection) { return $args; } $definitionParameters = $definition ? $definition->getParameters() : array(); foreach ($functionReflection->getParameters() as $index => $parameter) { if (array_key_exists($parameter->getName(), $parameters)) { // Look in the $parameters array $value =& $parameters[$parameter->getName()]; } elseif (array_key_exists($index, $definitionParameters)) { // Look in the definition $value =& $definitionParameters[$index]; } else { // If the parameter is optional and wasn't specified, we take its default value if ($parameter->isOptional()) { $args[] = $this->getParameterDefaultValue($parameter, $functionReflection); continue; } throw new DefinitionException(sprintf("The parameter '%s' of %s has no value defined or guessable", $parameter->getName(), $this->getFunctionName($functionReflection))); } if ($value instanceof DefinitionHelper) { $nestedDefinition = $value->getDefinition(''); // If the container cannot produce the entry, we can use the default parameter value if ($parameter->isOptional() && !$this->definitionResolver->isResolvable($nestedDefinition)) { $value = $this->getParameterDefaultValue($parameter, $functionReflection); } else { $value = $this->definitionResolver->resolve($nestedDefinition); } } $args[] =& $value; } return $args; }
private function dumpMethodParameters($className, MethodInjection $methodInjection) { $methodReflection = new \ReflectionMethod($className, $methodInjection->getMethodName()); $args = []; $definitionParameters = $methodInjection->getParameters(); foreach ($methodReflection->getParameters() as $index => $parameter) { if (array_key_exists($index, $definitionParameters)) { $value = $definitionParameters[$index]; if ($value instanceof EntryReference) { $args[] = sprintf('$%s = get(%s)', $parameter->getName(), $value->getName()); } else { $args[] = sprintf('$%s = %s', $parameter->getName(), var_export($value, true)); } continue; } // If the parameter is optional and wasn't specified, we take its default value if ($parameter->isOptional()) { try { $value = $parameter->getDefaultValue(); $args[] = sprintf('$%s = (default value) %s', $parameter->getName(), var_export($value, true)); continue; } catch (ReflectionException $e) { // The default value can't be read through Reflection because it is a PHP internal class } } $args[] = sprintf('$%s = #UNDEFINED#', $parameter->getName()); } return implode(PHP_EOL . ' ', $args); }