From 88f1bee7671f261d321b4c12810723b307a6b4c9 Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Sun, 19 May 2024 17:48:26 -0300 Subject: [PATCH] Changed DI preferences structure --- system/engine/registry.php | 83 ++++++++++++++++++++++++++++++++----- system/etc/preferences.json | 56 ++++++++++++++++--------- 2 files changed, 108 insertions(+), 31 deletions(-) diff --git a/system/engine/registry.php b/system/engine/registry.php index 41ec241..504c0af 100644 --- a/system/engine/registry.php +++ b/system/engine/registry.php @@ -204,15 +204,19 @@ final class Registry { return; } } - if (isset($dataArray['preferences'])) - self::$preferences = array_merge(self::$preferences, $dataArray['preferences']); + /* if (isset($dataArray['preferences'])) + self::$preferences = array_merge(self::$preferences, $dataArray['preferences']); */ - if(count($dataArray) >= 1) { + if(!empty($dataArray)){ + self::$preferences = self::array_merge_recursive_distinct(self::$preferences, $dataArray); + } + + /* if(count($dataArray) >= 1) { foreach($dataArray as $key => $value) { if($key !== "preferences") self::$diOptions[$key] = isset(self::$diOptions[$key]) ? self::array_merge_recursive_distinct(self::$diOptions[$key], $value) : $value; } - } + } */ } private static function array_merge_recursive_distinct(array $array1, array $array2) @@ -308,16 +312,36 @@ final class Registry { * @return void */ static public function addDIPreference($for, $to) { - self::$preferences = array_merge(self::$preferences, [$for => $to]); + self::$preferences[$for]['preference'] = $to; } /** + * Check if preference for a class exists * * @param string $for * @return bool */ static public function checkPreferenceExist($for) { - return isset(self::$preferences[$for]); + return isset(self::$preferences[$for]) ? (isset(self::$preferences[$for]['preference']) ?: is_string(self::$preferences[$for])) : false; + } + + /** + * Check if DI preferences exists + * + * @param string $for + * @return bool + */ + public static function havePreferences($class){ + return isset(self::$preferences[$class]); + } + + /** + * + * @param string $class + * @return array|null + */ + public static function getClassPreferences($class) { + return isset(self::$preferences[$class]) ? self::$preferences[$class] : null; } /** @@ -350,6 +374,11 @@ final class Registry { */ static public function checkPreference($class) { if(isset(self::$preferences[$class])) { + if(is_array(self::$preferences[$class]) && isset(self::$preferences[$class]['preference'])){ + return self::$preferences[$class]['preference']; + } elseif(is_array(self::$preferences[$class]) && !isset(self::$preferences[$class]['preference'])) { + return $class; + } return self::$preferences[$class]; } return $class; @@ -365,8 +394,40 @@ final class Registry { * @throws \Exception * @throws \Phacil\Framework\Exception */ - protected static function checkType($class, $argument) { - if(isset(self::$diOptions['type'])){ + protected static function checkArgumentType($class, $argument) { + if(self::havePreferences($class) && is_array(self::$preferences[$class])){ + if(isset(self::$preferences[$class]['arguments']) && isset(self::$preferences[$class]['arguments'][$argument])){ + //Check if is an array for determine configurations + if(!is_array(self::$preferences[$class]['arguments'][$argument])) + return self::$preferences[$class]['arguments'][$argument]; + + if(isset(self::$preferences[$class]['arguments'][$argument]['type']) && isset(self::$preferences[$class]['arguments'][$argument]['value'])){ + return self::returnArgumentType(self::$preferences[$class]['arguments'][$argument]['type'], self::$preferences[$class]['arguments'][$argument]['value']); + } + + if (isset(self::$preferences[$class]['arguments'][$argument]['objects'])) { + return self::returnArgumentType('object[]', self::$preferences[$class]['arguments'][$argument]['objects']); + } + + if (is_array(self::$preferences[$class]['arguments'][$argument]) && count(self::$preferences[$class]['arguments'][$argument]) === 1) { + $type = key(self::$preferences[$class]['arguments'][$argument]); + + // Verifica se $type é um tipo válido + $validTypes = ['string', 'int', 'integer', 'bool', 'boolean', 'float', 'double', 'array', 'object', 'objects', 'object[]', 'resource']; + + if (in_array($type, $validTypes, true)) { + return self::returnArgumentType($type, reset(self::$preferences[$class]['arguments'][$argument])); + } + + /* foreach(self::$preferences[$class]['arguments'][$argument] as $type => $value) { + return self::returnArgumentType($type, $value); + } */ + } + + return self::$preferences[$class]['arguments'][$argument]; + } + } + /* if(isset(self::$diOptions['type'])){ if(isset(self::$diOptions['type'][$class])){ if(isset(self::$diOptions['type'][$class][$argument])){ if(!is_array(self::$diOptions['type'][$class][$argument])) @@ -377,8 +438,8 @@ final class Registry { } } } - } - return false; + } */ + return null; } /** @@ -502,7 +563,7 @@ final class Registry { //$param is an instance of ReflectionParameter try { //Check for the predefined type - if($typeFound = self::checkType($originalClass ?: $class, $param->getName())) { + if(($typeFound = self::checkArgumentType($originalClass ?: $class, $param->getName())) !== null) { $argsToInject[$param->getPosition()] = $typeFound; continue; } diff --git a/system/etc/preferences.json b/system/etc/preferences.json index 7e818d9..24a741b 100644 --- a/system/etc/preferences.json +++ b/system/etc/preferences.json @@ -1,30 +1,46 @@ { - "preferences": { - "Phacil\\Framework\\Databases\\Api\\Object\\ResultInterface": "Phacil\\Framework\\Databases\\Object\\Result", - "Phacil\\Framework\\Interfaces\\Loader": "Phacil\\Framework\\Loader", - "Phacil\\Framework\\Interfaces\\Url": "Phacil\\Framework\\Url", - "Phacil\\Framework\\Databases\\Api\\Object\\ItemInterface": "Phacil\\Framework\\Databases\\Object\\Item", - "Phacil\\Framework\\Api\\Database": "Phacil\\Framework\\Database", - "Cm\\RedisSession\\Handler\\ConfigInterface": "Phacil\\Framework\\Session\\Redis\\Config", - "Cm\\RedisSession\\Handler\\LoggerInterface": "Phacil\\Framework\\Session\\Redis\\Logger", - "Phacil\\Framework\\Mail\\Api\\MailInterface": "Phacil\\Framework\\Mail", - "Phacil\\Framework\\Api\\Document": "Phacil\\Framework\\Document", - "Phacil\\Framework\\Api\\Log": "Phacil\\Framework\\Log", - "Phacil\\Framework\\Databases\\Api\\LogInterface": "Phacil\\Framework\\Log", - "Phacil\\Framework\\Interfaces\\Serializer": "Phacil\\Framework\\Json", - "Phacil\\Framework\\MagiQL\\Api\\Factory": "Phacil\\Framework\\MagiQL\\Factory", - "Phacil\\Framework\\Databases\\Connectors\\Oracle\\ORDS\\Api\\HandleInterface": "Phacil\\Framework\\Databases\\Connectors\\Oracle\\ORDS\\Model\\Handler\\Curl", - "Phacil\\Framework\\Databases\\Connectors\\Oracle\\ORDS\\Api\\Query": "Phacil\\Framework\\Databases\\Connectors\\Oracle\\ORDS\\Model\\Query" + "Phacil\\Framework\\Databases\\Api\\Object\\ResultInterface": "Phacil\\Framework\\Databases\\Object\\Result", + "Phacil\\Framework\\Interfaces\\Loader": "Phacil\\Framework\\Loader", + "Phacil\\Framework\\Interfaces\\Url": "Phacil\\Framework\\Url", + "Phacil\\Framework\\Databases\\Api\\Object\\ItemInterface": "Phacil\\Framework\\Databases\\Object\\Item", + "Phacil\\Framework\\Api\\Database": "Phacil\\Framework\\Database", + "Cm\\RedisSession\\Handler\\ConfigInterface": { + "preference": "Phacil\\Framework\\Session\\Redis\\Config" + }, + "Cm\\RedisSession\\Handler\\LoggerInterface": { + "preference": "Phacil\\Framework\\Session\\Redis\\Logger" + }, + "Phacil\\Framework\\Mail\\Api\\MailInterface": "Phacil\\Framework\\Mail", + "Phacil\\Framework\\Api\\Document": { + "preference": "Phacil\\Framework\\Document" + }, + "Phacil\\Framework\\Api\\Log": "Phacil\\Framework\\Log", + "Phacil\\Framework\\Databases\\Api\\LogInterface": "Phacil\\Framework\\Log", + "Phacil\\Framework\\Interfaces\\Serializer": "Phacil\\Framework\\Json", + "Phacil\\Framework\\MagiQL\\Api\\Factory": "Phacil\\Framework\\MagiQL\\Factory", + "Phacil\\Framework\\Databases\\Connectors\\Oracle\\ORDS\\Api\\HandleInterface": "Phacil\\Framework\\Databases\\Connectors\\Oracle\\ORDS\\Model\\Handler\\Curl", + "Phacil\\Framework\\Databases\\Connectors\\Oracle\\ORDS\\Api\\Query": "Phacil\\Framework\\Databases\\Connectors\\Oracle\\ORDS\\Model\\Query", + "Phacil\\Framework\\Version": { + "arguments": { + "framework": "2.0.0", + "teste": { + "type": "object[]", + "value": [ + "Phacil\\Framework\\Api\\Log", + "Phacil\\Framework\\Api\\Document" + ] + } + } }, "type": { "Phacil\\Framework\\Version": { "framework": "2.0.0", "teste": { "type": "object[]", - "value": { - "manta": "Phacil\\Framework\\Api\\Log", - "coberta": "Phacil\\Framework\\Api\\Document" - } + "value": [ + "Phacil\\Framework\\Api\\Log", + "Phacil\\Framework\\Api\\Document" + ] } } }