Skip to content

Commit 629e0aa

Browse files
author
Fredrick Peter
committed
DB::connection upgrade
1 parent 1e3ce2a commit 629e0aa

File tree

4 files changed

+37
-21
lines changed

4 files changed

+37
-21
lines changed

src/Connectors/Connector.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,19 +154,24 @@ public function getTablePrefix()
154154
/**
155155
* Get Connection data
156156
*
157-
* @return array
157+
* @param string|null $mode
158+
*
159+
* @return mixed
158160
*/
159-
public function dbConnection()
161+
public function dbConnection($mode = null)
160162
{
161163
// get connection data
162164
$conn = DatabaseManager::getConnection($this->name);
163165

164166
// connection data
165-
$conn = self::createConnector($conn['driver'])->connect($conn);
167+
$connData = self::createConnector($conn['driver'])->connect($conn);
166168

167-
return array_merge($conn, [
169+
// merge data
170+
$data = array_merge($connData ?? [], [
168171
'name' => $this->name,
169172
]);
173+
174+
return $data[$mode] ?? $data;
170175
}
171176

172177
/**
@@ -177,7 +182,7 @@ public function dbConnection()
177182
*/
178183
public function getPDO()
179184
{
180-
return $this->dbConnection($this->name)['pdo'];
185+
return $this->dbConnection('pdo');
181186
}
182187

183188
/**
@@ -250,7 +255,9 @@ private function setConnection($connection = null)
250255
*/
251256
private function setConnectionName($name = null)
252257
{
253-
$this->name = empty($name) ? 'default' : $name;
258+
$this->name = empty($name)
259+
? config("database.default")
260+
: $name;
254261
}
255262

256263
/**

src/DatabaseConnector.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ class DatabaseConnector{
1313

1414
/**
1515
* Find Database Connection data
16-
* @param mixed $data
16+
* @param array|null $data
1717
* @return array
1818
*/
1919
protected static function getDriverData($data = null)
2020
{
2121
if(!is_array($data)){
22-
$default = config("database.default");
22+
$default = self::getDriverName();
2323
return config(
24-
"database.connections.{$default}"
24+
"database.connections.{$default}",
25+
[]
2526
);
2627
}
2728

@@ -35,11 +36,16 @@ protected static function getDriverData($data = null)
3536
*/
3637
protected static function getDriverName($name = null)
3738
{
39+
if(empty($name)){
40+
return config("database.default");
41+
}
42+
3843
// try to get driver config data
39-
$config = config(
40-
"database.connections.{$name}"
41-
);
42-
return empty($config) ? 'default' : $name;
44+
$database = config("database.connections.{$name}");
45+
46+
return empty($database)
47+
? config("database.default")
48+
: $name;
4349
}
4450

4551
/**

src/DatabaseManager.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Tamedevelopers\Support\Capsule\FileCache;
88
use Tamedevelopers\Database\Connectors\Connector;
9+
use Tamedevelopers\Support\Server;
910

1011
class DatabaseManager extends DatabaseConnector {
1112

@@ -29,22 +30,24 @@ class DatabaseManager extends DatabaseConnector {
2930
*
3031
* @return $this
3132
*/
32-
public static function connection($name = null, ?array $default = [])
33+
public static function connection($name = null, $default = [])
3334
{
3435
$config = self::driverValidator($name);
3536
if (!FileCache::has($config['key'])) {
3637
// create data
37-
$data = self::getDriverData(
38+
$connectionData = self::getDriverData(
3839
config("database.connections.{$config['name']}")
3940
);
4041

4142
// merge data
42-
$mergeData = array_merge($data ?? [], $default ?? []);
43+
$mergeData = array_merge($connectionData, $default);
4344

4445
// Cache the connection
4546
FileCache::put(
4647
$config['key'],
47-
self::createDriverData($mergeData)
48+
self::createDriverData(
49+
$mergeData
50+
)
4851
);
4952
}
5053

@@ -77,7 +80,7 @@ public static function getConnection($name = null)
7780
*/
7881
public static function disconnect($name = null)
7982
{
80-
$name = empty($name) ? 'default' : $name;
83+
$name = empty($name) ? self::getDriverName() : $name;
8184
$key = self::getCacheKey($name);
8285
if (FileCache::has($key)) {
8386
FileCache::forget($key);
@@ -89,12 +92,12 @@ public static function disconnect($name = null)
8992
*
9093
* @param string|null $name
9194
*
92-
* * @param mixed $default
95+
* * @param array|null $default
9396
* [optional] The default value to return if the configuration option is not found
9497
*
9598
* @return object
9699
*/
97-
public static function reconnect($name = null, mixed $default = null)
100+
public static function reconnect($name = null, $default = null)
98101
{
99102
return self::connection($name, $default);
100103
}

src/helpers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function autoloader_start($custom_path = null)
3333
*
3434
* @return \Tamedevelopers\Database\Connectors\Connector
3535
*/
36-
function db(?string $key = 'default')
36+
function db(?string $key = 'mysql')
3737
{
3838
return DB::connection($key);
3939
}

0 commit comments

Comments
 (0)