55namespace Tamedevelopers \Database \Connectors ;
66
77use PDO ;
8+ use Exception ;
9+ use Tamedevelopers \Support \Server ;
810use Tamedevelopers \Database \Schema \Builder ;
911use Tamedevelopers \Support \Capsule \Manager ;
1012use 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 }
0 commit comments