Example #1
0
 /**
  * Create instance with call time arguments
  *
  * @param string $requestedType
  * @param array $arguments
  * @return object
  * @throws \Exception
  *
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function create($requestedType, array $arguments = array())
 {
     $type = $this->config->getInstanceType($requestedType);
     $parameters = $this->definitions->getParameters($type);
     if ($parameters == null) {
         return new $type();
     }
     if (isset($this->creationStack[$requestedType])) {
         $lastFound = end($this->creationStack);
         $this->creationStack = array();
         throw new \LogicException("Circular dependency: {$requestedType} depends on {$lastFound} and vice versa.");
     }
     $this->creationStack[$requestedType] = $requestedType;
     try {
         $args = $this->_resolveArguments($requestedType, $parameters, $arguments);
         unset($this->creationStack[$requestedType]);
     } catch (\Exception $e) {
         unset($this->creationStack[$requestedType]);
         throw $e;
     }
     switch (count($args)) {
         case 1:
             return new $type($args[0]);
         case 2:
             return new $type($args[0], $args[1]);
         case 3:
             return new $type($args[0], $args[1], $args[2]);
         case 4:
             return new $type($args[0], $args[1], $args[2], $args[3]);
         case 5:
             return new $type($args[0], $args[1], $args[2], $args[3], $args[4]);
         case 6:
             return new $type($args[0], $args[1], $args[2], $args[3], $args[4], $args[5]);
         case 7:
             return new $type($args[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6]);
         case 8:
             return new $type($args[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6], $args[7]);
         default:
             $reflection = new \ReflectionClass($type);
             return $reflection->newInstanceArgs($args);
     }
 }
Example #2
0
 /**
  * Create instance with call time arguments
  *
  * @param string $requestedType
  * @param array $arguments
  * @return object
  * @throws \Exception
  *
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function create($requestedType, array $arguments = array())
 {
     $type = $this->config->getInstanceType($requestedType);
     $parameters = $this->definitions->getParameters($type);
     if ($parameters == null) {
         return new $type();
     }
     if (isset($this->creationStack[$requestedType])) {
         $lastFound = end($this->creationStack);
         $this->creationStack = array();
         throw new \LogicException("Circular dependency: {$requestedType} depends on {$lastFound} and vice versa.");
     }
     $this->creationStack[$requestedType] = $requestedType;
     try {
         $args = $this->_resolveArguments($requestedType, $parameters, $arguments);
         unset($this->creationStack[$requestedType]);
     } catch (\Exception $e) {
         unset($this->creationStack[$requestedType]);
         throw $e;
     }
     $reflection = new \ReflectionClass($type);
     return $reflection->newInstanceArgs($args);
 }