Completed
Push — develop ( 72ba3e...de019a )
by Marco
25s queued 12s
created

DriverManager::parseDatabaseUrl()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 41
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 7.0046

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 41
ccs 21
cts 22
cp 0.9545
rs 8.6506
c 0
b 0
f 0
cc 7
nc 18
nop 1
crap 7.0046
1
<?php
2
3
namespace Doctrine\DBAL;
4
5
use Doctrine\Common\EventManager;
6
use Doctrine\DBAL\Driver\IBMDB2\DB2Driver;
7
use Doctrine\DBAL\Driver\Mysqli\Driver as MySQLiDriver;
8
use Doctrine\DBAL\Driver\OCI8\Driver as OCI8Driver;
9
use Doctrine\DBAL\Driver\PDOMySql\Driver as PDOMySQLDriver;
10
use Doctrine\DBAL\Driver\PDOOracle\Driver as PDOOCIDriver;
11
use Doctrine\DBAL\Driver\PDOPgSql\Driver as PDOPgSQLDriver;
12
use Doctrine\DBAL\Driver\PDOSqlite\Driver as PDOSQLiteDriver;
13
use Doctrine\DBAL\Driver\PDOSqlsrv\Driver as PDOSQLSrvDriver;
14
use Doctrine\DBAL\Driver\SQLAnywhere\Driver as SQLAnywhereDriver;
15
use Doctrine\DBAL\Driver\SQLSrv\Driver as SQLSrvDriver;
16
use PDO;
17
use function array_keys;
18
use function array_map;
19
use function array_merge;
20
use function assert;
21
use function class_implements;
22
use function in_array;
23
use function is_string;
24
use function is_subclass_of;
25
use function parse_str;
26
use function parse_url;
27
use function preg_replace;
28
use function str_replace;
29
use function strpos;
30
use function substr;
31
32
/**
33
 * Factory for creating Doctrine\DBAL\Connection instances.
34
 */
35
final class DriverManager
36
{
37
    /**
38
     * List of supported drivers and their mappings to the driver classes.
39
     *
40
     * To add your own driver use the 'driverClass' parameter to
41
     * {@link DriverManager::getConnection()}.
42
     *
43
     * @var string[]
44
     */
45
    private static $_driverMap = [
46
        'pdo_mysql'   => PDOMySQLDriver::class,
47
        'pdo_sqlite'  => PDOSQLiteDriver::class,
48
        'pdo_pgsql'   => PDOPgSQLDriver::class,
49
        'pdo_oci'     => PDOOCIDriver::class,
50
        'oci8'        => OCI8Driver::class,
51
        'ibm_db2'     => DB2Driver::class,
52
        'pdo_sqlsrv'  => PDOSQLSrvDriver::class,
53
        'mysqli'      => MySQLiDriver::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
     *
102
     * OR 'driverClass' that contains the full class name (with namespace) of the
103
     * driver class to instantiate.
104
     *
105
     * Other (optional) parameters:
106
     *
107
     * <b>user (string)</b>:
108
     * The username to use when connecting.
109
     *
110
     * <b>password (string)</b>:
111
     * The password to use when connecting.
112
     *
113
     * <b>driverOptions (array)</b>:
114
     * Any additional driver-specific options for the driver. These are just passed
115
     * through to the driver.
116
     *
117
     * <b>pdo</b>:
118
     * You can pass an existing PDO instance through this parameter. The PDO
119
     * instance will be wrapped in a Doctrine\DBAL\Connection.
120
     *
121
     * <b>wrapperClass</b>:
122
     * You may specify a custom wrapper class through the 'wrapperClass'
123
     * parameter but this class MUST inherit from Doctrine\DBAL\Connection.
124
     *
125
     * <b>driverClass</b>:
126
     * The driver class to use.
127
     *
128
     * @param mixed[]            $params       The parameters.
129
     * @param Configuration|null $config       The configuration to use.
130
     * @param EventManager|null  $eventManager The event manager to use.
131
     *
132
     * @throws DBALException
133
     */
134 3241
    public static function getConnection(
135
        array $params,
136
        ?Configuration $config = null,
137
        ?EventManager $eventManager = null
138
    ) : Connection {
139
        // create default config and event manager, if not set
140 3241
        if (! $config) {
141 3234
            $config = new Configuration();
142
        }
143 3241
        if (! $eventManager) {
144 3234
            $eventManager = new EventManager();
145
        }
146
147 3241
        $params = self::parseDatabaseUrl($params);
148
149
        // URL support for MasterSlaveConnection
150 3238
        if (isset($params['master'])) {
151 1820
            $params['master'] = self::parseDatabaseUrl($params['master']);
152
        }
153
154 3238
        if (isset($params['slaves'])) {
155 1820
            foreach ($params['slaves'] as $key => $slaveParams) {
156 1820
                $params['slaves'][$key] = self::parseDatabaseUrl($slaveParams);
157
            }
158
        }
159
160
        // URL support for PoolingShardConnection
161 3238
        if (isset($params['global'])) {
162 1810
            $params['global'] = self::parseDatabaseUrl($params['global']);
163
        }
164
165 3238
        if (isset($params['shards'])) {
166 1810
            foreach ($params['shards'] as $key => $shardParams) {
167 1810
                $params['shards'][$key] = self::parseDatabaseUrl($shardParams);
168
            }
169
        }
170
171
        // check for existing pdo object
172 3238
        if (isset($params['pdo']) && ! $params['pdo'] instanceof PDO) {
173 2060
            throw DBALException::invalidPdoInstance();
174 3237
        } elseif (isset($params['pdo'])) {
175 2040
            $params['pdo']->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
176 2040
            $params['driver'] = 'pdo_' . $params['pdo']->getAttribute(PDO::ATTR_DRIVER_NAME);
177
        } else {
178 3232
            self::_checkParams($params);
179
        }
180
181 3234
        $className = $params['driverClass'] ?? self::$_driverMap[$params['driver']];
182
183 3234
        $driver = new $className();
184
185 3234
        $wrapperClass = Connection::class;
186 3234
        if (isset($params['wrapperClass'])) {
187 1939
            if (! is_subclass_of($params['wrapperClass'], $wrapperClass)) {
188 1892
                throw DBALException::invalidWrapperClass($params['wrapperClass']);
189
            }
190
191 1938
            $wrapperClass = $params['wrapperClass'];
192
        }
193
194 3233
        return new $wrapperClass($params, $driver, $config, $eventManager);
195
    }
196
197
    /**
198
     * Returns the list of supported drivers.
199
     *
200
     * @return string[]
201
     */
202
    public static function getAvailableDrivers() : array
203
    {
204
        return array_keys(self::$_driverMap);
205
    }
206
207
    /**
208
     * Checks the list of parameters.
209
     *
210
     * @param mixed[] $params The list of parameters.
211
     *
212
     * @throws DBALException
213
     */
214 3232
    private static function _checkParams(array $params) : void
215
    {
216
        // check existence of mandatory parameters
217
218
        // driver
219 3232
        if (! isset($params['driver']) && ! isset($params['driverClass'])) {
220 1988
            throw DBALException::driverRequired();
221
        }
222
223
        // check validity of parameters
224
225
        // driver
226 3231
        if (isset($params['driver']) && ! isset(self::$_driverMap[$params['driver']])) {
227 1964
            throw DBALException::unknownDriver($params['driver'], array_keys(self::$_driverMap));
228
        }
229
230 3230
        if (isset($params['driverClass']) && ! in_array(Driver::class, class_implements($params['driverClass'], true))) {
231 1868
            throw DBALException::invalidDriverClass($params['driverClass']);
232
        }
233 3229
    }
234
235
    /**
236
     * Normalizes the given connection URL path.
237
     *
238
     * @return string The normalized connection URL path
239
     */
240 1846
    private static function normalizeDatabaseUrlPath(string $urlPath) : string
241
    {
242
        // Trim leading slash from URL path.
243 1846
        return substr($urlPath, 1);
244
    }
245
246
    /**
247
     * Extracts parts from a database URL, if present, and returns an
248
     * updated list of parameters.
249
     *
250
     * @param mixed[] $params The list of parameters.
251
     *
252
     * @return mixed[] A modified list of parameters with info from a database
253
     *                 URL extracted into indidivual parameter parts.
254
     *
255
     * @throws DBALException
256
     */
257 3241
    private static function parseDatabaseUrl(array $params) : array
258
    {
259 3241
        if (! isset($params['url'])) {
260 3213
            return $params;
261
        }
262
263
        // (pdo_)?sqlite3?:///... => (pdo_)?sqlite3?://localhost/... or else the URL will be invalid
264 1849
        $url = preg_replace('#^((?:pdo_)?sqlite3?):///#', '$1://localhost/', $params['url']);
265 1849
        assert(is_string($url));
266
267 1849
        $url = parse_url($url);
268
269 1849
        if ($url === false) {
270
            throw new DBALException('Malformed parameter "url".');
271
        }
272
273 1849
        $url = array_map('rawurldecode', $url);
274
275
        // If we have a connection URL, we have to unset the default PDO instance connection parameter (if any)
276
        // as we cannot merge connection details from the URL into the PDO instance (URL takes precedence).
277 1849
        unset($params['pdo']);
278
279 1849
        $params = self::parseDatabaseUrlScheme($url, $params);
280
281 1846
        if (isset($url['host'])) {
282 1846
            $params['host'] = $url['host'];
283
        }
284 1846
        if (isset($url['port'])) {
285 1822
            $params['port'] = $url['port'];
286
        }
287 1846
        if (isset($url['user'])) {
288 1840
            $params['user'] = $url['user'];
289
        }
290 1846
        if (isset($url['pass'])) {
291 1840
            $params['password'] = $url['pass'];
292
        }
293
294 1846
        $params = self::parseDatabaseUrlPath($url, $params);
295 1846
        $params = self::parseDatabaseUrlQuery($url, $params);
296
297 1846
        return $params;
298
    }
299
300
    /**
301
     * Parses the given connection URL and resolves the given connection parameters.
302
     *
303
     * Assumes that the connection URL scheme is already parsed and resolved into the given connection parameters
304
     * via {@link parseDatabaseUrlScheme}.
305
     *
306
     * @see parseDatabaseUrlScheme
307
     *
308
     * @param mixed[] $url    The URL parts to evaluate.
309
     * @param mixed[] $params The connection parameters to resolve.
310
     *
311
     * @return mixed[] The resolved connection parameters.
312
     */
313 1846
    private static function parseDatabaseUrlPath(array $url, array $params) : array
314
    {
315 1846
        if (! isset($url['path'])) {
316
            return $params;
317
        }
318
319 1846
        $url['path'] = self::normalizeDatabaseUrlPath($url['path']);
320
321
        // If we do not have a known DBAL driver, we do not know any connection URL path semantics to evaluate
322
        // and therefore treat the path as regular DBAL connection URL path.
323 1846
        if (! isset($params['driver'])) {
324 1388
            return self::parseRegularDatabaseUrlPath($url, $params);
325
        }
326
327 1845
        if (strpos($params['driver'], 'sqlite') !== false) {
328 1729
            return self::parseSqliteDatabaseUrlPath($url, $params);
329
        }
330
331 1839
        return self::parseRegularDatabaseUrlPath($url, $params);
332
    }
333
334
    /**
335
     * Parses the query part of the given connection URL and resolves the given connection parameters.
336
     *
337
     * @param mixed[] $url    The connection URL parts to evaluate.
338
     * @param mixed[] $params The connection parameters to resolve.
339
     *
340
     * @return mixed[] The resolved connection parameters.
341
     */
342 1846
    private static function parseDatabaseUrlQuery(array $url, array $params) : array
343
    {
344 1846
        if (! isset($url['query'])) {
345 1845
            return $params;
346
        }
347
348 1580
        $query = [];
349
350 1580
        parse_str($url['query'], $query); // simply ingest query as extra params, e.g. charset or sslmode
351
352 1580
        return array_merge($params, $query); // parse_str wipes existing array elements
353
    }
354
355
    /**
356
     * Parses the given regular connection URL and resolves the given connection parameters.
357
     *
358
     * Assumes that the "path" URL part is already normalized via {@link normalizeDatabaseUrlPath}.
359
     *
360
     * @see normalizeDatabaseUrlPath
361
     *
362
     * @param mixed[] $url    The regular connection URL parts to evaluate.
363
     * @param mixed[] $params The connection parameters to resolve.
364
     *
365
     * @return mixed[] The resolved connection parameters.
366
     */
367 1840
    private static function parseRegularDatabaseUrlPath(array $url, array $params) : array
368
    {
369 1840
        $params['dbname'] = $url['path'];
370
371 1840
        return $params;
372
    }
373
374
    /**
375
     * Parses the given SQLite connection URL and resolves the given connection parameters.
376
     *
377
     * Assumes that the "path" URL part is already normalized via {@link normalizeDatabaseUrlPath}.
378
     *
379
     * @see normalizeDatabaseUrlPath
380
     *
381
     * @param mixed[] $url    The SQLite connection URL parts to evaluate.
382
     * @param mixed[] $params The connection parameters to resolve.
383
     *
384
     * @return mixed[] The resolved connection parameters.
385
     */
386 1729
    private static function parseSqliteDatabaseUrlPath(array $url, array $params) : array
387
    {
388 1729
        if ($url['path'] === ':memory:') {
389 1653
            $params['memory'] = true;
390
391 1653
            return $params;
392
        }
393
394 1727
        $params['path'] = $url['path']; // pdo_sqlite driver uses 'path' instead of 'dbname' key
395
396 1727
        return $params;
397
    }
398
399
    /**
400
     * Parses the scheme part from given connection URL and resolves the given connection parameters.
401
     *
402
     * @param mixed[] $url    The connection URL parts to evaluate.
403
     * @param mixed[] $params The connection parameters to resolve.
404
     *
405
     * @return mixed[] The resolved connection parameters.
406
     *
407
     * @throws DBALException If parsing failed or resolution is not possible.
408
     */
409 1849
    private static function parseDatabaseUrlScheme(array $url, array $params) : array
410
    {
411 1849
        if (isset($url['scheme'])) {
412
            // The requested driver from the URL scheme takes precedence
413
            // over the default custom driver from the connection parameters (if any).
414 1842
            unset($params['driverClass']);
415
416
            // URL schemes must not contain underscores, but dashes are ok
417 1842
            $driver = str_replace('-', '_', $url['scheme']);
418 1842
            assert(is_string($driver));
419
420
            // The requested driver from the URL scheme takes precedence over the
421
            // default driver from the connection parameters. If the driver is
422
            // an alias (e.g. "postgres"), map it to the actual name ("pdo-pgsql").
423
            // Otherwise, let checkParams decide later if the driver exists.
424 1842
            $params['driver'] = self::$driverSchemeAliases[$driver] ?? $driver;
425
426 1842
            return $params;
427
        }
428
429
        // If a schemeless connection URL is given, we require a default driver or default custom driver
430
        // as connection parameter.
431 1538
        if (! isset($params['driverClass']) && ! isset($params['driver'])) {
432 1534
            throw DBALException::driverRequired($params['url']);
433
        }
434
435 1415
        return $params;
436
    }
437
}
438