Skip to content

Commit ed7dffd

Browse files
author
Fredrick Peter
committed
critical update, no usage modification
1 parent 629e0aa commit ed7dffd

19 files changed

+216
-174
lines changed

composer.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
],
1818
"require": {
1919
"php": ">=8.0",
20-
"tamedevelopers/support": "^2.0.0",
21-
"vlucas/phpdotenv": "^5.4.1",
22-
"filp/whoops": "^2.15"
20+
"tamedevelopers/support": "^2.0.0"
2321
},
2422
"autoload": {
2523
"files": [

src/AutoLoader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ public static function configPagination(?array $options = [])
111111
| Adding Pagination Configuration into Constant
112112
|--------------------------------------------------------------------------
113113
*/
114-
if ( ! defined('PAGINATION_CONFIG') ) {
115-
define('PAGINATION_CONFIG', $default);
114+
if ( ! defined('TAME_PAGI_CONFIG') ) {
115+
define('TAME_PAGI_CONFIG', $default);
116116
}
117117
}
118118

src/Capsule/AppManager.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Tamedevelopers\Database\AutoLoader;
1010
use Tamedevelopers\Support\Capsule\Manager;
1111
use Tamedevelopers\Support\Capsule\FileCache;
12-
use Tamedevelopers\Database\Capsule\DebugManager;
12+
use Tamedevelopers\Support\Capsule\DebugManager;
1313

1414
class AppManager{
1515

@@ -63,14 +63,6 @@ public static function bootLoader()
6363
|--------------------------------------------------------------------------
6464
*/
6565
AutoLoader::start();
66-
67-
/*
68-
|--------------------------------------------------------------------------
69-
| Default connection driver is `mysql`
70-
| use DB::connection() \to connection to other connection instance
71-
|--------------------------------------------------------------------------
72-
*/
73-
DB::connection();
7466
}
7567

7668
}

src/Capsule/DebugManager.php

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/Connectors/ConnectionBuilder.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,21 @@ public function __construct($data = null, $name = null, $connection = null)
6969
$this->pdo = $connection['pdo'] ?? $connection['message'];
7070
$this->config = array_merge($connection['config'], ['name' => $name]);
7171
$this->database = $this->config['database'];
72-
$this->tablePrefix = $this->config['prefix'];
72+
$this->tablePrefix = $this->getTablePrefixIfAllowed();
73+
}
74+
}
75+
76+
/**
77+
* Get Table Prefix
78+
* @return string|null
79+
*/
80+
private function getTablePrefixIfAllowed()
81+
{
82+
// if prefixes is set and is `true`
83+
if(isset($this->config['prefix_indexes']) && $this->config['prefix_indexes']){
84+
if(isset($this->config['prefix'])){
85+
return $this->config['prefix'];
86+
}
7387
}
7488
}
7589

src/Connectors/Connector.php

Lines changed: 105 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace Tamedevelopers\Database\Connectors;
66

77
use PDO;
8+
use Exception;
9+
use Tamedevelopers\Support\Server;
810
use Tamedevelopers\Database\Schema\Builder;
911
use Tamedevelopers\Support\Capsule\Manager;
1012
use Tamedevelopers\Database\DatabaseManager;
@@ -25,23 +27,77 @@ class Connector {
2527
*/
2628
private $connection;
2729

30+
/**
31+
* @var array|null
32+
*/
33+
private $default;
34+
2835
/**
2936
* @var string|null
3037
*/
3138
private $name;
3239

40+
/**
41+
* Save instances of all connection
42+
* @var mixed
43+
*/
44+
private static $connections = [];
3345

3446
/**
3547
* Constructors
3648
*
37-
* @param string|null $name\Database connection name
38-
* @param mixed $connection \Connection instance
39-
*
49+
* @param string|null $name - Driver name
50+
* @param mixed $connection - Connection instance
51+
* @param array $data - Default data
4052
*/
41-
public function __construct($name = null, mixed $connection = null)
53+
public function __construct($name = null, $connection = null, $data = [])
4254
{
4355
$this->setConnectionName($name);
4456
$this->setConnection($connection);
57+
$this->setDefaultData($data);
58+
}
59+
60+
/**
61+
* Add to connection instance
62+
*
63+
* @param string|null $name - Driver name
64+
* @param mixed $connection - Connection instance
65+
* @param array $data - Default data
66+
*
67+
* @return \Tamedevelopers\Database\Connectors\Connector
68+
*/
69+
static public function addConnection($name = null, $connection = null, $data = [])
70+
{
71+
$driver = DatabaseManager::driverValidator($name);
72+
73+
// driver name
74+
$driverName = $driver['name'];
75+
76+
// connector object
77+
self::$connections[$driverName] = new self(
78+
connection: $connection,
79+
name: $driverName,
80+
data: $data,
81+
);
82+
83+
return self::$connections[$driverName];
84+
}
85+
86+
/**
87+
* Remove from connection instance
88+
*
89+
* @param string|null $name - Driver name
90+
*
91+
* @return void
92+
*/
93+
static public function removeFromConnection($name = null)
94+
{
95+
$driver = DatabaseManager::driverValidator($name);
96+
97+
// driver name
98+
$driverName = $driver['name'];
99+
100+
unset(self::$connections[$driverName]);
45101
}
46102

47103
/**
@@ -96,8 +152,8 @@ public function configPagination(array $options = [])
96152

97153
// Only if the Global Constant is not yet defined
98154
// If set to allow global use of ENV Autoloader Settings
99-
if(defined('PAGINATION_CONFIG') && Manager::isEnvBool(PAGINATION_CONFIG['allow']) === true){
100-
$paginator->configPagination(PAGINATION_CONFIG);
155+
if(defined('TAME_PAGI_CONFIG') && Manager::isEnvBool(TAME_PAGI_CONFIG['allow']) === true){
156+
$paginator->configPagination(TAME_PAGI_CONFIG);
101157
} else{
102158
$paginator->configPagination($options);
103159
}
@@ -111,18 +167,24 @@ public function configPagination(array $options = [])
111167
*/
112168
private function buidTable($table = null)
113169
{
170+
// begin build
114171
$builder = new Builder;
115172
$builder->manager = new Manager;
116173
$builder->dbManager = new DatabaseManager;
117174

118-
// create instance of self
119-
$instance = new self(
120-
$this->name,
121-
$this->dbConnection(),
122-
);
175+
// get saved connection from $connections array
176+
$instance = self::$connections[$this->name] ?? null;
177+
178+
// There's no connecton instance set
179+
if(empty($instance)){
180+
throw new Exception("There's no active connection! Unknown connection [{$this->name}].");
181+
}
182+
183+
// set connection
184+
$instance->connection = $this->dbConnection();
123185

124186
// setup table name
125-
$builder->from = $this->compileTableWithPrefix($table, $this->getConfig());
187+
$builder->from = $table;
126188

127189
// building of table name is only called once
128190
// so we will build the instance of Connection data into the
@@ -161,8 +223,9 @@ public function getTablePrefix()
161223
public function dbConnection($mode = null)
162224
{
163225
// get connection data
164-
$conn = DatabaseManager::getConnection($this->name);
165-
226+
// merge data to default if provided, before we try to connect
227+
$conn = self::getConnectionFromDatabaseFile($this->name, $this->default);
228+
166229
// connection data
167230
$connData = self::createConnector($conn['driver'])->connect($conn);
168231

@@ -202,25 +265,25 @@ public function getDatabaseName()
202265
*/
203266
public function getConfig()
204267
{
205-
return DatabaseManager::getConnection($this->name);
268+
return self::getConnectionFromDatabaseFile($this->name, $this->default);
206269
}
207270

208271
/**
209-
* Get Table Name
210-
* @param string $table
211-
* @param array $data
212-
* @return string
272+
* Get Connection data
273+
*
274+
* @param string|null $name
275+
* @param array|null $default
276+
*
277+
* @return array
213278
*/
214-
private static function compileTableWithPrefix($table = null, ?array $data = null)
279+
private static function getConnectionFromDatabaseFile($name = null, $default = [])
215280
{
216-
// check prefixes
217-
if(isset($data['prefix_indexes']) && $data['prefix_indexes']){
218-
if(isset($data['prefix'])){
219-
$table = "{$data['prefix']}{$table}";
220-
}
221-
}
281+
$data = Server::config(
282+
DatabaseManager::getConnectionKey($name),
283+
[]
284+
);
222285

223-
return $table;
286+
return array_merge($data, $default ?? []);
224287
}
225288

226289
/**
@@ -247,6 +310,19 @@ private function setConnection($connection = null)
247310
}
248311
}
249312

313+
/**
314+
* Set default connection data
315+
*
316+
* @param array $default
317+
* @return void
318+
*/
319+
private function setDefaultData($default = [])
320+
{
321+
if(!empty($default)){
322+
$this->default = $default;
323+
}
324+
}
325+
250326
/**
251327
* Set connection connection name
252328
*
@@ -256,7 +332,7 @@ private function setConnection($connection = null)
256332
private function setConnectionName($name = null)
257333
{
258334
$this->name = empty($name)
259-
? config("database.default")
335+
? Server::config("database.default")
260336
: $name;
261337
}
262338

@@ -270,7 +346,7 @@ private function isModelDriverCreated()
270346
{
271347
if(self::isModelExtended()){
272348
$this->setConnectionName();
273-
$key = DatabaseManager::getCacheKey($this->name);
349+
$key = DatabaseManager::getConnectionKey($this->name);
274350
if (!FileCache::exists($key)) {
275351
DatabaseManager::connection($this->name);
276352
}

src/Connectors/PostgresConnector.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
namespace Tamedevelopers\Database\Connectors;
66

77
use PDO;
8+
use Exception;
89
use PDOException;
9-
use Tamedevelopers\Database\Constant;
1010
use Tamedevelopers\Support\Str;
11+
use Tamedevelopers\Database\Constant;
1112
use Tamedevelopers\Database\Schema\Builder;
1213
use Tamedevelopers\Database\Connectors\ConnectorInterface;
1314
use Tamedevelopers\Database\Connectors\Traits\ConnectorTrait;
@@ -38,6 +39,7 @@ class PostgresConnector
3839
public function connect(array $config)
3940
{
4041
//
42+
throw new Exception("Driver cannot be used at the moment! Unsupported driver [{$config['driver']}].");
4143
}
4244

4345

src/Connectors/SQLiteConnector.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
namespace Tamedevelopers\Database\Connectors;
66

77
use PDO;
8+
use Exception;
89
use PDOException;
9-
use Tamedevelopers\Database\Constant;
1010
use Tamedevelopers\Support\Str;
11+
use Tamedevelopers\Database\Constant;
1112
use Tamedevelopers\Database\Schema\Builder;
1213
use Tamedevelopers\Database\Connectors\ConnectorInterface;
1314
use Tamedevelopers\Database\Connectors\Traits\ConnectorTrait;
@@ -38,6 +39,7 @@ class SQLiteConnector
3839
public function connect(array $config)
3940
{
4041
//
42+
throw new Exception("Driver cannot be used at the moment! Unsupported driver [{$config['driver']}].");
4143
}
4244

4345

src/DB.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
* @method static \Tamedevelopers\Database\Connectors\Connector reconnect(string|null $name = null)
2323
* @method static \Tamedevelopers\Database\DatabaseManager disconnect(string|null $name = null)
2424
* @method static string getDefaultConnection()
25-
* @method static array getConnection(string|null $name)
2625
* @method static \Tamedevelopers\Database\Schema\Builder table(string $table)
2726
* @method static \Tamedevelopers\Database\Schema\Builder from(string $table)
2827
* @method static \Tamedevelopers\Database\Schema\Builder query()

0 commit comments

Comments
 (0)