From 629e0aa2616ae8274692e93fb261a793baa5fb2e Mon Sep 17 00:00:00 2001 From: Fredrick Peter Date: Mon, 9 Oct 2023 05:07:08 +0100 Subject: [PATCH] DB::connection upgrade --- src/Connectors/Connector.php | 19 +++++++++++++------ src/DatabaseConnector.php | 20 +++++++++++++------- src/DatabaseManager.php | 17 ++++++++++------- src/helpers.php | 2 +- 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/src/Connectors/Connector.php b/src/Connectors/Connector.php index 8238385..466f61a 100644 --- a/src/Connectors/Connector.php +++ b/src/Connectors/Connector.php @@ -154,19 +154,24 @@ public function getTablePrefix() /** * Get Connection data * - * @return array + * @param string|null $mode + * + * @return mixed */ - public function dbConnection() + public function dbConnection($mode = null) { // get connection data $conn = DatabaseManager::getConnection($this->name); // connection data - $conn = self::createConnector($conn['driver'])->connect($conn); + $connData = self::createConnector($conn['driver'])->connect($conn); - return array_merge($conn, [ + // merge data + $data = array_merge($connData ?? [], [ 'name' => $this->name, ]); + + return $data[$mode] ?? $data; } /** @@ -177,7 +182,7 @@ public function dbConnection() */ public function getPDO() { - return $this->dbConnection($this->name)['pdo']; + return $this->dbConnection('pdo'); } /** @@ -250,7 +255,9 @@ private function setConnection($connection = null) */ private function setConnectionName($name = null) { - $this->name = empty($name) ? 'default' : $name; + $this->name = empty($name) + ? config("database.default") + : $name; } /** diff --git a/src/DatabaseConnector.php b/src/DatabaseConnector.php index 71e169a..4ef93e5 100644 --- a/src/DatabaseConnector.php +++ b/src/DatabaseConnector.php @@ -13,15 +13,16 @@ class DatabaseConnector{ /** * Find Database Connection data - * @param mixed $data + * @param array|null $data * @return array */ protected static function getDriverData($data = null) { if(!is_array($data)){ - $default = config("database.default"); + $default = self::getDriverName(); return config( - "database.connections.{$default}" + "database.connections.{$default}", + [] ); } @@ -35,11 +36,16 @@ protected static function getDriverData($data = null) */ protected static function getDriverName($name = null) { + if(empty($name)){ + return config("database.default"); + } + // try to get driver config data - $config = config( - "database.connections.{$name}" - ); - return empty($config) ? 'default' : $name; + $database = config("database.connections.{$name}"); + + return empty($database) + ? config("database.default") + : $name; } /** diff --git a/src/DatabaseManager.php b/src/DatabaseManager.php index d0efcd5..dae7ce8 100644 --- a/src/DatabaseManager.php +++ b/src/DatabaseManager.php @@ -6,6 +6,7 @@ use Tamedevelopers\Support\Capsule\FileCache; use Tamedevelopers\Database\Connectors\Connector; +use Tamedevelopers\Support\Server; class DatabaseManager extends DatabaseConnector { @@ -29,22 +30,24 @@ class DatabaseManager extends DatabaseConnector { * * @return $this */ - public static function connection($name = null, ?array $default = []) + public static function connection($name = null, $default = []) { $config = self::driverValidator($name); if (!FileCache::has($config['key'])) { // create data - $data = self::getDriverData( + $connectionData = self::getDriverData( config("database.connections.{$config['name']}") ); // merge data - $mergeData = array_merge($data ?? [], $default ?? []); + $mergeData = array_merge($connectionData, $default); // Cache the connection FileCache::put( $config['key'], - self::createDriverData($mergeData) + self::createDriverData( + $mergeData + ) ); } @@ -77,7 +80,7 @@ public static function getConnection($name = null) */ public static function disconnect($name = null) { - $name = empty($name) ? 'default' : $name; + $name = empty($name) ? self::getDriverName() : $name; $key = self::getCacheKey($name); if (FileCache::has($key)) { FileCache::forget($key); @@ -89,12 +92,12 @@ public static function disconnect($name = null) * * @param string|null $name * - * * @param mixed $default + * * @param array|null $default * [optional] The default value to return if the configuration option is not found * * @return object */ - public static function reconnect($name = null, mixed $default = null) + public static function reconnect($name = null, $default = null) { return self::connection($name, $default); } diff --git a/src/helpers.php b/src/helpers.php index adfb57d..978f08a 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -33,7 +33,7 @@ function autoloader_start($custom_path = null) * * @return \Tamedevelopers\Database\Connectors\Connector */ - function db(?string $key = 'default') + function db(?string $key = 'mysql') { return DB::connection($key); }