/**
  * @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;
 }
Exemple #2
0
 /**
  * Test if the container can provide something for the given name.
  *
  * @param string $name Entry name or a class name.
  *
  * @throws InvalidArgumentException The name parameter must be of type string.
  * @return bool
  */
 public function has($name)
 {
     if (!is_string($name)) {
         throw new InvalidArgumentException(sprintf('The name parameter must be of type string, %s given', is_object($name) ? get_class($name) : gettype($name)));
     }
     if (array_key_exists($name, $this->singletonEntries)) {
         return true;
     }
     $definition = $this->definitionSource->getDefinition($name);
     if ($definition === null) {
         return false;
     }
     return $this->definitionResolver->isResolvable($definition);
 }