/**
  * Create the parameter array to call a method.
  *
  * @param ClassDefinition  $definition
  * @param MethodInjection  $methodInjection
  * @param ReflectionMethod $methodReflection
  * @param array            $parameters       Force some parameters to specific values.
  *
  * @throws DefinitionException A parameter has no defined or guessable value.
  * @return array Array of parameters to use to call the method.
  */
 private function prepareMethodParameters(ClassDefinition $definition, MethodInjection $methodInjection = null, ReflectionMethod $methodReflection = null, array $parameters = array())
 {
     if (!$methodReflection) {
         return array();
     }
     $args = array();
     foreach ($methodReflection->getParameters() as $index => $parameter) {
         if (array_key_exists($parameter->getName(), $parameters)) {
             // Look in the $parameters array
             $value = $parameters[$parameter->getName()];
         } elseif ($methodInjection && $methodInjection->hasParameter($index)) {
             // Look in the definition
             $value = $methodInjection->getParameter($index);
         } else {
             // If the parameter is optional and wasn't specified, we take its default value
             if ($parameter->isOptional()) {
                 $args[] = $this->getParameterDefaultValue($parameter, $methodReflection);
                 continue;
             }
             throw DefinitionException::create($definition, sprintf("The parameter '%s' of %s::%s has no value defined or guessable", $parameter->getName(), $methodReflection->getDeclaringClass()->getName(), $methodReflection->getName()));
         }
         if ($value instanceof EntryReference) {
             $args[] = $this->container->get($value->getName());
         } else {
             $args[] = $value;
         }
     }
     return $args;
 }
示例#2
0
 private function dumpMethodParameters($className, MethodInjection $methodInjection)
 {
     $methodReflection = new \ReflectionMethod($className, $methodInjection->getMethodName());
     $args = array();
     foreach ($methodReflection->getParameters() as $index => $parameter) {
         if ($methodInjection->hasParameter($index)) {
             $value = $methodInjection->getParameter($index);
             if ($value instanceof EntryReference) {
                 $args[] = sprintf('$%s = link(%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);
 }