/**
  * 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);
 }
 /**
  * {@inheritdoc}
  */
 public function dump(Definition $definition)
 {
     if (!$definition instanceof FunctionCallDefinition) {
         throw new \InvalidArgumentException(sprintf('This definition dumper is only compatible with FunctionCallDefinition objects, %s given', get_class($definition)));
     }
     $callable = $definition->getCallable();
     $functionReflection = CallableReflectionFactory::fromCallable($callable);
     $functionName = $this->getFunctionName($functionReflection);
     $parameters = $this->dumpMethodParameters($definition, $functionReflection);
     return sprintf("%s(\n    %s\n)", $functionName, $parameters);
 }
Exemplo n.º 3
0
 /**
  * {@inheritdoc}
  * TODO use a `callable` type-hint once support is for PHP 5.4 and up
  */
 public function getCallableDefinition($callable)
 {
     $reflection = CallableReflectionFactory::fromCallable($callable);
     return new FunctionCallDefinition($callable, $this->getParametersDefinition($reflection));
 }