/** * Inject all your properties into controller at run time * @param $controllerInstance * @param $controller * @throws Exception */ public function propertyInjection($controllerInstance, $controller) { $definition = $this->getDefinition(); $injectableDefinitions = $definition()->getPropertyDependencies(); $this->setPropertyInjection($injectableDefinitions); $dependencies = $this->getDefinitions($controller); if (array_key_exists($controller, $this->definitions)) { $reflection = new Reflection(); $reflection->setClass($controller); foreach ($dependencies as $classProperty => $object) { if (!$reflection->reflectionClass->hasProperty($classProperty)) { throw new Exception(sprintf("Property %s is not defined in {$controller} controller", $classProperty)); } $reflection->makePropertyAccessible($classProperty); //set property value $reflection->reflectionProperty->setValue($controllerInstance, $object); } } }
/** * Resolve all dependencies of your class and return instance of * your class. * * @param $class * * @throws \Cygnite\Container\Exceptions\ContainerException * * @return mixed */ public function make($class, $arguments = []) { /* * If instance of the class already created and stored into * stack then simply return from here */ if (isset($this[$class])) { return $this[$class]; } $reflection = new Reflection(); $reflectionClass = $reflection->setClass($class)->getReflectionClass(); /* * Check if reflection class is not instantiable then throw ContainerException */ if (!$reflectionClass->isInstantiable()) { $type = $reflection->reflectionClass->isInterface() ? 'interface' : 'class'; throw new ContainerException("Cannot instantiate {$type} {$class}"); } $constructor = null; $constructorArgsCount = 0; if ($reflectionClass->hasMethod('__construct')) { $constructor = $reflectionClass->getConstructor(); $constructor->setAccessible(true); $constructorArgsCount = $constructor->getNumberOfParameters(); } // 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) { return $this[$class] = $reflectionClass->newInstance(); } $dependencies = $constructor->getParameters(); $constructorArgs = []; foreach ($dependencies as $dependency) { if (!is_null($dependency->getClass())) { $constructorArgs[] = $this->resolverClass($dependency, $arguments); } else { /* | Check if construct has default value | if exists we will simply assign it into array and continue | for next argument */ if ($dependency->isDefaultValueAvailable()) { $constructorArgs[] = $this->checkIfConstructorHasDefaultArgs($dependency, $arguments); continue; } /* | Check parameters are optional or not | if it is optional we will set the default value */ $constructorArgs[] = $this->isOptionalArgs($dependency); } } return $this[$class] = $reflectionClass->newInstanceArgs($constructorArgs); }
/** * 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]; }
/** * Resolve all dependencies of your class and return instance of * your class * * @param $class string * @throws \Exception * @return object */ public function make($class) { $reflection = new Reflection(); $reflection->setClass($class); if (false === $reflection->reflectionClass->isInstantiable()) { throw new DependencyException("Cannot instantiate " . ($reflection->reflectionClass->isInterface() ? 'interface' : 'class') . " '{$class}'"); } $constructor = null; $constructorArgsCount = ''; if ($reflection->reflectionClass->hasMethod('__construct')) { $constructor = $reflection->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] = $reflection->reflectionClass->newInstance(); } else { $dependencies = $constructor->getParameters(); $constructorArgs = array(); foreach ($dependencies as $dependency) { if (!is_null($dependency->getClass())) { //Get constructor class name $resolveClass = $dependency->getClass()->name; $reflectionParam = new ReflectionClass($resolveClass); // Application and Container cannot be injected into controller currently // since Application constructor is protected if ($reflectionParam->IsInstantiable()) { $constructorArgs[] = $this->makeInstance($resolveClass); } /* | Check if constructor dependency is Interface or not. | if interface we will check definition for the interface | and inject into controller constructor */ if (!$reflectionParam->IsInstantiable() && $reflectionParam->isInterface()) { $definition = $this->getDefinition(); $aliases = $definition()->registerAlias(); $interface = Inflector::getClassName($reflectionParam->getName()); if (array_key_exists($interface, $aliases)) { $constructorArgs[] = $this->makeInstance($aliases[$interface]); } } } else { /* | Check parameters are optional or not | if it is optional we will set the default value */ if ($dependency->isOptional()) { $constructorArgs[] = $dependency->getDefaultValue(); } } } $this->stack[$class] = $reflection->reflectionClass->newInstanceArgs($constructorArgs); } return $this->stack[$class]; }
/** * @param $controller * * @return array */ private function setReflectionClassAttributes($controller) { $reflection = new Reflection(); $reflection->setClass($controller); return [$reflection, $reflection->getReflectionClass()]; }