DriverManager::getConnection()   F
last analyzed

Complexity

Conditions 14
Paths 448

Size

Total Lines 61
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 14

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 61
ccs 30
cts 30
cp 1
rs 2.8666
c 0
b 0
f 0
cc 14
nc 448
nop 3
crap 14

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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