Skip to content

Commit

Permalink
DB::connection upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
Fredrick Peter committed Oct 9, 2023
1 parent 1e3ce2a commit 629e0aa
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
19 changes: 13 additions & 6 deletions src/Connectors/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -177,7 +182,7 @@ public function dbConnection()
*/
public function getPDO()
{
return $this->dbConnection($this->name)['pdo'];
return $this->dbConnection('pdo');
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down
20 changes: 13 additions & 7 deletions src/DatabaseConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
[]
);
}

Expand All @@ -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;
}

/**
Expand Down
17 changes: 10 additions & 7 deletions src/DatabaseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Tamedevelopers\Support\Capsule\FileCache;
use Tamedevelopers\Database\Connectors\Connector;
use Tamedevelopers\Support\Server;

class DatabaseManager extends DatabaseConnector {

Expand All @@ -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
)
);
}

Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 629e0aa

Please sign in to comment.