Exemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function invoke(ContainerInterface $container, array $params = array())
 {
     $params = array_replace($this->params, $params);
     $callable = $this->definition->getCallable();
     if (is_array($callable) && is_string($callable[0])) {
         $callable[0] = $container->resolve($callable[0]);
     }
     $args = [];
     foreach ($this->definition->getArguments() as $arg) {
         if ($arg->isClass()) {
             $resolvedArg = $this->resolveClassArg($container, $arg, $params);
         } else {
             $resolvedArg = $this->resolveScalarArg($container, $arg, $params);
         }
         if ($resolvedArg instanceof FactoryInterface) {
             $resolvedArg = $resolvedArg->invoke($container);
         }
         $args[$arg->getPosition()] = $resolvedArg;
     }
     return call_user_func_array($callable, $args);
 }
Exemplo n.º 2
0
 /**
  * Add arguments to an existing factory via reflection.
  *
  * @param Definition                      $factory
  * @param ReflectionFunctionAbstract|null $reflectionFunction Optional
  */
 protected static function addReflectionArguments(Definition $factory, ReflectionFunctionAbstract $reflectionFunction = null)
 {
     if (!$reflectionFunction) {
         $callable = $factory->getCallable();
         if (is_array($callable)) {
             $reflectionFunction = new ReflectionMethod($callable[0], $callable[1]);
         } else {
             $reflectionFunction = new ReflectionFunction($callable);
         }
     }
     foreach ($reflectionFunction->getParameters() as $arg) {
         try {
             $name = $arg->getName();
             $required = !$arg->isOptional();
             if ($argClass = $arg->getClass()) {
                 $factory->addClassArgument($name, $argClass->getName(), $required);
             } else {
                 $default = $required ? null : $arg->getDefaultValue();
                 $factory->addScalarArgument($name, null, $required, $default);
             }
         } catch (ReflectionException $re) {
             throw UnresolvableArgumentException::fromReflectionParam($arg, $reflectionFunction, $re);
         }
     }
 }