Skip to content

Commit 31f7014

Browse files
authored
Merge pull request #4040 from mostafakhudair/initdriver-db-method
Add initDriver Method
2 parents 42b7c05 + 094ae63 commit 31f7014

File tree

1 file changed

+49
-30
lines changed

1 file changed

+49
-30
lines changed

system/Database/Database.php

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
class Database
2222
{
2323
/**
24-
* Maintains an array of the instances of all connections
25-
* that have been created. Helps to keep track of all open
26-
* connections for performance monitoring, logging, etc.
24+
* Maintains an array of the instances of all connections that have
25+
* been created.
26+
*
27+
* Helps to keep track of all open connections for performance
28+
* monitoring, logging, etc.
2729
*
2830
* @var array
2931
*/
@@ -32,13 +34,16 @@ class Database
3234
//--------------------------------------------------------------------
3335

3436
/**
35-
* Parses the connection binds and returns an instance of
36-
* the driver ready to go.
37+
* Parses the connection binds and returns an instance of the driver
38+
* ready to go.
3739
*
3840
* @param array $params
3941
* @param string $alias
4042
*
41-
* @return mixed
43+
* @return mixed
44+
*
45+
* @throws InvalidArgumentException
46+
*
4247
* @internal param bool $useBuilder
4348
*/
4449
public function load(array $params = [], string $alias = '')
@@ -60,60 +65,50 @@ public function load(array $params = [], string $alias = '')
6065
throw new InvalidArgumentException('You have not selected a database type to connect to.');
6166
}
6267

63-
$className = strpos($params['DBDriver'], '\\') === false
64-
? '\CodeIgniter\Database\\' . $params['DBDriver'] . '\\Connection'
65-
: $params['DBDriver'] . '\\Connection';
66-
67-
$class = new $className($params);
68-
6968
// Store the connection
70-
$this->connections[$alias] = $class;
69+
$this->connections[$alias] = $this->initDriver($params['DBDriver'], 'Connection', $params);
7170

7271
return $this->connections[$alias];
7372
}
7473

7574
//--------------------------------------------------------------------
7675

7776
/**
78-
* Creates a new Forge instance for the current database type.
77+
* Creates a Forge instance for the current database type.
7978
*
80-
* @param ConnectionInterface|BaseConnection $db
79+
* @param ConnectionInterface $db
8180
*
82-
* @return mixed
81+
* @return object
8382
*/
84-
public function loadForge(ConnectionInterface $db)
83+
public function loadForge(ConnectionInterface $db): object
8584
{
86-
$className = strpos($db->DBDriver, '\\') === false ? '\CodeIgniter\Database\\' . $db->DBDriver . '\\Forge' : $db->DBDriver . '\\Forge';
87-
88-
// Make sure a connection exists
85+
// Initialize database connection if not exists.
8986
if (! $db->connID)
9087
{
9188
$db->initialize();
9289
}
9390

94-
return new $className($db);
91+
return $this->initDriver($db->DBDriver, 'Forge', $db);
9592
}
9693

9794
//--------------------------------------------------------------------
9895

9996
/**
100-
* Loads the Database Utilities class.
97+
* Creates a Utils instance for the current database type.
10198
*
102-
* @param ConnectionInterface|BaseConnection $db
99+
* @param ConnectionInterface $db
103100
*
104-
* @return mixed
101+
* @return object
105102
*/
106-
public function loadUtils(ConnectionInterface $db)
103+
public function loadUtils(ConnectionInterface $db): object
107104
{
108-
$className = strpos($db->DBDriver, '\\') === false ? '\CodeIgniter\Database\\' . $db->DBDriver . '\\Utils' : $db->DBDriver . '\\Utils';
109-
110-
// Make sure a connection exists
105+
// Initialize database connection if not exists.
111106
if (! $db->connID)
112107
{
113108
$db->initialize();
114109
}
115110

116-
return new $className($db);
111+
return $this->initDriver($db->DBDriver, 'Utils', $db);
117112
}
118113

119114
//--------------------------------------------------------------------
@@ -124,11 +119,14 @@ public function loadUtils(ConnectionInterface $db)
124119
* @param array $params
125120
*
126121
* @return array
122+
*
127123
* @throws InvalidArgumentException
128124
*/
129125
protected function parseDSN(array $params): array
130126
{
131-
if (($dsn = parse_url($params['DSN'])) === false)
127+
$dsn = parse_url($params['DSN']);
128+
129+
if (! $dsn)
132130
{
133131
throw new InvalidArgumentException('Your DSN connection string is invalid.');
134132
}
@@ -163,4 +161,25 @@ protected function parseDSN(array $params): array
163161
}
164162

165163
//--------------------------------------------------------------------
164+
165+
/**
166+
* Initialize database driver.
167+
*
168+
* @param string $driver Database driver name (e.g. 'MySQLi')
169+
* @param string $class Database class name (e.g. 'Forge')
170+
* @param array|object $argument
171+
*
172+
* @return object
173+
*/
174+
protected function initDriver(string $driver, string $class, $argument): object
175+
{
176+
$class = $driver . '\\' . $class;
177+
178+
if (strpos($driver, '\\') === false)
179+
{
180+
$class = "CodeIgniter\Database\\{$class}";
181+
}
182+
183+
return new $class($argument);
184+
}
166185
}

0 commit comments

Comments
 (0)