From 552a7dd5a66180fbd5b659dbc24771c9fee69631 Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Wed, 11 May 2022 22:10:57 -0300 Subject: [PATCH] Databases accept fully qualified driver class name In config file now we can specify the database driver with you class to load, it's allow uses personalized classes with personalized database adapters. --- system/database/autoload.php | 31 +++++++++++++++++++++++++++---- system/engine/autoload.php | 15 +++++++++++++-- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/system/database/autoload.php b/system/database/autoload.php index 14b343d..ae1f7a2 100644 --- a/system/database/autoload.php +++ b/system/database/autoload.php @@ -1,9 +1,10 @@ */ namespace Phacil\Framework; @@ -14,6 +15,7 @@ use Phacil\Framework\Config; /** * Principal class to load databases drivers * + * @since 2.0.0 * @package Phacil\Framework */ final class Database { /** @@ -22,6 +24,27 @@ final class Database { * @var DatabaseInterface */ private $driver; + + /** + * Legacy config drivers correspondent classes + * + * @var string[] + */ + static public $legacyDrivers = [ + 'mpdo' => '\Phacil\Framework\Databases\mPDO', + 'mysql' => '\Phacil\Framework\Databases\MySQL', + 'dbmysqli' => '\Phacil\Framework\Databases\DBMySQLi', + 'mssql' => '\Phacil\Framework\Databases\MSSQL', + 'mysql_legacy' => '\Phacil\Framework\Databases\MySQL_legacy', + 'mysql_pdo' => '\Phacil\Framework\Databases\MYSQL_PDO', + 'mysqli' => '\Phacil\Framework\Databases\MySQLi', + 'nullstatement' => '\Phacil\Framework\Databases\nullStatement', + 'oracle' => '\Phacil\Framework\Databases\Oracle', + 'postgre' => '\Phacil\Framework\Databases\Postgre', + 'sqlite3_db' => '\Phacil\Framework\Databases\Sqlite3_db', + 'sqlsrv' => '\Phacil\Framework\Databases\SQLSRV', + 'sqlsrvpdo' => '\Phacil\Framework\Databases\sqlsrvPDO' + ]; /** * Prefix for query cache @@ -42,11 +65,11 @@ final class Database { */ public function __construct($driver, $hostname, $username, $password, $database) { - $driverClass = "\\Phacil\\Framework\\Databases\\".$driver; + $driverClass = (isset(self::$legacyDrivers[strtolower($driver)])) ? self::$legacyDrivers[strtolower($driver)] : $driver; try { $this->createDriver(new $driverClass($hostname, $username, $password, $database)); - } catch (Exception $th) { + } catch (\Exception $th) { throw new \Phacil\Framework\Exception($driver. ' not loaded. '.$th->getMessage(), $th->getCode()); } @@ -62,7 +85,7 @@ final class Database { try { $this->driver = $driverObject; } catch (Exception $th) { - throw new Exception('Error: Could not create the driver!'); + throw new Exception('Error: Could not create the driver! '.$th->getMessage()); } } diff --git a/system/engine/autoload.php b/system/engine/autoload.php index ebabba2..12f4499 100644 --- a/system/engine/autoload.php +++ b/system/engine/autoload.php @@ -34,6 +34,8 @@ */ private static $class = null; + private static $subCallClass = []; + /** * * @var string @@ -151,7 +153,8 @@ if($file){ if(self::required($file)) { - self::getInstance()->loadedClasses[] = self::$class; + self::getInstance()->loadedClasses[] = count(self::$subCallClass) > 1 ? self::$subCallClass : self::$class; + self::$subCallClass = []; return true; } } else { @@ -212,7 +215,7 @@ } } catch (\Exception $e) { $log = new \Phacil\Framework\Log("exception.log"); - $log->write($class . ' not loaded!'); + $log->write(self::$class . ' not loaded!'); exit($e->getMessage()); } } @@ -510,6 +513,10 @@ } + protected function checkIsLoadedClass($class) { + return in_array($class, $this->loadedClasses); + } + /** * Initite loaders process * @@ -521,8 +528,12 @@ static public function load($class) { self::$class = $class; + self::$subCallClass[] = $class; + $autoload = self::getInstance(); + //if($autoload->checkIsLoadedClass($class)) return; + if($autoload->loadConfigClass()) return; $autoload->configIsFirst();