Exemplo n.º 1
0
 /**
  * For classes that expect dependencies in their constructor, you can use
  * this method instead of "new" with lots of Container::get calls.
  *
  * Type-hinted arguments _must_ match a dependency. If no dependency exists,
  * a new instance will be passed.
  *
  * @return mixed An object of the same class as called on, with dependencies
  *  injected through its constructor.
  * @throws Disclosure\TypeMismatchException if the retrieved dependency does
  *  not satisfy the argument's type hint.
  * @throws Psr\Container\Exception\NotFoundExceptionInterface if _any_ of
  *  the requested dependencies could not be resolved.
  */
 public static function resolve()
 {
     $reflection = new ReflectionClass(__CLASS__);
     $constructor = $reflection->getConstructor();
     $args = [];
     $container = new Container();
     foreach ($constructor->getParameters() as $parameter) {
         $name = $parameter->name;
         $e = $inject = $class = null;
         try {
             $inject = $container->get($name);
         } catch (NotFoundExceptionInterface $e) {
         }
         if ($class = $parameter->getClass()) {
             $instance = $class->getName();
             if (isset($inject)) {
                 if ($inject instanceof $instance) {
                     $args[] = $inject;
                     continue;
                 } else {
                     throw new TypeMismatchException(get_class($inject));
                 }
             }
             if ($class->implementsInterface('Disclosure\\Injectable')) {
                 $args[] = $class::resolve();
             } else {
                 $args[] = $class->newInstance();
             }
         } elseif ($parameter->isDefaultValueAvailable()) {
             $args[] = $parameter->getDefaultValue();
         } elseif (isset($e)) {
             throw $e;
         } elseif (isset($inject)) {
             $args[] = $inject;
         }
     }
     return $reflection->newInstanceArgs($args);
 }