/**
  * Resolve a function call definition to a value.
  *
  * This will call the function and return its result.
  *
  * @param FunctionCallDefinition $definition
  *
  * {@inheritdoc}
  */
 public function resolve(Definition $definition, array $parameters = array())
 {
     $this->assertIsFunctionCallDefinition($definition);
     $callable = $definition->getCallable();
     $functionReflection = CallableReflectionFactory::fromCallable($callable);
     try {
         $args = $this->parameterResolver->resolveParameters($definition, $functionReflection, $parameters);
     } catch (DefinitionException $e) {
         throw DefinitionException::create($definition, $e->getMessage());
     }
     if ($functionReflection instanceof \ReflectionFunction) {
         return $functionReflection->invokeArgs($args);
     }
     /** @var \ReflectionMethod $functionReflection */
     if ($functionReflection->isStatic()) {
         // Static method
         $object = null;
     } elseif (is_object($callable)) {
         // Callable object
         $object = $callable;
     } elseif (is_string($callable)) {
         // Callable class (need to be instantiated)
         $object = $this->container->get($callable);
     } elseif (is_string($callable[0])) {
         // Class method
         $object = $this->container->get($callable[0]);
     } else {
         // Object method
         $object = $callable[0];
     }
     return $functionReflection->invokeArgs($object, $args);
 }
Example #2
0
 protected function injectMethodsAndProperties($object, ObjectDefinition $objectDefinition)
 {
     // Property injections
     foreach ($objectDefinition->getPropertyInjections() as $propertyInjection) {
         $this->injectProperty($object, $propertyInjection);
     }
     // Method injections
     foreach ($objectDefinition->getMethodInjections() as $methodInjection) {
         $methodReflection = new \ReflectionMethod($object, $methodInjection->getMethodName());
         $args = $this->parameterResolver->resolveParameters($methodInjection, $methodReflection);
         $methodReflection->invokeArgs($object, $args);
     }
 }