Esempio n. 1
0
 /**
  * {@inheritdoc}
  */
 public function call($callable, array $parameters = array())
 {
     if ($this->container) {
         $callable = $this->resolveCallableFromContainer($callable);
     }
     $this->assertIsCallable($callable);
     $callableReflection = CallableReflection::create($callable);
     $args = $this->parameterResolver->getParameters($callableReflection, $parameters, array());
     $this->assertMandatoryParametersAreResolved($args, $callableReflection);
     // Sort by array key because invokeArgs ignores numeric keys
     ksort($args);
     if ($callableReflection instanceof \ReflectionFunction) {
         return $callableReflection->invokeArgs($args);
     }
     /** @var \ReflectionMethod $callableReflection */
     if ($callableReflection->isStatic()) {
         // Static method
         $object = null;
     } elseif (is_object($callable)) {
         // Callable object
         $object = $callable;
     } else {
         // Object method
         $object = $callable[0];
     }
     return $callableReflection->invokeArgs($object, $args);
 }
Esempio n. 2
0
 /**
  * {@inheritdoc}
  */
 public function call($callable, array $parameters = array())
 {
     if ($this->container) {
         $callable = $this->resolveCallableFromContainer($callable);
     }
     if (!is_callable($callable)) {
         throw new NotCallableException(sprintf('%s is not a callable', is_object($callable) ? 'Instance of ' . get_class($callable) : var_export($callable, true)));
     }
     $callableReflection = CallableReflection::create($callable);
     $args = $this->parameterResolver->getParameters($callableReflection, $parameters, array());
     // Sort by array key because call_user_func_array ignores numeric keys
     ksort($args);
     // Check all parameters are resolved
     $diff = array_diff_key($callableReflection->getParameters(), $args);
     if (!empty($diff)) {
         /** @var \ReflectionParameter $parameter */
         $parameter = reset($diff);
         throw new NotEnoughParametersException(sprintf('Unable to invoke the callable because no value was given for parameter %d ($%s)', $parameter->getPosition() + 1, $parameter->name));
     }
     return call_user_func_array($callable, $args);
 }
 /**
  * {@inheritdoc}
  */
 public function getArguments(Request $request, $controller)
 {
     $controllerReflection = CallableReflection::create($controller);
     $controllerParameters = $controllerReflection->getParameters();
     $resolvedArguments = [];
     foreach ($controllerParameters as $index => $parameter) {
         if ('request' === $parameter->getName() || $parameter->getClass() && $parameter->getClass()->isInstance($request)) {
             $resolvedArguments[$index] = $request;
             break;
         }
     }
     $arguments = $this->parameterResolver->getParameters($controllerReflection, $request->attributes->all(), $resolvedArguments);
     ksort($arguments);
     // Check if all parameters are resolved
     $diff = array_diff_key($controllerParameters, $arguments);
     if (0 < count($diff)) {
         /** @var \ReflectionParameter $parameter */
         $parameter = reset($diff);
         throw new \RuntimeException(sprintf('Controller "%s" requires that you provide a value for the "$%s" argument.', $controllerReflection->getName(), $parameter->getName()));
     }
     return $arguments;
 }