From 28b64672a4f90acecc5041b4e0b9a63328e3c3e1 Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Sat, 18 May 2024 13:28:36 -0300 Subject: [PATCH] Added type argument injector, xml to DB item, Version class --- README.md | 6 +- system/Version/autoload.php | 52 +++++++++++ system/config/autoload.php | 2 +- .../Connectors/Oracle/ORDS/Helper/Data.php | 2 +- .../Oracle/ORDS/Model/Handler/Curl.php | 2 +- system/database/Databases/Driver/MySQLi.php | 4 +- system/database/Databases/Object/Item.php | 33 +++++++ system/engine/registry.php | 88 ++++++++++++++++++- system/etc/preferences.json | 12 +++ system/system.php | 4 +- 10 files changed, 196 insertions(+), 9 deletions(-) create mode 100644 system/Version/autoload.php diff --git a/README.md b/README.md index f41fc57..6af77ad 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Phacil-framework - ![GitHub](https://img.shields.io/github/license/exacti/phacil-framework.svg) ![GitHub top language](https://img.shields.io/github/languages/top/exacti/phacil-framework.svg) ![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/exacti/phacil-framework.svg) ![GitHub issues](https://img.shields.io/github/issues/exacti/phacil-framework.svg) ![PHP Version](https://img.shields.io/badge/php-%3E%3D5.4.45-blue.svg) ![GitHub last commit](https://img.shields.io/github/last-commit/exacti/phacil-framework.svg) ![GitHub Release Date](https://img.shields.io/github/release-date/exacti/phacil-framework.svg) ![GitHub release](https://img.shields.io/github/release/exacti/phacil-framework.svg) + ![GitHub](https://img.shields.io/github/license/exacti/phacil-framework.svg) ![GitHub top language](https://img.shields.io/github/languages/top/exacti/phacil-framework.svg) ![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/exacti/phacil-framework.svg) ![GitHub issues](https://img.shields.io/github/issues/exacti/phacil-framework.svg) ![PHP Version](https://img.shields.io/badge/php-%3E%3D5.6.20-blue.svg) ![GitHub last commit](https://img.shields.io/github/last-commit/exacti/phacil-framework.svg) ![GitHub Release Date](https://img.shields.io/github/release-date/exacti/phacil-framework.svg) ![GitHub release](https://img.shields.io/github/release/exacti/phacil-framework.svg) A super easy PHP Framework for web development! @@ -8,8 +8,8 @@ A super easy PHP Framework for web development! ## Requirements - - PHP 5.4+ (PHP 7.0+ recommended with OPCache and igbinary extensions) - - HTTP Web Server (Apache 2.4+ recommended with mod_rewrite) + - PHP 5.6.20+ (PHP 8.0+ recommended with OPCache extensions) + - HTTP Web Server (Apache 2.4+ recommended with mod_rewrite or NGINX) - Some writable directories (like logs and cache) ## Structure diff --git a/system/Version/autoload.php b/system/Version/autoload.php new file mode 100644 index 0000000..2f7715e --- /dev/null +++ b/system/Version/autoload.php @@ -0,0 +1,52 @@ +framework = $framework; + } + + /** + * Return Phacil Framework version + * + * @return string|false + */ + public function get() + { + return $this->framework; + //return file_get_contents(\Phacil\Framework\Config::DIR_SYSTEM() . "engine/VERSION"); + } + + /** + * Alias for get method + * @return string|false + */ + public function getFrameworkVersion() { + return $this->get(); + } + + /** + * + * @return string|false + */ + public function getPHPVersion() { + return phpversion(); + } +} \ No newline at end of file diff --git a/system/config/autoload.php b/system/config/autoload.php index 81c30ee..735a053 100644 --- a/system/config/autoload.php +++ b/system/config/autoload.php @@ -10,8 +10,8 @@ namespace Phacil\Framework; /** @package Phacil\Framework */ final class Config { + /** - * * @var array */ private $data = array(); diff --git a/system/database/Databases/Connectors/Oracle/ORDS/Helper/Data.php b/system/database/Databases/Connectors/Oracle/ORDS/Helper/Data.php index f4684cd..a03f56a 100644 --- a/system/database/Databases/Connectors/Oracle/ORDS/Helper/Data.php +++ b/system/database/Databases/Connectors/Oracle/ORDS/Helper/Data.php @@ -45,7 +45,7 @@ class Data extends \Phacil\Framework\AbstractHelper { break; default: // Escape single quotes and add single quotes around strings - $formattedValue = "'" . str_replace("'", "''", $value) . "'"; + $formattedValue = sprintf("'%s'", str_replace("'", "''", $value) ); } return $formattedValue; diff --git a/system/database/Databases/Connectors/Oracle/ORDS/Model/Handler/Curl.php b/system/database/Databases/Connectors/Oracle/ORDS/Model/Handler/Curl.php index acad650..e44b125 100644 --- a/system/database/Databases/Connectors/Oracle/ORDS/Model/Handler/Curl.php +++ b/system/database/Databases/Connectors/Oracle/ORDS/Model/Handler/Curl.php @@ -20,7 +20,7 @@ use Phacil\Framework\Databases\Connectors\Oracle\ORDS\Connector; class Curl implements HandleInterface { /** * - * @var resource|false + * @var \CurlHandle|resource|false */ private $curl; diff --git a/system/database/Databases/Driver/MySQLi.php b/system/database/Databases/Driver/MySQLi.php index 2bd5e5f..82d3f90 100644 --- a/system/database/Databases/Driver/MySQLi.php +++ b/system/database/Databases/Driver/MySQLi.php @@ -148,7 +148,9 @@ class MySQLi implements DriverInterface { } else { $stmt = $this->connection->prepare($sql); } - + if(!$stmt){ + return $stmt; + } $result_exec = $stmt->execute(); if ($stmt->errno) { diff --git a/system/database/Databases/Object/Item.php b/system/database/Databases/Object/Item.php index ab4b77b..5fe32b0 100644 --- a/system/database/Databases/Object/Item.php +++ b/system/database/Databases/Object/Item.php @@ -110,4 +110,37 @@ class Item extends NativeArrayObject implements ObjectInterface { return $lower_array_object[$fieldname]; } } + + /** + * Convert object data into XML string + * + * @param string $rootName (Optional) root node name + * @param bool $addOpenTag (Optional) flag that allow to add initial xml node + * @param bool $addCdata (Optional) flag that require wrap all values in CDATA + * @return string + */ + public function toXml($rootName = 'item', $addOpenTag = true, $addCdata = true) + { + $xml = ''; + //$data = $this->__data; + foreach ($this->__data as $fieldName => $fieldValue) { + if ($addCdata === true) { + $fieldValue = ""; + } else { + $fieldValue = $fieldValue !== null ? str_replace( + ['&', '"', "'", '<', '>'], + ['&', '"', ''', '<', '>'], + $fieldValue + ) : ''; + } + $xml .= "<{$fieldName}>{$fieldValue}\n"; + } + if ($rootName) { + $xml = "<{$rootName}>\n{$xml}\n"; + } + if ($addOpenTag) { + $xml = '' . "\n" . $xml; + } + return $xml; + } } \ No newline at end of file diff --git a/system/engine/registry.php b/system/engine/registry.php index 62e843b..1fc8e11 100644 --- a/system/engine/registry.php +++ b/system/engine/registry.php @@ -57,12 +57,19 @@ final class Registry { static private $autoInstances = []; /** - * AutoInstances Loaded + * DI Preference class * * @var array */ static private $preferences = []; + /** + * DI Preference class + * + * @var array + */ + static private $diOptions = []; + static private $diCheckedRoutes = []; /** @@ -199,6 +206,13 @@ final class Registry { } if (isset($dataArray['preferences'])) self::$preferences = array_merge(self::$preferences, $dataArray['preferences']); + + if(count($dataArray) > 1) { + foreach($dataArray as $key => $value) { + if($key !== "preferences") + self::$diOptions[$key] = isset(self::$diOptions[$key]) ? array_merge(self::$diOptions[$key], $value) : $value; + } + } } /** @@ -307,6 +321,73 @@ final class Registry { return $class; } + /** + * Check argument type to inject + * + * @param string $class + * @param string $argument + * @return mixed + * @throws \ReflectionException + * @throws \Exception + * @throws \Phacil\Framework\Exception + */ + static public function checkType($class, $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])) + return self::$diOptions['type'][$class][$argument]; + + if(isset(self::$diOptions['type'][$class][$argument]['type'])){ + return self::returnArgumentType(self::$diOptions['type'][$class][$argument]['type'], self::$diOptions['type'][$class][$argument]['value']); + } + } + } + } + return false; + } + + /** + * Help argument type + * + * @param string $type + * @param mixed $argumentValue + * @return mixed + * @throws \ReflectionException + * @throws \Exception + * @throws \Phacil\Framework\Exception + */ + static public function returnArgumentType($type, $argumentValue) { + switch ($type) { + case 'object': + return self::getInstance($argumentValue); + break; + + case 'object[]': + $object = array_map(function($value){ + return self::getInstance($value); + }, $argumentValue); + /* foreach($argumentValue as $objectValue) { + $object[] = self::getInstance($objectValue); + } */ + return $object; + break; + + case 'bool': + case 'boolean': + return boolval($argumentValue); + break; + + case 'int': + return intval($argumentValue); + break; + + default: + return $argumentValue; + break; + } + } + /** * Sets an auto-instantiated object to be used as a dependency. * @@ -389,6 +470,11 @@ final class Registry { foreach ($params as $param) { //$param is an instance of ReflectionParameter try { + //Check for the predefined type + if($typeFound = self::checkType($originalClass ?: $class, $param->getName())) { + $argsToInject[$param->getPosition()] = $typeFound; + continue; + } if (version_compare(phpversion(), "7.2.0", "<")) { $declaringClass = $param->__toString(); $pattern = '/\[.*?>\s*([^$\s]+)/'; //$pattern = '/<[^>]+>\s*([^$\s]+)/'; diff --git a/system/etc/preferences.json b/system/etc/preferences.json index bb52a21..7e818d9 100644 --- a/system/etc/preferences.json +++ b/system/etc/preferences.json @@ -15,5 +15,17 @@ "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" + }, + "type": { + "Phacil\\Framework\\Version": { + "framework": "2.0.0", + "teste": { + "type": "object[]", + "value": { + "manta": "Phacil\\Framework\\Api\\Log", + "coberta": "Phacil\\Framework\\Api\\Document" + } + } + } } } \ No newline at end of file diff --git a/system/system.php b/system/system.php index b43cc76..4940bdb 100644 --- a/system/system.php +++ b/system/system.php @@ -278,7 +278,9 @@ final class startEngineExacTI { * @return string|false */ public function version() { - return file_get_contents(\Phacil\Framework\Config::DIR_SYSTEM()."engine/VERSION"); + /** @var \Phacil\Framework\Version */ + $version = $this->registry->getInstance(\Phacil\Framework\Version::class); + return $version->get(); } /**