DriverManager.php 13.3 KB
Newer Older
romanb's avatar
romanb committed
1 2
<?php

Michael Moravec's avatar
Michael Moravec committed
3 4
declare(strict_types=1);

5 6 7
namespace Doctrine\DBAL;

use Doctrine\Common\EventManager;
8 9 10 11 12 13 14 15 16 17
use Doctrine\DBAL\Driver\IBMDB2\DB2Driver;
use Doctrine\DBAL\Driver\Mysqli\Driver as MySQLiDriver;
use Doctrine\DBAL\Driver\OCI8\Driver as OCI8Driver;
use Doctrine\DBAL\Driver\PDOMySql\Driver as PDOMySQLDriver;
use Doctrine\DBAL\Driver\PDOOracle\Driver as PDOOCIDriver;
use Doctrine\DBAL\Driver\PDOPgSql\Driver as PDOPgSQLDriver;
use Doctrine\DBAL\Driver\PDOSqlite\Driver as PDOSQLiteDriver;
use Doctrine\DBAL\Driver\PDOSqlsrv\Driver as PDOSQLSrvDriver;
use Doctrine\DBAL\Driver\SQLAnywhere\Driver as SQLAnywhereDriver;
use Doctrine\DBAL\Driver\SQLSrv\Driver as SQLSrvDriver;
18 19 20 21
use Doctrine\DBAL\Exception\DriverRequired;
use Doctrine\DBAL\Exception\InvalidDriverClass;
use Doctrine\DBAL\Exception\InvalidWrapperClass;
use Doctrine\DBAL\Exception\UnknownDriver;
22 23 24
use function array_keys;
use function array_map;
use function array_merge;
Sergei Morozov's avatar
Sergei Morozov committed
25
use function assert;
26 27
use function class_implements;
use function in_array;
Sergei Morozov's avatar
Sergei Morozov committed
28
use function is_string;
29 30 31 32 33 34 35
use function is_subclass_of;
use function parse_str;
use function parse_url;
use function preg_replace;
use function str_replace;
use function strpos;
use function substr;
romanb's avatar
romanb committed
36 37

/**
38
 * Factory for creating Doctrine\DBAL\Connection instances.
romanb's avatar
romanb committed
39
 */
40
final class DriverManager
romanb's avatar
romanb committed
41 42
{
    /**
43
     * List of supported drivers and their mappings to the driver classes.
romanb's avatar
romanb committed
44
     *
45 46 47
     * To add your own driver use the 'driverClass' parameter to
     * {@link DriverManager::getConnection()}.
     *
48
     * @var string[]
romanb's avatar
romanb committed
49
     */
50
    private static $_driverMap = [
Benjamin Morel's avatar
Benjamin Morel committed
51 52 53 54 55 56 57 58 59 60
        'pdo_mysql'   => PDOMySQLDriver::class,
        'pdo_sqlite'  => PDOSQLiteDriver::class,
        'pdo_pgsql'   => PDOPgSQLDriver::class,
        'pdo_oci'     => PDOOCIDriver::class,
        'oci8'        => OCI8Driver::class,
        'ibm_db2'     => DB2Driver::class,
        'pdo_sqlsrv'  => PDOSQLSrvDriver::class,
        'mysqli'      => MySQLiDriver::class,
        'sqlanywhere' => SQLAnywhereDriver::class,
        'sqlsrv'      => SQLSrvDriver::class,
61
    ];
62

63 64
    /**
     * List of URL schemes from a database URL and their mappings to driver.
65 66
     *
     * @var string[]
67
     */
68
    private static $driverSchemeAliases = [
69 70 71 72 73 74 75 76 77
        'db2'        => 'ibm_db2',
        'mssql'      => 'pdo_sqlsrv',
        'mysql'      => 'pdo_mysql',
        'mysql2'     => 'pdo_mysql', // Amazon RDS, for some weird reason
        'postgres'   => 'pdo_pgsql',
        'postgresql' => 'pdo_pgsql',
        'pgsql'      => 'pdo_pgsql',
        'sqlite'     => 'pdo_sqlite',
        'sqlite3'    => 'pdo_sqlite',
78
    ];
79

Benjamin Morel's avatar
Benjamin Morel committed
80 81 82 83 84 85
    /**
     * Private constructor. This class cannot be instantiated.
     */
    private function __construct()
    {
    }
86

romanb's avatar
romanb committed
87
    /**
88
     * Creates a connection object based on the specified parameters.
89
     * This method returns a Doctrine\DBAL\Connection which wraps the underlying
90
     * driver connection.
romanb's avatar
romanb committed
91
     *
92
     * $params must contain at least one of the following.
Benjamin Eberlei's avatar
Benjamin Eberlei committed
93
     *
94
     * Either 'driver' with one of the array keys of {@link $_driverMap},
95 96
     * OR 'driverClass' that contains the full class name (with namespace) of the
     * driver class to instantiate.
Benjamin Eberlei's avatar
Benjamin Eberlei committed
97
     *
98
     * Other (optional) parameters:
Benjamin Eberlei's avatar
Benjamin Eberlei committed
99
     *
100
     * <b>user (string)</b>:
Benjamin Eberlei's avatar
Benjamin Eberlei committed
101 102
     * The username to use when connecting.
     *
103 104
     * <b>password (string)</b>:
     * The password to use when connecting.
Benjamin Eberlei's avatar
Benjamin Eberlei committed
105
     *
106 107 108
     * <b>driverOptions (array)</b>:
     * Any additional driver-specific options for the driver. These are just passed
     * through to the driver.
Benjamin Eberlei's avatar
Benjamin Eberlei committed
109
     *
110 111
     * <b>pdo</b>:
     * You can pass an existing PDO instance through this parameter. The PDO
112
     * instance will be wrapped in a Doctrine\DBAL\Connection.
Benjamin Eberlei's avatar
Benjamin Eberlei committed
113
     *
114 115
     * <b>wrapperClass</b>:
     * You may specify a custom wrapper class through the 'wrapperClass'
116
     * parameter but this class MUST inherit from Doctrine\DBAL\Connection.
Benjamin Eberlei's avatar
Benjamin Eberlei committed
117
     *
118 119 120
     * <b>driverClass</b>:
     * The driver class to use.
     *
121
     * @param mixed[]            $params       The parameters.
122 123
     * @param Configuration|null $config       The configuration to use.
     * @param EventManager|null  $eventManager The event manager to use.
Benjamin Morel's avatar
Benjamin Morel committed
124
     *
125
     * @throws DBALException
romanb's avatar
romanb committed
126
     */
127
    public static function getConnection(
128 129 130 131
        array $params,
        ?Configuration $config = null,
        ?EventManager $eventManager = null
    ) : Connection {
romanb's avatar
romanb committed
132
        // create default config and event manager, if not set
133
        if ($config === null) {
134
            $config = new Configuration();
romanb's avatar
romanb committed
135
        }
136

137
        if ($eventManager === null) {
138
            $eventManager = new EventManager();
romanb's avatar
romanb committed
139
        }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
140

David Zuelke's avatar
David Zuelke committed
141
        $params = self::parseDatabaseUrl($params);
142

143 144 145 146 147 148 149 150 151 152 153
        // URL support for MasterSlaveConnection
        if (isset($params['master'])) {
            $params['master'] = self::parseDatabaseUrl($params['master']);
        }

        if (isset($params['slaves'])) {
            foreach ($params['slaves'] as $key => $slaveParams) {
                $params['slaves'][$key] = self::parseDatabaseUrl($slaveParams);
            }
        }

154
        self::_checkParams($params);
155

156
        $className = $params['driverClass'] ?? self::$_driverMap[$params['driver']];
Benjamin Eberlei's avatar
Benjamin Eberlei committed
157

romanb's avatar
romanb committed
158
        $driver = new $className();
Benjamin Eberlei's avatar
Benjamin Eberlei committed
159

160
        $wrapperClass = Connection::class;
161
        if (isset($params['wrapperClass'])) {
162
            if (! is_subclass_of($params['wrapperClass'], $wrapperClass)) {
163
                throw InvalidWrapperClass::new($params['wrapperClass']);
164
            }
165 166

            $wrapperClass = $params['wrapperClass'];
romanb's avatar
romanb committed
167
        }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
168

romanb's avatar
romanb committed
169 170
        return new $wrapperClass($params, $driver, $config, $eventManager);
    }
171

172
    /**
173
     * Returns the list of supported drivers.
174
     *
175
     * @return string[]
176
     */
177
    public static function getAvailableDrivers() : array
178
    {
179
        return array_keys(self::$_driverMap);
180 181
    }

romanb's avatar
romanb committed
182 183 184
    /**
     * Checks the list of parameters.
     *
185
     * @param mixed[] $params The list of parameters.
Benjamin Morel's avatar
Benjamin Morel committed
186
     *
187
     * @throws DBALException
romanb's avatar
romanb committed
188
     */
189
    private static function _checkParams(array $params) : void
Benjamin Eberlei's avatar
Benjamin Eberlei committed
190
    {
Pascal Borreli's avatar
Pascal Borreli committed
191
        // check existence of mandatory parameters
Benjamin Eberlei's avatar
Benjamin Eberlei committed
192

romanb's avatar
romanb committed
193
        // driver
194
        if (! isset($params['driver']) && ! isset($params['driverClass'])) {
195
            throw DriverRequired::new();
romanb's avatar
romanb committed
196
        }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
197

romanb's avatar
romanb committed
198
        // check validity of parameters
Benjamin Eberlei's avatar
Benjamin Eberlei committed
199

romanb's avatar
romanb committed
200
        // driver
201
        if (isset($params['driver']) && ! isset(self::$_driverMap[$params['driver']])) {
202
            throw UnknownDriver::new($params['driver'], array_keys(self::$_driverMap));
romanb's avatar
romanb committed
203
        }
204

205
        if (isset($params['driverClass']) && ! in_array(Driver::class, class_implements($params['driverClass']), true)) {
206
            throw InvalidDriverClass::new($params['driverClass']);
207
        }
romanb's avatar
romanb committed
208
    }
209

210 211 212 213 214
    /**
     * Normalizes the given connection URL path.
     *
     * @return string The normalized connection URL path
     */
215
    private static function normalizeDatabaseUrlPath(string $urlPath) : string
216 217 218 219 220
    {
        // Trim leading slash from URL path.
        return substr($urlPath, 1);
    }

221 222 223 224
    /**
     * Extracts parts from a database URL, if present, and returns an
     * updated list of parameters.
     *
225
     * @param mixed[] $params The list of parameters.
226
     *
227 228
     * @return mixed[] A modified list of parameters with info from a database
     *                 URL extracted into indidivual parameter parts.
229
     *
230
     * @throws DBALException
231
     */
232
    private static function parseDatabaseUrl(array $params) : array
233
    {
234
        if (! isset($params['url'])) {
David Zuelke's avatar
David Zuelke committed
235
            return $params;
236
        }
237

238
        // (pdo_)?sqlite3?:///... => (pdo_)?sqlite3?://localhost/... or else the URL will be invalid
David Zuelke's avatar
David Zuelke committed
239
        $url = preg_replace('#^((?:pdo_)?sqlite3?):///#', '$1://localhost/', $params['url']);
Sergei Morozov's avatar
Sergei Morozov committed
240 241
        assert(is_string($url));

242
        $url = parse_url($url);
243

David Zuelke's avatar
David Zuelke committed
244
        if ($url === false) {
245 246
            throw new DBALException('Malformed parameter "url".');
        }
247

248 249
        $url = array_map('rawurldecode', $url);

250
        $params = self::parseDatabaseUrlScheme($url, $params);
251

252 253 254
        if (isset($url['host'])) {
            $params['host'] = $url['host'];
        }
255

256 257 258
        if (isset($url['port'])) {
            $params['port'] = $url['port'];
        }
259

260 261 262
        if (isset($url['user'])) {
            $params['user'] = $url['user'];
        }
263

264 265 266
        if (isset($url['pass'])) {
            $params['password'] = $url['pass'];
        }
267

268 269
        $params = self::parseDatabaseUrlPath($url, $params);
        $params = self::parseDatabaseUrlQuery($url, $params);
270 271 272 273 274

        return $params;
    }

    /**
275 276 277 278
     * Parses the given connection URL and resolves the given connection parameters.
     *
     * Assumes that the connection URL scheme is already parsed and resolved into the given connection parameters
     * via {@link parseDatabaseUrlScheme}.
279
     *
280 281
     * @see parseDatabaseUrlScheme
     *
282 283
     * @param mixed[] $url    The URL parts to evaluate.
     * @param mixed[] $params The connection parameters to resolve.
284
     *
285
     * @return mixed[] The resolved connection parameters.
286
     */
287
    private static function parseDatabaseUrlPath(array $url, array $params) : array
288
    {
289
        if (! isset($url['path'])) {
290 291 292
            return $params;
        }

293
        $url['path'] = self::normalizeDatabaseUrlPath($url['path']);
294

295 296 297 298 299 300 301
        // If we do not have a known DBAL driver, we do not know any connection URL path semantics to evaluate
        // and therefore treat the path as regular DBAL connection URL path.
        if (! isset($params['driver'])) {
            return self::parseRegularDatabaseUrlPath($url, $params);
        }

        if (strpos($params['driver'], 'sqlite') !== false) {
302 303 304
            return self::parseSqliteDatabaseUrlPath($url, $params);
        }

305 306 307 308 309 310
        return self::parseRegularDatabaseUrlPath($url, $params);
    }

    /**
     * Parses the query part of the given connection URL and resolves the given connection parameters.
     *
311 312
     * @param mixed[] $url    The connection URL parts to evaluate.
     * @param mixed[] $params The connection parameters to resolve.
313
     *
314
     * @return mixed[] The resolved connection parameters.
315
     */
316
    private static function parseDatabaseUrlQuery(array $url, array $params) : array
317 318 319 320 321
    {
        if (! isset($url['query'])) {
            return $params;
        }

322
        $query = [];
323 324 325 326 327 328 329 330 331 332 333

        parse_str($url['query'], $query); // simply ingest query as extra params, e.g. charset or sslmode

        return array_merge($params, $query); // parse_str wipes existing array elements
    }

    /**
     * Parses the given regular connection URL and resolves the given connection parameters.
     *
     * Assumes that the "path" URL part is already normalized via {@link normalizeDatabaseUrlPath}.
     *
334 335
     * @see normalizeDatabaseUrlPath
     *
336 337
     * @param mixed[] $url    The regular connection URL parts to evaluate.
     * @param mixed[] $params The connection parameters to resolve.
338
     *
339
     * @return mixed[] The resolved connection parameters.
340
     */
341
    private static function parseRegularDatabaseUrlPath(array $url, array $params) : array
342
    {
343 344 345 346 347 348
        $params['dbname'] = $url['path'];

        return $params;
    }

    /**
349
     * Parses the given SQLite connection URL and resolves the given connection parameters.
350
     *
351 352
     * Assumes that the "path" URL part is already normalized via {@link normalizeDatabaseUrlPath}.
     *
353 354
     * @see normalizeDatabaseUrlPath
     *
355 356
     * @param mixed[] $url    The SQLite connection URL parts to evaluate.
     * @param mixed[] $params The connection parameters to resolve.
357
     *
358
     * @return mixed[] The resolved connection parameters.
359
     */
360
    private static function parseSqliteDatabaseUrlPath(array $url, array $params) : array
361 362 363 364 365 366 367 368 369
    {
        if ($url['path'] === ':memory:') {
            $params['memory'] = true;

            return $params;
        }

        $params['path'] = $url['path']; // pdo_sqlite driver uses 'path' instead of 'dbname' key

David Zuelke's avatar
David Zuelke committed
370
        return $params;
371
    }
372 373 374 375

    /**
     * Parses the scheme part from given connection URL and resolves the given connection parameters.
     *
376 377
     * @param mixed[] $url    The connection URL parts to evaluate.
     * @param mixed[] $params The connection parameters to resolve.
378
     *
379
     * @return mixed[] The resolved connection parameters.
380
     *
381
     * @throws DBALException If parsing failed or resolution is not possible.
382
     */
383
    private static function parseDatabaseUrlScheme(array $url, array $params) : array
384 385 386 387 388 389 390 391
    {
        if (isset($url['scheme'])) {
            // The requested driver from the URL scheme takes precedence
            // over the default custom driver from the connection parameters (if any).
            unset($params['driverClass']);

            // URL schemes must not contain underscores, but dashes are ok
            $driver = str_replace('-', '_', $url['scheme']);
Sergei Morozov's avatar
Sergei Morozov committed
392
            assert(is_string($driver));
393

394 395 396 397
            // The requested driver from the URL scheme takes precedence over the
            // default driver from the connection parameters. If the driver is
            // an alias (e.g. "postgres"), map it to the actual name ("pdo-pgsql").
            // Otherwise, let checkParams decide later if the driver exists.
398
            $params['driver'] = self::$driverSchemeAliases[$driver] ?? $driver;
399 400 401 402 403 404 405

            return $params;
        }

        // If a schemeless connection URL is given, we require a default driver or default custom driver
        // as connection parameter.
        if (! isset($params['driverClass']) && ! isset($params['driver'])) {
406
            throw DriverRequired::new($params['url']);
407 408 409 410
        }

        return $params;
    }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
411
}