示例#1
0
 /**
  * Resolve all dependencies of your class and return instance of
  * your class
  *
  * @param $class
  * @return mixed
  * @throws \Cygnite\Container\Exceptions\ContainerException
  */
 public function make($class)
 {
     $reflectionClass = $reflection = null;
     $reflection = new Reflection();
     $reflection->setClass($class);
     $reflectionClass = $reflection->getReflectionClass();
     if (false === $reflectionClass->isInstantiable()) {
         throw new ContainerException("Cannot instantiate " . ($reflection->reflectionClass->isInterface() ? 'interface' : 'class') . " '{$class}'");
     }
     $constructor = null;
     $constructorArgsCount = '';
     if ($reflectionClass->hasMethod('__construct')) {
         $constructor = $reflectionClass->getConstructor();
         $constructorArgsCount = $constructor->getNumberOfParameters();
         $constructor->setAccessible(true);
     }
     // if class does not have explicitly defined constructor or constructor does not have parameters
     // get the new instance
     if (!isset($constructor) && is_null($constructor) || $constructorArgsCount < 1) {
         $this->stack[$class] = $reflectionClass->newInstance();
     } else {
         $dependencies = $constructor->getParameters();
         $constructorArgs = [];
         foreach ($dependencies as $dependency) {
             if (!is_null($dependency->getClass())) {
                 list($resolveClass, $reflectionParam) = $this->getReflectionParam($dependency);
                 // Application and Container cannot be injected into controller currently
                 // since Application constructor is protected
                 if ($reflectionParam->IsInstantiable()) {
                     $constructorArgs[] = $this->makeInstance($resolveClass);
                 } else {
                     $constructorArgs[] = $this->interfaceInjection($reflectionParam);
                 }
             } else {
                 /*
                 | We will check if construct has default value
                 | if exists we will simply assign it and continue
                 | for next argument
                 */
                 if ($dependency->isDefaultValueAvailable()) {
                     $constructorArgs[] = $dependency->getDefaultValue();
                     continue;
                 }
                 /*
                 | Check parameters are optional or not
                 | if it is optional we will set the default value
                 */
                 $constructorArgs[] = $this->isOptionalArgs($dependency);
             }
         }
         $this->stack[$class] = $reflectionClass->newInstanceArgs($constructorArgs);
     }
     return $this->stack[$class];
 }
示例#2
0
 /**
  * @param $controller
  *
  * @return array
  */
 private function setReflectionClassAttributes($controller)
 {
     $reflection = new Reflection();
     $reflection->setClass($controller);
     return [$reflection, $reflection->getReflectionClass()];
 }