/** * Create the dependency injected * @param string $variable * @param bool $singleton * @param string $classNameSpace * @param string $calledClass * @return mixed */ public static function constructInyectableInstance($variable, $singleton, $classNameSpace, $calledClass) { Logger::log('Create inyectable instance for ' . $classNameSpace); $reflector = new \ReflectionClass($calledClass); $property = $reflector->getProperty($variable); $varInstanceType = null === $classNameSpace ? InjectorHelper::extractVarType($property->getDocComment()) : $classNameSpace; if (true === $singleton && method_exists($varInstanceType, "getInstance")) { $instance = $varInstanceType::getInstance(); } else { $instance = new $varInstanceType(); } return $instance; }
/** * Método que inyecta automáticamente las dependencias en la clase */ public function init() { if (!$this->isLoaded()) { $cacheFilename = "reflections" . DIRECTORY_SEPARATOR . sha1(get_class($this)) . ".json"; /** @var \PSFS\base\Cache $cacheService */ $cacheService = Cache::getInstance(); /** @var \PSFS\base\config\Config $configService */ $configService = Config::getInstance(); $properties = $cacheService->getDataFromFile($cacheFilename, Cache::JSON); if (true === $configService->getDebugMode() || null === $properties) { $properties = InjectorHelper::getClassProperties(get_class($this)); $cacheService->storeData($cacheFilename, $properties, Cache::JSON); } /** @var \ReflectionProperty $property */ if (!empty($properties) && is_array($properties)) { foreach ($properties as $property => $class) { $this->load($property, true, $class); } } $this->setLoaded(); } else { Logger::log(get_class($this) . ' already loaded', LOG_INFO); } }
/** * Extract all the properties from Dto class * * @param string $class * * @return array */ protected function extractDtoProperties($class) { $properties = []; $reflector = new \ReflectionClass($class); if ($reflector->isSubclassOf(self::DTO_INTERFACE)) { $properties = array_merge($properties, InjectorHelper::extractProperties($reflector, \ReflectionMethod::IS_PUBLIC)); } return $properties; }