DriverManager.php 14.5 KB
Newer Older
romanb's avatar
romanb committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
Benjamin Eberlei's avatar
Benjamin Eberlei committed
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
romanb's avatar
romanb committed
18 19
 */

20 21 22
namespace Doctrine\DBAL;

use Doctrine\Common\EventManager;
23 24 25 26 27 28 29 30 31 32 33 34
use function array_keys;
use function array_map;
use function array_merge;
use function class_implements;
use function in_array;
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
35 36

/**
37
 * Factory for creating Doctrine\DBAL\Connection instances.
romanb's avatar
romanb committed
38 39 40 41
 *
 * @author Roman Borschel <roman@code-factory.org>
 * @since 2.0
 */
42
final class DriverManager
romanb's avatar
romanb committed
43 44
{
    /**
45
     * List of supported drivers and their mappings to the driver classes.
romanb's avatar
romanb committed
46
     *
47 48 49
     * To add your own driver use the 'driverClass' parameter to
     * {@link DriverManager::getConnection()}.
     *
romanb's avatar
romanb committed
50 51
     * @var array
     */
52
     private static $_driverMap = [
Steve Müller's avatar
Steve Müller committed
53 54 55 56 57 58 59 60
         'pdo_mysql'          => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
         'pdo_sqlite'         => 'Doctrine\DBAL\Driver\PDOSqlite\Driver',
         'pdo_pgsql'          => 'Doctrine\DBAL\Driver\PDOPgSql\Driver',
         'pdo_oci'            => 'Doctrine\DBAL\Driver\PDOOracle\Driver',
         'oci8'               => 'Doctrine\DBAL\Driver\OCI8\Driver',
         'ibm_db2'            => 'Doctrine\DBAL\Driver\IBMDB2\DB2Driver',
         'pdo_sqlsrv'         => 'Doctrine\DBAL\Driver\PDOSqlsrv\Driver',
         'mysqli'             => 'Doctrine\DBAL\Driver\Mysqli\Driver',
61
         'drizzle_pdo_mysql'  => 'Doctrine\DBAL\Driver\DrizzlePDOMySql\Driver',
Steve Müller's avatar
Steve Müller committed
62 63
         'sqlanywhere'        => 'Doctrine\DBAL\Driver\SQLAnywhere\Driver',
         'sqlsrv'             => 'Doctrine\DBAL\Driver\SQLSrv\Driver',
64
    ];
65

66 67 68
    /**
     * List of URL schemes from a database URL and their mappings to driver.
     */
69
    private static $driverSchemeAliases = [
70 71 72 73 74 75 76 77 78
        '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',
79
    ];
80

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

romanb's avatar
romanb committed
88
    /**
89
     * Creates a connection object based on the specified parameters.
90
     * This method returns a Doctrine\DBAL\Connection which wraps the underlying
91
     * driver connection.
romanb's avatar
romanb committed
92
     *
93
     * $params must contain at least one of the following.
Benjamin Eberlei's avatar
Benjamin Eberlei committed
94
     *
95
     * Either 'driver' with one of the following values:
96
     *
97 98 99
     *     pdo_mysql
     *     pdo_sqlite
     *     pdo_pgsql
100 101
     *     pdo_oci (unstable)
     *     pdo_sqlsrv
102
     *     pdo_sqlsrv
103
     *     mysqli
104
     *     sqlanywhere
105 106 107
     *     sqlsrv
     *     ibm_db2 (unstable)
     *     drizzle_pdo_mysql
Benjamin Eberlei's avatar
Benjamin Eberlei committed
108
     *
109 110
     * OR 'driverClass' that contains the full class name (with namespace) of the
     * driver class to instantiate.
Benjamin Eberlei's avatar
Benjamin Eberlei committed
111
     *
112
     * Other (optional) parameters:
Benjamin Eberlei's avatar
Benjamin Eberlei committed
113
     *
114
     * <b>user (string)</b>:
Benjamin Eberlei's avatar
Benjamin Eberlei committed
115 116
     * The username to use when connecting.
     *
117 118
     * <b>password (string)</b>:
     * The password to use when connecting.
Benjamin Eberlei's avatar
Benjamin Eberlei committed
119
     *
120 121 122
     * <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
123
     *
124 125
     * <b>pdo</b>:
     * You can pass an existing PDO instance through this parameter. The PDO
126
     * instance will be wrapped in a Doctrine\DBAL\Connection.
Benjamin Eberlei's avatar
Benjamin Eberlei committed
127
     *
128 129
     * <b>wrapperClass</b>:
     * You may specify a custom wrapper class through the 'wrapperClass'
130
     * parameter but this class MUST inherit from Doctrine\DBAL\Connection.
Benjamin Eberlei's avatar
Benjamin Eberlei committed
131
     *
132 133 134
     * <b>driverClass</b>:
     * The driver class to use.
     *
Benjamin Morel's avatar
Benjamin Morel committed
135 136 137 138
     * @param array                              $params       The parameters.
     * @param \Doctrine\DBAL\Configuration|null  $config       The configuration to use.
     * @param \Doctrine\Common\EventManager|null $eventManager The event manager to use.
     *
139
     * @return \Doctrine\DBAL\Connection
Benjamin Morel's avatar
Benjamin Morel committed
140 141
     *
     * @throws \Doctrine\DBAL\DBALException
romanb's avatar
romanb committed
142
     */
143 144
    public static function getConnection(
            array $params,
145
            Configuration $config = null,
146
            EventManager $eventManager = null): Connection
romanb's avatar
romanb committed
147 148 149
    {
        // create default config and event manager, if not set
        if ( ! $config) {
150
            $config = new Configuration();
romanb's avatar
romanb committed
151 152
        }
        if ( ! $eventManager) {
153
            $eventManager = new EventManager();
romanb's avatar
romanb committed
154
        }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
155

David Zuelke's avatar
David Zuelke committed
156
        $params = self::parseDatabaseUrl($params);
157

romanb's avatar
romanb committed
158
        // check for existing pdo object
159
        if (isset($params['pdo']) && ! $params['pdo'] instanceof \PDO) {
160
            throw DBALException::invalidPdoInstance();
Steve Müller's avatar
Steve Müller committed
161
        } elseif (isset($params['pdo'])) {
162
            $params['pdo']->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
jwage's avatar
jwage committed
163
            $params['driver'] = 'pdo_' . $params['pdo']->getAttribute(\PDO::ATTR_DRIVER_NAME);
romanb's avatar
romanb committed
164
        } else {
165
            self::_checkParams($params);
romanb's avatar
romanb committed
166
        }
167

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

romanb's avatar
romanb committed
170
        $driver = new $className();
Benjamin Eberlei's avatar
Benjamin Eberlei committed
171

172
        $wrapperClass = 'Doctrine\DBAL\Connection';
173 174 175 176 177 178
        if (isset($params['wrapperClass'])) {
            if (is_subclass_of($params['wrapperClass'], $wrapperClass)) {
               $wrapperClass = $params['wrapperClass'];
            } else {
                throw DBALException::invalidWrapperClass($params['wrapperClass']);
            }
romanb's avatar
romanb committed
179
        }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
180

romanb's avatar
romanb committed
181 182
        return new $wrapperClass($params, $driver, $config, $eventManager);
    }
183

184
    /**
185
     * Returns the list of supported drivers.
186 187 188
     *
     * @return array
     */
189
    public static function getAvailableDrivers(): array
190
    {
191
        return array_keys(self::$_driverMap);
192 193
    }

romanb's avatar
romanb committed
194 195 196
    /**
     * Checks the list of parameters.
     *
Benjamin Morel's avatar
Benjamin Morel committed
197 198 199 200 201
     * @param array $params The list of parameters.
     *
     * @return void
     *
     * @throws \Doctrine\DBAL\DBALException
romanb's avatar
romanb committed
202
     */
203
    private static function _checkParams(array $params): void
Benjamin Eberlei's avatar
Benjamin Eberlei committed
204
    {
Pascal Borreli's avatar
Pascal Borreli committed
205
        // check existence of mandatory parameters
Benjamin Eberlei's avatar
Benjamin Eberlei committed
206

romanb's avatar
romanb committed
207 208
        // driver
        if ( ! isset($params['driver']) && ! isset($params['driverClass'])) {
209
            throw DBALException::driverRequired();
romanb's avatar
romanb committed
210
        }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
211

romanb's avatar
romanb committed
212
        // check validity of parameters
Benjamin Eberlei's avatar
Benjamin Eberlei committed
213

romanb's avatar
romanb committed
214
        // driver
215
        if (isset($params['driver']) && ! isset(self::$_driverMap[$params['driver']])) {
216
            throw DBALException::unknownDriver($params['driver'], array_keys(self::$_driverMap));
romanb's avatar
romanb committed
217
        }
218 219 220 221

        if (isset($params['driverClass']) && ! in_array('Doctrine\DBAL\Driver', class_implements($params['driverClass'], true))) {
            throw DBALException::invalidDriverClass($params['driverClass']);
        }
romanb's avatar
romanb committed
222
    }
223

224 225 226 227 228 229 230
    /**
     * Normalizes the given connection URL path.
     *
     * @param string $urlPath
     *
     * @return string The normalized connection URL path
     */
231
    private static function normalizeDatabaseUrlPath(string $urlPath): string
232 233 234 235 236
    {
        // Trim leading slash from URL path.
        return substr($urlPath, 1);
    }

237 238 239 240 241 242
    /**
     * Extracts parts from a database URL, if present, and returns an
     * updated list of parameters.
     *
     * @param array $params The list of parameters.
     *
243 244
     * @return array A modified list of parameters with info from a database
     *               URL extracted into indidivual parameter parts.
245
     *
246
     * @throws DBALException
247
     */
248
    private static function parseDatabaseUrl(array $params): array
249 250
    {
        if (!isset($params['url'])) {
David Zuelke's avatar
David Zuelke committed
251
            return $params;
252
        }
253

254
        // (pdo_)?sqlite3?:///... => (pdo_)?sqlite3?://localhost/... or else the URL will be invalid
David Zuelke's avatar
David Zuelke committed
255
        $url = preg_replace('#^((?:pdo_)?sqlite3?):///#', '$1://localhost/', $params['url']);
256
        $url = parse_url($url);
257

David Zuelke's avatar
David Zuelke committed
258
        if ($url === false) {
259 260
            throw new DBALException('Malformed parameter "url".');
        }
261

262 263
        $url = array_map('rawurldecode', $url);

264 265 266 267 268
        // If we have a connection URL, we have to unset the default PDO instance connection parameter (if any)
        // as we cannot merge connection details from the URL into the PDO instance (URL takes precedence).
        unset($params['pdo']);

        $params = self::parseDatabaseUrlScheme($url, $params);
269

270 271 272 273 274 275 276 277 278 279 280 281
        if (isset($url['host'])) {
            $params['host'] = $url['host'];
        }
        if (isset($url['port'])) {
            $params['port'] = $url['port'];
        }
        if (isset($url['user'])) {
            $params['user'] = $url['user'];
        }
        if (isset($url['pass'])) {
            $params['password'] = $url['pass'];
        }
282

283 284
        $params = self::parseDatabaseUrlPath($url, $params);
        $params = self::parseDatabaseUrlQuery($url, $params);
285 286 287 288 289

        return $params;
    }

    /**
290 291 292 293
     * 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}.
294 295 296 297 298
     *
     * @param array $url    The URL parts to evaluate.
     * @param array $params The connection parameters to resolve.
     *
     * @return array The resolved connection parameters.
299 300
     *
     * @see parseDatabaseUrlScheme
301
     */
302
    private static function parseDatabaseUrlPath(array $url, array $params): array
303
    {
304
        if (! isset($url['path'])) {
305 306 307
            return $params;
        }

308
        $url['path'] = self::normalizeDatabaseUrlPath($url['path']);
309

310 311 312 313 314 315 316
        // 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) {
317 318 319
            return self::parseSqliteDatabaseUrlPath($url, $params);
        }

320 321 322 323 324 325 326 327 328 329 330
        return self::parseRegularDatabaseUrlPath($url, $params);
    }

    /**
     * Parses the query part of the given connection URL and resolves the given connection parameters.
     *
     * @param array $url    The connection URL parts to evaluate.
     * @param array $params The connection parameters to resolve.
     *
     * @return array The resolved connection parameters.
     */
331
    private static function parseDatabaseUrlQuery(array $url, array $params): array
332 333 334 335 336
    {
        if (! isset($url['query'])) {
            return $params;
        }

337
        $query = [];
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355

        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}.
     *
     * @param array $url    The regular connection URL parts to evaluate.
     * @param array $params The connection parameters to resolve.
     *
     * @return array The resolved connection parameters.
     *
     * @see normalizeDatabaseUrlPath
     */
356
    private static function parseRegularDatabaseUrlPath(array $url, array $params): array
357
    {
358 359 360 361 362 363
        $params['dbname'] = $url['path'];

        return $params;
    }

    /**
364
     * Parses the given SQLite connection URL and resolves the given connection parameters.
365
     *
366 367 368
     * Assumes that the "path" URL part is already normalized via {@link normalizeDatabaseUrlPath}.
     *
     * @param array $url    The SQLite connection URL parts to evaluate.
369 370 371
     * @param array $params The connection parameters to resolve.
     *
     * @return array The resolved connection parameters.
372 373
     *
     * @see normalizeDatabaseUrlPath
374
     */
375
    private static function parseSqliteDatabaseUrlPath(array $url, array $params): array
376 377 378 379 380 381 382 383 384
    {
        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
385
        return $params;
386
    }
387 388 389 390 391 392 393 394 395 396 397

    /**
     * Parses the scheme part from given connection URL and resolves the given connection parameters.
     *
     * @param array $url    The connection URL parts to evaluate.
     * @param array $params The connection parameters to resolve.
     *
     * @return array The resolved connection parameters.
     *
     * @throws DBALException if parsing failed or resolution is not possible.
     */
398
    private static function parseDatabaseUrlScheme(array $url, array $params): array
399 400 401 402 403 404 405 406 407
    {
        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']);

408 409 410 411
            // 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.
412
            $params['driver'] = self::$driverSchemeAliases[$driver] ?? $driver;
413 414 415 416 417 418 419 420 421 422 423 424

            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'])) {
            throw DBALException::driverRequired($params['url']);
        }

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