From ed7dffdc20ee37717809d5565328a0fb06167c54 Mon Sep 17 00:00:00 2001 From: Fredrick Peter Date: Fri, 13 Oct 2023 23:54:02 +0100 Subject: [PATCH] critical update, no usage modification --- composer.json | 4 +- src/AutoLoader.php | 4 +- src/Capsule/AppManager.php | 10 +- src/Capsule/DebugManager.php | 49 ---------- src/Connectors/ConnectionBuilder.php | 16 ++- src/Connectors/Connector.php | 134 ++++++++++++++++++++------ src/Connectors/PostgresConnector.php | 4 +- src/Connectors/SQLiteConnector.php | 4 +- src/DB.php | 1 - src/DatabaseConnector.php | 9 +- src/DatabaseManager.php | 101 ++++++++----------- src/Dummy/dummyInit.dum | 10 +- src/Schema/Builder.php | 4 +- src/Schema/Pagination/Paginator.php | 4 +- src/Schema/Traits/BuilderTrait.php | 2 +- src/Schema/Traits/MySqlProperties.php | 2 +- src/helpers.php | 12 ++- tests/Model/Admin.php | 19 ++++ tests/Model/User.php | 1 + 19 files changed, 216 insertions(+), 174 deletions(-) delete mode 100644 src/Capsule/DebugManager.php create mode 100644 tests/Model/Admin.php diff --git a/composer.json b/composer.json index 698a7b8..d2052b6 100644 --- a/composer.json +++ b/composer.json @@ -17,9 +17,7 @@ ], "require": { "php": ">=8.0", - "tamedevelopers/support": "^2.0.0", - "vlucas/phpdotenv": "^5.4.1", - "filp/whoops": "^2.15" + "tamedevelopers/support": "^2.0.0" }, "autoload": { "files": [ diff --git a/src/AutoLoader.php b/src/AutoLoader.php index f144759..d99f71b 100644 --- a/src/AutoLoader.php +++ b/src/AutoLoader.php @@ -111,8 +111,8 @@ public static function configPagination(?array $options = []) | Adding Pagination Configuration into Constant |-------------------------------------------------------------------------- */ - if ( ! defined('PAGINATION_CONFIG') ) { - define('PAGINATION_CONFIG', $default); + if ( ! defined('TAME_PAGI_CONFIG') ) { + define('TAME_PAGI_CONFIG', $default); } } diff --git a/src/Capsule/AppManager.php b/src/Capsule/AppManager.php index 800904c..d8b80ac 100644 --- a/src/Capsule/AppManager.php +++ b/src/Capsule/AppManager.php @@ -9,7 +9,7 @@ use Tamedevelopers\Database\AutoLoader; use Tamedevelopers\Support\Capsule\Manager; use Tamedevelopers\Support\Capsule\FileCache; -use Tamedevelopers\Database\Capsule\DebugManager; +use Tamedevelopers\Support\Capsule\DebugManager; class AppManager{ @@ -63,14 +63,6 @@ public static function bootLoader() |-------------------------------------------------------------------------- */ AutoLoader::start(); - - /* - |-------------------------------------------------------------------------- - | Default connection driver is `mysql` - | use DB::connection() \to connection to other connection instance - |-------------------------------------------------------------------------- - */ - DB::connection(); } } \ No newline at end of file diff --git a/src/Capsule/DebugManager.php b/src/Capsule/DebugManager.php deleted file mode 100644 index 1cab7ba..0000000 --- a/src/Capsule/DebugManager.php +++ /dev/null @@ -1,49 +0,0 @@ -pushHandler(new PrettyPageHandler()); - self::$whoops->register(); - } - } - } - -} \ No newline at end of file diff --git a/src/Connectors/ConnectionBuilder.php b/src/Connectors/ConnectionBuilder.php index 2f52f6b..18053e9 100644 --- a/src/Connectors/ConnectionBuilder.php +++ b/src/Connectors/ConnectionBuilder.php @@ -69,7 +69,21 @@ public function __construct($data = null, $name = null, $connection = null) $this->pdo = $connection['pdo'] ?? $connection['message']; $this->config = array_merge($connection['config'], ['name' => $name]); $this->database = $this->config['database']; - $this->tablePrefix = $this->config['prefix']; + $this->tablePrefix = $this->getTablePrefixIfAllowed(); + } + } + + /** + * Get Table Prefix + * @return string|null + */ + private function getTablePrefixIfAllowed() + { + // if prefixes is set and is `true` + if(isset($this->config['prefix_indexes']) && $this->config['prefix_indexes']){ + if(isset($this->config['prefix'])){ + return $this->config['prefix']; + } } } diff --git a/src/Connectors/Connector.php b/src/Connectors/Connector.php index 466f61a..6c5f22b 100644 --- a/src/Connectors/Connector.php +++ b/src/Connectors/Connector.php @@ -5,6 +5,8 @@ namespace Tamedevelopers\Database\Connectors; use PDO; +use Exception; +use Tamedevelopers\Support\Server; use Tamedevelopers\Database\Schema\Builder; use Tamedevelopers\Support\Capsule\Manager; use Tamedevelopers\Database\DatabaseManager; @@ -25,23 +27,77 @@ class Connector { */ private $connection; + /** + * @var array|null + */ + private $default; + /** * @var string|null */ private $name; + /** + * Save instances of all connection + * @var mixed + */ + private static $connections = []; /** * Constructors * - * @param string|null $name\Database connection name - * @param mixed $connection \Connection instance - * + * @param string|null $name - Driver name + * @param mixed $connection - Connection instance + * @param array $data - Default data */ - public function __construct($name = null, mixed $connection = null) + public function __construct($name = null, $connection = null, $data = []) { $this->setConnectionName($name); $this->setConnection($connection); + $this->setDefaultData($data); + } + + /** + * Add to connection instance + * + * @param string|null $name - Driver name + * @param mixed $connection - Connection instance + * @param array $data - Default data + * + * @return \Tamedevelopers\Database\Connectors\Connector + */ + static public function addConnection($name = null, $connection = null, $data = []) + { + $driver = DatabaseManager::driverValidator($name); + + // driver name + $driverName = $driver['name']; + + // connector object + self::$connections[$driverName] = new self( + connection: $connection, + name: $driverName, + data: $data, + ); + + return self::$connections[$driverName]; + } + + /** + * Remove from connection instance + * + * @param string|null $name - Driver name + * + * @return void + */ + static public function removeFromConnection($name = null) + { + $driver = DatabaseManager::driverValidator($name); + + // driver name + $driverName = $driver['name']; + + unset(self::$connections[$driverName]); } /** @@ -96,8 +152,8 @@ public function configPagination(array $options = []) // Only if the Global Constant is not yet defined // If set to allow global use of ENV Autoloader Settings - if(defined('PAGINATION_CONFIG') && Manager::isEnvBool(PAGINATION_CONFIG['allow']) === true){ - $paginator->configPagination(PAGINATION_CONFIG); + if(defined('TAME_PAGI_CONFIG') && Manager::isEnvBool(TAME_PAGI_CONFIG['allow']) === true){ + $paginator->configPagination(TAME_PAGI_CONFIG); } else{ $paginator->configPagination($options); } @@ -111,18 +167,24 @@ public function configPagination(array $options = []) */ private function buidTable($table = null) { + // begin build $builder = new Builder; $builder->manager = new Manager; $builder->dbManager = new DatabaseManager; - // create instance of self - $instance = new self( - $this->name, - $this->dbConnection(), - ); + // get saved connection from $connections array + $instance = self::$connections[$this->name] ?? null; + + // There's no connecton instance set + if(empty($instance)){ + throw new Exception("There's no active connection! Unknown connection [{$this->name}]."); + } + + // set connection + $instance->connection = $this->dbConnection(); // setup table name - $builder->from = $this->compileTableWithPrefix($table, $this->getConfig()); + $builder->from = $table; // building of table name is only called once // so we will build the instance of Connection data into the @@ -161,8 +223,9 @@ public function getTablePrefix() public function dbConnection($mode = null) { // get connection data - $conn = DatabaseManager::getConnection($this->name); - + // merge data to default if provided, before we try to connect + $conn = self::getConnectionFromDatabaseFile($this->name, $this->default); + // connection data $connData = self::createConnector($conn['driver'])->connect($conn); @@ -202,25 +265,25 @@ public function getDatabaseName() */ public function getConfig() { - return DatabaseManager::getConnection($this->name); + return self::getConnectionFromDatabaseFile($this->name, $this->default); } /** - * Get Table Name - * @param string $table - * @param array $data - * @return string + * Get Connection data + * + * @param string|null $name + * @param array|null $default + * + * @return array */ - private static function compileTableWithPrefix($table = null, ?array $data = null) + private static function getConnectionFromDatabaseFile($name = null, $default = []) { - // check prefixes - if(isset($data['prefix_indexes']) && $data['prefix_indexes']){ - if(isset($data['prefix'])){ - $table = "{$data['prefix']}{$table}"; - } - } + $data = Server::config( + DatabaseManager::getConnectionKey($name), + [] + ); - return $table; + return array_merge($data, $default ?? []); } /** @@ -247,6 +310,19 @@ private function setConnection($connection = null) } } + /** + * Set default connection data + * + * @param array $default + * @return void + */ + private function setDefaultData($default = []) + { + if(!empty($default)){ + $this->default = $default; + } + } + /** * Set connection connection name * @@ -256,7 +332,7 @@ private function setConnection($connection = null) private function setConnectionName($name = null) { $this->name = empty($name) - ? config("database.default") + ? Server::config("database.default") : $name; } @@ -270,7 +346,7 @@ private function isModelDriverCreated() { if(self::isModelExtended()){ $this->setConnectionName(); - $key = DatabaseManager::getCacheKey($this->name); + $key = DatabaseManager::getConnectionKey($this->name); if (!FileCache::exists($key)) { DatabaseManager::connection($this->name); } diff --git a/src/Connectors/PostgresConnector.php b/src/Connectors/PostgresConnector.php index 48c08e2..4ce64b3 100644 --- a/src/Connectors/PostgresConnector.php +++ b/src/Connectors/PostgresConnector.php @@ -5,9 +5,10 @@ namespace Tamedevelopers\Database\Connectors; use PDO; +use Exception; use PDOException; -use Tamedevelopers\Database\Constant; use Tamedevelopers\Support\Str; +use Tamedevelopers\Database\Constant; use Tamedevelopers\Database\Schema\Builder; use Tamedevelopers\Database\Connectors\ConnectorInterface; use Tamedevelopers\Database\Connectors\Traits\ConnectorTrait; @@ -38,6 +39,7 @@ class PostgresConnector public function connect(array $config) { // + throw new Exception("Driver cannot be used at the moment! Unsupported driver [{$config['driver']}]."); } diff --git a/src/Connectors/SQLiteConnector.php b/src/Connectors/SQLiteConnector.php index 5babb01..29499b0 100644 --- a/src/Connectors/SQLiteConnector.php +++ b/src/Connectors/SQLiteConnector.php @@ -5,9 +5,10 @@ namespace Tamedevelopers\Database\Connectors; use PDO; +use Exception; use PDOException; -use Tamedevelopers\Database\Constant; use Tamedevelopers\Support\Str; +use Tamedevelopers\Database\Constant; use Tamedevelopers\Database\Schema\Builder; use Tamedevelopers\Database\Connectors\ConnectorInterface; use Tamedevelopers\Database\Connectors\Traits\ConnectorTrait; @@ -38,6 +39,7 @@ class SQLiteConnector public function connect(array $config) { // + throw new Exception("Driver cannot be used at the moment! Unsupported driver [{$config['driver']}]."); } diff --git a/src/DB.php b/src/DB.php index 4f0fb75..c0b7186 100644 --- a/src/DB.php +++ b/src/DB.php @@ -22,7 +22,6 @@ * @method static \Tamedevelopers\Database\Connectors\Connector reconnect(string|null $name = null) * @method static \Tamedevelopers\Database\DatabaseManager disconnect(string|null $name = null) * @method static string getDefaultConnection() - * @method static array getConnection(string|null $name) * @method static \Tamedevelopers\Database\Schema\Builder table(string $table) * @method static \Tamedevelopers\Database\Schema\Builder from(string $table) * @method static \Tamedevelopers\Database\Schema\Builder query() diff --git a/src/DatabaseConnector.php b/src/DatabaseConnector.php index 4ef93e5..770e0b4 100644 --- a/src/DatabaseConnector.php +++ b/src/DatabaseConnector.php @@ -5,6 +5,7 @@ namespace Tamedevelopers\Database; use Tamedevelopers\Support\Str; +use Tamedevelopers\Support\Server; /** * @property-read mixed $table @@ -20,7 +21,7 @@ protected static function getDriverData($data = null) { if(!is_array($data)){ $default = self::getDriverName(); - return config( + return Server::config( "database.connections.{$default}", [] ); @@ -37,14 +38,14 @@ protected static function getDriverData($data = null) protected static function getDriverName($name = null) { if(empty($name)){ - return config("database.default"); + return Server::config("database.default"); } // try to get driver config data - $database = config("database.connections.{$name}"); + $database = Server::config("database.connections.{$name}"); return empty($database) - ? config("database.default") + ? Server::config("database.default") : $name; } diff --git a/src/DatabaseManager.php b/src/DatabaseManager.php index dae7ce8..368a941 100644 --- a/src/DatabaseManager.php +++ b/src/DatabaseManager.php @@ -4,20 +4,17 @@ namespace Tamedevelopers\Database; -use Tamedevelopers\Support\Capsule\FileCache; use Tamedevelopers\Database\Connectors\Connector; -use Tamedevelopers\Support\Server; -class DatabaseManager extends DatabaseConnector { +class DatabaseManager extends DatabaseConnector { /** - * Database Storage Cache Key + * Database Storage Key * This allow us ability to store multiple connections on each instance * @var string */ - const CONNECTION_CACHE = "database.connections."; - + const CONNECTION_KEY = "database.connections."; /** * Connect to a Database @@ -28,89 +25,75 @@ class DatabaseManager extends DatabaseConnector { * @param array $default * [optional] The default value to return if the configuration option is not found * - * @return $this + * @return \Tamedevelopers\Database\Connectors\Connector */ public static function connection($name = null, $default = []) { - $config = self::driverValidator($name); - if (!FileCache::has($config['key'])) { - // create data - $connectionData = self::getDriverData( - config("database.connections.{$config['name']}") - ); - - // merge data - $mergeData = array_merge($connectionData, $default); - - // Cache the connection - FileCache::put( - $config['key'], - self::createDriverData( - $mergeData - ) - ); - } - - return new Connector($config['name']); + [$name, $default] = self::prepareValues( + $name, $default, func_num_args() === 2 + ); + + // connector object + return Connector::addConnection( + name: $name, + data: $default, + ); } /** - * Get Connection data - * - * @param string|null $name - * - [name] of connections\Default name is `default` - * - * @return mixed + * Prepare Values + * + * @param string|null $name + * @param array $default + * @param bool $useDefault + * @return void */ - public static function getConnection($name = null) + private static function prepareValues($name = null, $default = [], $useDefault) { - $key = self::getCacheKey($name); - if (FileCache::has($key)) { - return FileCache::get($key); + // when only one data is passed + // now we just check if data is an array + if(!$useDefault && is_array($name)){ + return [null, $name]; } - return []; + return [$name, $default]; } /** - * Disconnect from a database. + * Reconnect to a database. * * @param string|null $name - * @return void + * + * * @param array $default + * [optional] The default value to return if the configuration option is not found + * + * @return \Tamedevelopers\Database\Connectors\Connector */ - public static function disconnect($name = null) + public static function reconnect($name = null, $default = []) { - $name = empty($name) ? self::getDriverName() : $name; - $key = self::getCacheKey($name); - if (FileCache::has($key)) { - FileCache::forget($key); - } + return self::connection($name, $default); } /** - * Reconnect to a database. + * Disconnect from a database. * * @param string|null $name - * - * * @param array|null $default - * [optional] The default value to return if the configuration option is not found - * - * @return object + * @return void */ - public static function reconnect($name = null, $default = null) + public static function disconnect($name = null) { - return self::connection($name, $default); + Connector::removeFromConnection($name); } /** - * get Cache Key name + * Get Connection Key * * @param string $name * @return string */ - public static function getCacheKey($name = null) + public static function getConnectionKey($name = null) { - return self::CONNECTION_CACHE . $name; + return self::CONNECTION_KEY . $name; } /** @@ -119,12 +102,12 @@ public static function getCacheKey($name = null) * @param string|null $name * @return array */ - private static function driverValidator($name = null) + public static function driverValidator($name = null) { $name = self::getDriverName($name); return [ 'name' => $name, - 'key' => self::getCacheKey($name), + 'key' => self::getConnectionKey($name), ]; } diff --git a/src/Dummy/dummyInit.dum b/src/Dummy/dummyInit.dum index f8cd5ff..012fb3b 100644 --- a/src/Dummy/dummyInit.dum +++ b/src/Dummy/dummyInit.dum @@ -1,7 +1,9 @@ pagination_settings = PAGINATION_CONFIG; + if(defined('TAME_PAGI_CONFIG') && is_bool(TAME_PAGI_CONFIG['allow']) && TAME_PAGI_CONFIG['allow'] === true){ + $this->pagination_settings = TAME_PAGI_CONFIG; }else{ // create a default data $this->pagination_settings = array_merge([ diff --git a/src/Schema/Traits/BuilderTrait.php b/src/Schema/Traits/BuilderTrait.php index 6e8b352..389f564 100644 --- a/src/Schema/Traits/BuilderTrait.php +++ b/src/Schema/Traits/BuilderTrait.php @@ -13,7 +13,7 @@ use Tamedevelopers\Database\Schema\Builder; use Tamedevelopers\Database\Schema\Expression; use Tamedevelopers\Database\Schema\JoinClause; -use Tamedevelopers\Database\Capsule\DebugManager; +use Tamedevelopers\Support\Capsule\DebugManager; use Tamedevelopers\Database\Collections\Collection; use Tamedevelopers\Database\Schema\BuilderCompiler; use Tamedevelopers\Database\Schema\Pagination\Paginator; diff --git a/src/Schema/Traits/MySqlProperties.php b/src/Schema/Traits/MySqlProperties.php index b1e9d26..5a42b1f 100644 --- a/src/Schema/Traits/MySqlProperties.php +++ b/src/Schema/Traits/MySqlProperties.php @@ -22,7 +22,7 @@ trait MySqlProperties{ public $dbManager; /** - * @var \Tamedevelopers\Database\Capsule\Manager + * @var \Tamedevelopers\Support\Capsule\Manager */ public $manager; diff --git a/src/helpers.php b/src/helpers.php index 978f08a..ecd8bc9 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -27,15 +27,17 @@ function autoloader_start($custom_path = null) /** * Get Database * - * @param string $key - * - key of already defined connections - * [dir]/config/database.php + * @param string|null $name + * - [name] of connections in [config/database.php] file + * + * @param array $default + * [optional] The default value to return if the configuration option is not found * * @return \Tamedevelopers\Database\Connectors\Connector */ - function db(?string $key = 'mysql') + function db($name = null, $default = []) { - return DB::connection($key); + return DB::connection($name, $default); } } diff --git a/tests/Model/Admin.php b/tests/Model/Admin.php new file mode 100644 index 0000000..ef95047 --- /dev/null +++ b/tests/Model/Admin.php @@ -0,0 +1,19 @@ +where('active', 1) + ->join('tb_orders_payment', 'tb_orders_payment.user_id', '=', 'tb_user.user_id') + ->join('tb_wallet', 'tb_wallet.user_id', '=', 'tb_user.user_id') + ->limit(10) + ->get(); + } + +} \ No newline at end of file diff --git a/tests/Model/User.php b/tests/Model/User.php index feecb2d..93fb36d 100644 --- a/tests/Model/User.php +++ b/tests/Model/User.php @@ -6,6 +6,7 @@ class User extends Model{ // this model table name will become `users` // you can dump to always see table name + // protected $table = 'users'; public function getUsers() {