1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\DBAL; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\EventManager; |
6
|
|
|
use Doctrine\DBAL\Driver\DrizzlePDOMySql\Driver as DrizzlePDOMySQLDriver; |
7
|
|
|
use Doctrine\DBAL\Driver\IBMDB2\DB2Driver; |
8
|
|
|
use Doctrine\DBAL\Driver\Mysqli\Driver as MySQLiDriver; |
9
|
|
|
use Doctrine\DBAL\Driver\OCI8\Driver as OCI8Driver; |
10
|
|
|
use Doctrine\DBAL\Driver\PDOMySql\Driver as PDOMySQLDriver; |
11
|
|
|
use Doctrine\DBAL\Driver\PDOOracle\Driver as PDOOCIDriver; |
12
|
|
|
use Doctrine\DBAL\Driver\PDOPgSql\Driver as PDOPgSQLDriver; |
13
|
|
|
use Doctrine\DBAL\Driver\PDOSqlite\Driver as PDOSQLiteDriver; |
14
|
|
|
use Doctrine\DBAL\Driver\PDOSqlsrv\Driver as PDOSQLSrvDriver; |
15
|
|
|
use Doctrine\DBAL\Driver\SQLAnywhere\Driver as SQLAnywhereDriver; |
16
|
|
|
use Doctrine\DBAL\Driver\SQLSrv\Driver as SQLSrvDriver; |
17
|
|
|
use PDO; |
18
|
|
|
use function array_keys; |
19
|
|
|
use function array_map; |
20
|
|
|
use function array_merge; |
21
|
|
|
use function assert; |
22
|
|
|
use function class_implements; |
23
|
|
|
use function in_array; |
24
|
|
|
use function is_string; |
25
|
|
|
use function is_subclass_of; |
26
|
|
|
use function parse_str; |
27
|
|
|
use function parse_url; |
28
|
|
|
use function preg_replace; |
29
|
|
|
use function str_replace; |
30
|
|
|
use function strpos; |
31
|
|
|
use function substr; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Factory for creating Doctrine\DBAL\Connection instances. |
35
|
|
|
*/ |
36
|
|
|
final class DriverManager |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* List of supported drivers and their mappings to the driver classes. |
40
|
|
|
* |
41
|
|
|
* To add your own driver use the 'driverClass' parameter to |
42
|
|
|
* {@link DriverManager::getConnection()}. |
43
|
|
|
* |
44
|
|
|
* @var string[] |
45
|
|
|
*/ |
46
|
|
|
private static $_driverMap = [ |
47
|
|
|
'pdo_mysql' => PDOMySQLDriver::class, |
48
|
|
|
'pdo_sqlite' => PDOSQLiteDriver::class, |
49
|
|
|
'pdo_pgsql' => PDOPgSQLDriver::class, |
50
|
|
|
'pdo_oci' => PDOOCIDriver::class, |
51
|
|
|
'oci8' => OCI8Driver::class, |
52
|
|
|
'ibm_db2' => DB2Driver::class, |
53
|
|
|
'pdo_sqlsrv' => PDOSQLSrvDriver::class, |
54
|
|
|
'mysqli' => MySQLiDriver::class, |
55
|
|
|
'drizzle_pdo_mysql' => DrizzlePDOMySQLDriver::class, |
56
|
|
|
'sqlanywhere' => SQLAnywhereDriver::class, |
57
|
|
|
'sqlsrv' => SQLSrvDriver::class, |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* List of URL schemes from a database URL and their mappings to driver. |
62
|
|
|
* |
63
|
|
|
* @var string[] |
64
|
|
|
*/ |
65
|
|
|
private static $driverSchemeAliases = [ |
66
|
|
|
'db2' => 'ibm_db2', |
67
|
|
|
'mssql' => 'pdo_sqlsrv', |
68
|
|
|
'mysql' => 'pdo_mysql', |
69
|
|
|
'mysql2' => 'pdo_mysql', // Amazon RDS, for some weird reason |
70
|
|
|
'postgres' => 'pdo_pgsql', |
71
|
|
|
'postgresql' => 'pdo_pgsql', |
72
|
|
|
'pgsql' => 'pdo_pgsql', |
73
|
|
|
'sqlite' => 'pdo_sqlite', |
74
|
|
|
'sqlite3' => 'pdo_sqlite', |
75
|
|
|
]; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Private constructor. This class cannot be instantiated. |
79
|
|
|
*/ |
80
|
|
|
private function __construct() |
81
|
|
|
{ |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Creates a connection object based on the specified parameters. |
86
|
|
|
* This method returns a Doctrine\DBAL\Connection which wraps the underlying |
87
|
|
|
* driver connection. |
88
|
|
|
* |
89
|
|
|
* $params must contain at least one of the following. |
90
|
|
|
* |
91
|
|
|
* Either 'driver' with one of the array keys of {@link $_driverMap}, |
92
|
|
|
* OR 'driverClass' that contains the full class name (with namespace) of the |
93
|
|
|
* driver class to instantiate. |
94
|
|
|
* |
95
|
|
|
* Other (optional) parameters: |
96
|
|
|
* |
97
|
|
|
* <b>user (string)</b>: |
98
|
|
|
* The username to use when connecting. |
99
|
|
|
* |
100
|
|
|
* <b>password (string)</b>: |
101
|
|
|
* The password to use when connecting. |
102
|
|
|
* |
103
|
|
|
* <b>driverOptions (array)</b>: |
104
|
|
|
* Any additional driver-specific options for the driver. These are just passed |
105
|
|
|
* through to the driver. |
106
|
|
|
* |
107
|
|
|
* <b>pdo</b>: |
108
|
|
|
* You can pass an existing PDO instance through this parameter. The PDO |
109
|
|
|
* instance will be wrapped in a Doctrine\DBAL\Connection. |
110
|
|
|
* |
111
|
|
|
* <b>wrapperClass</b>: |
112
|
|
|
* You may specify a custom wrapper class through the 'wrapperClass' |
113
|
|
|
* parameter but this class MUST inherit from Doctrine\DBAL\Connection. |
114
|
|
|
* |
115
|
|
|
* <b>driverClass</b>: |
116
|
|
|
* The driver class to use. |
117
|
|
|
* |
118
|
|
|
* @param mixed[] $params The parameters. |
119
|
|
|
* @param Configuration|null $config The configuration to use. |
120
|
|
|
* @param EventManager|null $eventManager The event manager to use. |
121
|
|
|
* |
122
|
|
|
* @throws DBALException |
123
|
|
|
*/ |
124
|
663 |
|
public static function getConnection( |
125
|
|
|
array $params, |
126
|
|
|
?Configuration $config = null, |
127
|
|
|
?EventManager $eventManager = null |
128
|
|
|
) : Connection { |
129
|
|
|
// create default config and event manager, if not set |
130
|
663 |
|
if (! $config) { |
131
|
649 |
|
$config = new Configuration(); |
132
|
|
|
} |
133
|
663 |
|
if (! $eventManager) { |
134
|
649 |
|
$eventManager = new EventManager(); |
135
|
|
|
} |
136
|
|
|
|
137
|
3714 |
|
$params = self::parseDatabaseUrl($params); |
138
|
|
|
|
139
|
|
|
// URL support for MasterSlaveConnection |
140
|
657 |
|
if (isset($params['master'])) { |
141
|
217 |
|
$params['master'] = self::parseDatabaseUrl($params['master']); |
142
|
|
|
} |
143
|
3051 |
|
|
144
|
3694 |
|
if (isset($params['slaves'])) { |
145
|
217 |
|
foreach ($params['slaves'] as $key => $slaveParams) { |
146
|
3268 |
|
$params['slaves'][$key] = self::parseDatabaseUrl($slaveParams); |
147
|
3037 |
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
3051 |
|
// URL support for PoolingShardConnection |
151
|
657 |
|
if (isset($params['global'])) { |
152
|
242 |
|
$params['global'] = self::parseDatabaseUrl($params['global']); |
153
|
3045 |
|
} |
154
|
1586 |
|
|
155
|
657 |
|
if (isset($params['shards'])) { |
156
|
242 |
|
foreach ($params['shards'] as $key => $shardParams) { |
157
|
3287 |
|
$params['shards'][$key] = self::parseDatabaseUrl($shardParams); |
158
|
1586 |
|
} |
159
|
1586 |
|
} |
160
|
|
|
|
161
|
|
|
// check for existing pdo object |
162
|
657 |
|
if (isset($params['pdo']) && ! $params['pdo'] instanceof PDO) { |
163
|
247 |
|
throw DBALException::invalidPdoInstance(); |
164
|
3045 |
|
} |
165
|
1592 |
|
|
166
|
655 |
|
if (isset($params['pdo'])) { |
167
|
254 |
|
$params['pdo']->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
168
|
3299 |
|
$params['driver'] = 'pdo_' . $params['pdo']->getAttribute(PDO::ATTR_DRIVER_NAME); |
169
|
1592 |
|
} else { |
170
|
2237 |
|
self::_checkParams($params); |
171
|
|
|
} |
172
|
|
|
|
173
|
649 |
|
$className = $params['driverClass'] ?? self::$_driverMap[$params['driver']]; |
174
|
|
|
|
175
|
3694 |
|
$driver = new $className(); |
176
|
1806 |
|
|
177
|
649 |
|
$wrapperClass = Connection::class; |
178
|
649 |
|
if (isset($params['wrapperClass'])) { |
179
|
3318 |
|
if (! is_subclass_of($params['wrapperClass'], $wrapperClass)) { |
180
|
2020 |
|
throw DBALException::invalidWrapperClass($params['wrapperClass']); |
181
|
1794 |
|
} |
182
|
|
|
|
183
|
3306 |
|
$wrapperClass = $params['wrapperClass']; |
184
|
|
|
} |
185
|
|
|
|
186
|
3684 |
|
return new $wrapperClass($params, $driver, $config, $eventManager); |
187
|
|
|
} |
188
|
3037 |
|
|
189
|
|
|
/** |
190
|
3037 |
|
* Returns the list of supported drivers. |
191
|
3037 |
|
* |
192
|
1720 |
|
* @return string[] |
193
|
1652 |
|
*/ |
194
|
|
|
public static function getAvailableDrivers() : array |
195
|
|
|
{ |
196
|
1718 |
|
return array_keys(self::$_driverMap); |
197
|
|
|
} |
198
|
|
|
|
199
|
3035 |
|
/** |
200
|
|
|
* Checks the list of parameters. |
201
|
|
|
* |
202
|
|
|
* @param mixed[] $params The list of parameters. |
203
|
|
|
* |
204
|
|
|
* @throws DBALException |
205
|
|
|
*/ |
206
|
645 |
|
private static function _checkParams(array $params) : void |
207
|
|
|
{ |
208
|
|
|
// check existence of mandatory parameters |
209
|
|
|
|
210
|
|
|
// driver |
211
|
645 |
|
if (! isset($params['driver']) && ! isset($params['driverClass'])) { |
212
|
238 |
|
throw DBALException::driverRequired(); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
// check validity of parameters |
216
|
|
|
|
217
|
|
|
// driver |
218
|
643 |
|
if (isset($params['driver']) && ! isset(self::$_driverMap[$params['driver']])) { |
219
|
3268 |
|
throw DBALException::unknownDriver($params['driver'], array_keys(self::$_driverMap)); |
220
|
|
|
} |
221
|
|
|
|
222
|
641 |
|
if (isset($params['driverClass']) && ! in_array(Driver::class, class_implements($params['driverClass'], true))) { |
223
|
223 |
|
throw DBALException::invalidDriverClass($params['driverClass']); |
224
|
3033 |
|
} |
225
|
2379 |
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Normalizes the given connection URL path. |
229
|
|
|
* |
230
|
|
|
* @return string The normalized connection URL path |
231
|
3031 |
|
*/ |
232
|
1987 |
|
private static function normalizeDatabaseUrlPath(string $urlPath) : string |
233
|
|
|
{ |
234
|
|
|
// Trim leading slash from URL path. |
235
|
3298 |
|
return substr($urlPath, 1); |
236
|
1630 |
|
} |
237
|
|
|
|
238
|
3027 |
|
/** |
239
|
|
|
* Extracts parts from a database URL, if present, and returns an |
240
|
|
|
* updated list of parameters. |
241
|
|
|
* |
242
|
|
|
* @param mixed[] $params The list of parameters. |
243
|
|
|
* |
244
|
|
|
* @return mixed[] A modified list of parameters with info from a database |
245
|
1638 |
|
* URL extracted into indidivual parameter parts. |
246
|
|
|
* |
247
|
|
|
* @throws DBALException |
248
|
1638 |
|
*/ |
249
|
663 |
|
private static function parseDatabaseUrl(array $params) : array |
250
|
|
|
{ |
251
|
663 |
|
if (! isset($params['url'])) { |
252
|
607 |
|
return $params; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
// (pdo_)?sqlite3?:///... => (pdo_)?sqlite3?://localhost/... or else the URL will be invalid |
256
|
275 |
|
$url = preg_replace('#^((?:pdo_)?sqlite3?):///#', '$1://localhost/', $params['url']); |
257
|
275 |
|
assert(is_string($url)); |
258
|
|
|
|
259
|
275 |
|
$url = parse_url($url); |
260
|
|
|
|
261
|
275 |
|
if ($url === false) { |
262
|
3051 |
|
throw new DBALException('Malformed parameter "url".'); |
263
|
|
|
} |
264
|
3051 |
|
|
265
|
3270 |
|
$url = array_map('rawurldecode', $url); |
266
|
|
|
|
267
|
|
|
// If we have a connection URL, we have to unset the default PDO instance connection parameter (if any) |
268
|
|
|
// as we cannot merge connection details from the URL into the PDO instance (URL takes precedence). |
269
|
1919 |
|
unset($params['pdo']); |
270
|
1644 |
|
|
271
|
275 |
|
$params = self::parseDatabaseUrlScheme($url, $params); |
272
|
1644 |
|
|
273
|
269 |
|
if (isset($url['host'])) { |
274
|
1913 |
|
$params['host'] = $url['host']; |
275
|
|
|
} |
276
|
269 |
|
if (isset($url['port'])) { |
277
|
221 |
|
$params['port'] = $url['port']; |
278
|
1644 |
|
} |
279
|
269 |
|
if (isset($url['user'])) { |
280
|
257 |
|
$params['user'] = $url['user']; |
281
|
|
|
} |
282
|
1913 |
|
if (isset($url['pass'])) { |
283
|
257 |
|
$params['password'] = $url['pass']; |
284
|
1644 |
|
} |
285
|
|
|
|
286
|
1907 |
|
$params = self::parseDatabaseUrlPath($url, $params); |
287
|
1907 |
|
$params = self::parseDatabaseUrlQuery($url, $params); |
288
|
|
|
|
289
|
1907 |
|
return $params; |
290
|
1590 |
|
} |
291
|
|
|
|
292
|
1638 |
|
/** |
293
|
1626 |
|
* Parses the given connection URL and resolves the given connection parameters. |
294
|
|
|
* |
295
|
1638 |
|
* Assumes that the connection URL scheme is already parsed and resolved into the given connection parameters |
296
|
1626 |
|
* via {@link parseDatabaseUrlScheme}. |
297
|
|
|
* |
298
|
|
|
* @see parseDatabaseUrlScheme |
299
|
1638 |
|
* |
300
|
1638 |
|
* @param mixed[] $url The URL parts to evaluate. |
301
|
|
|
* @param mixed[] $params The connection parameters to resolve. |
302
|
1638 |
|
* |
303
|
|
|
* @return mixed[] The resolved connection parameters. |
304
|
|
|
*/ |
305
|
269 |
|
private static function parseDatabaseUrlPath(array $url, array $params) : array |
306
|
|
|
{ |
307
|
269 |
|
if (! isset($url['path'])) { |
308
|
|
|
return $params; |
309
|
|
|
} |
310
|
|
|
|
311
|
269 |
|
$url['path'] = self::normalizeDatabaseUrlPath($url['path']); |
312
|
|
|
|
313
|
|
|
// If we do not have a known DBAL driver, we do not know any connection URL path semantics to evaluate |
314
|
|
|
// and therefore treat the path as regular DBAL connection URL path. |
315
|
269 |
|
if (! isset($params['driver'])) { |
316
|
175 |
|
return self::parseRegularDatabaseUrlPath($url, $params); |
317
|
|
|
} |
318
|
1638 |
|
|
319
|
267 |
|
if (strpos($params['driver'], 'sqlite') !== false) { |
320
|
1853 |
|
return self::parseSqliteDatabaseUrlPath($url, $params); |
321
|
|
|
} |
322
|
|
|
|
323
|
255 |
|
return self::parseRegularDatabaseUrlPath($url, $params); |
324
|
1638 |
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Parses the query part of the given connection URL and resolves the given connection parameters. |
328
|
1638 |
|
* |
329
|
1278 |
|
* @param mixed[] $url The connection URL parts to evaluate. |
330
|
|
|
* @param mixed[] $params The connection parameters to resolve. |
331
|
|
|
* |
332
|
1636 |
|
* @return mixed[] The resolved connection parameters. |
333
|
1508 |
|
*/ |
334
|
269 |
|
private static function parseDatabaseUrlQuery(array $url, array $params) : array |
335
|
|
|
{ |
336
|
1893 |
|
if (! isset($url['query'])) { |
337
|
267 |
|
return $params; |
338
|
|
|
} |
339
|
|
|
|
340
|
196 |
|
$query = []; |
341
|
|
|
|
342
|
196 |
|
parse_str($url['query'], $query); // simply ingest query as extra params, e.g. charset or sslmode |
343
|
|
|
|
344
|
196 |
|
return array_merge($params, $query); // parse_str wipes existing array elements |
345
|
|
|
} |
346
|
|
|
|
347
|
1638 |
|
/** |
348
|
|
|
* Parses the given regular connection URL and resolves the given connection parameters. |
349
|
1638 |
|
* |
350
|
1636 |
|
* Assumes that the "path" URL part is already normalized via {@link normalizeDatabaseUrlPath}. |
351
|
|
|
* |
352
|
|
|
* @see normalizeDatabaseUrlPath |
353
|
1432 |
|
* |
354
|
|
|
* @param mixed[] $url The regular connection URL parts to evaluate. |
355
|
1432 |
|
* @param mixed[] $params The connection parameters to resolve. |
356
|
|
|
* |
357
|
1432 |
|
* @return mixed[] The resolved connection parameters. |
358
|
|
|
*/ |
359
|
257 |
|
private static function parseRegularDatabaseUrlPath(array $url, array $params) : array |
360
|
|
|
{ |
361
|
257 |
|
$params['dbname'] = $url['path']; |
362
|
|
|
|
363
|
257 |
|
return $params; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* Parses the given SQLite connection URL and resolves the given connection parameters. |
368
|
|
|
* |
369
|
|
|
* Assumes that the "path" URL part is already normalized via {@link normalizeDatabaseUrlPath}. |
370
|
|
|
* |
371
|
|
|
* @see normalizeDatabaseUrlPath |
372
|
1626 |
|
* |
373
|
|
|
* @param mixed[] $url The SQLite connection URL parts to evaluate. |
374
|
1626 |
|
* @param mixed[] $params The connection parameters to resolve. |
375
|
|
|
* |
376
|
1626 |
|
* @return mixed[] The resolved connection parameters. |
377
|
|
|
*/ |
378
|
215 |
|
private static function parseSqliteDatabaseUrlPath(array $url, array $params) : array |
379
|
|
|
{ |
380
|
215 |
|
if ($url['path'] === ':memory:') { |
381
|
204 |
|
$params['memory'] = true; |
382
|
|
|
|
383
|
204 |
|
return $params; |
384
|
|
|
} |
385
|
|
|
|
386
|
211 |
|
$params['path'] = $url['path']; // pdo_sqlite driver uses 'path' instead of 'dbname' key |
387
|
|
|
|
388
|
211 |
|
return $params; |
389
|
|
|
} |
390
|
|
|
|
391
|
1508 |
|
/** |
392
|
|
|
* Parses the scheme part from given connection URL and resolves the given connection parameters. |
393
|
1508 |
|
* |
394
|
1478 |
|
* @param mixed[] $url The connection URL parts to evaluate. |
395
|
|
|
* @param mixed[] $params The connection parameters to resolve. |
396
|
1478 |
|
* |
397
|
|
|
* @return mixed[] The resolved connection parameters. |
398
|
|
|
* |
399
|
1504 |
|
* @throws DBALException If parsing failed or resolution is not possible. |
400
|
|
|
*/ |
401
|
1779 |
|
private static function parseDatabaseUrlScheme(array $url, array $params) : array |
402
|
|
|
{ |
403
|
275 |
|
if (isset($url['scheme'])) { |
404
|
|
|
// The requested driver from the URL scheme takes precedence |
405
|
|
|
// over the default custom driver from the connection parameters (if any). |
406
|
261 |
|
unset($params['driverClass']); |
407
|
|
|
|
408
|
|
|
// URL schemes must not contain underscores, but dashes are ok |
409
|
261 |
|
$driver = str_replace('-', '_', $url['scheme']); |
410
|
261 |
|
assert(is_string($driver)); |
411
|
|
|
|
412
|
|
|
// The requested driver from the URL scheme takes precedence over the |
413
|
|
|
// default driver from the connection parameters. If the driver is |
414
|
1644 |
|
// an alias (e.g. "postgres"), map it to the actual name ("pdo-pgsql"). |
415
|
|
|
// Otherwise, let checkParams decide later if the driver exists. |
416
|
1905 |
|
$params['driver'] = self::$driverSchemeAliases[$driver] ?? $driver; |
417
|
|
|
|
418
|
261 |
|
return $params; |
419
|
1630 |
|
} |
420
|
|
|
|
421
|
|
|
// If a schemeless connection URL is given, we require a default driver or default custom driver |
422
|
1630 |
|
// as connection parameter. |
423
|
1832 |
|
if (! isset($params['driverClass']) && ! isset($params['driver'])) { |
424
|
194 |
|
throw DBALException::driverRequired($params['url']); |
425
|
|
|
} |
426
|
|
|
|
427
|
184 |
|
return $params; |
428
|
|
|
} |
429
|
|
|
} |
430
|
|
|
|