Completed
Push — 3.0.x ( 0d9a8c...f82f5c )
by Sergei
25s queued 17s
created

DriverManager::getConnection()   B

Complexity

Conditions 8
Paths 48

Size

Total Lines 42
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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