ArrayParameters
2 years ago
Cache
9 months ago
Driver
9 months ago
Event
2 years ago
Exception
9 months ago
Id
2 years ago
Logging
2 years ago
Platforms
9 months ago
Portability
2 years ago
Query
2 years ago
SQL
9 months ago
Schema
8 months ago
Types
9 months ago
ArrayParameterType.php
2 years ago
ColumnCase.php
2 years ago
Configuration.php
9 months ago
Connection.php
9 months ago
ConnectionException.php
2 years ago
Driver.php
2 years ago
DriverManager.php
2 years ago
Events.php
2 years ago
Exception.php
2 years ago
ExpandArrayParameters.php
2 years ago
FetchMode.php
2 years ago
LockMode.php
2 years ago
ParameterType.php
2 years ago
Query.php
2 years ago
Result.php
2 years ago
Statement.php
2 years ago
TransactionIsolationLevel.php
2 years ago
VersionAwarePlatformDriver.php
2 years ago
index.php
2 years ago
DriverManager.php
107 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Doctrine\DBAL; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Doctrine\Common\EventManager; |
| 5 | use MailPoetVendor\Doctrine\DBAL\Driver\IBMDB2; |
| 6 | use MailPoetVendor\Doctrine\DBAL\Driver\Mysqli; |
| 7 | use MailPoetVendor\Doctrine\DBAL\Driver\OCI8; |
| 8 | use MailPoetVendor\Doctrine\DBAL\Driver\PDO; |
| 9 | use MailPoetVendor\Doctrine\DBAL\Driver\PgSQL; |
| 10 | use MailPoetVendor\Doctrine\DBAL\Driver\SQLite3; |
| 11 | use MailPoetVendor\Doctrine\DBAL\Driver\SQLSrv; |
| 12 | use MailPoetVendor\Doctrine\DBAL\Exception\MalformedDsnException; |
| 13 | use MailPoetVendor\Doctrine\DBAL\Tools\DsnParser; |
| 14 | use MailPoetVendor\Doctrine\Deprecations\Deprecation; |
| 15 | use MailPoetVendor\SensitiveParameter; |
| 16 | use function array_keys; |
| 17 | use function array_merge; |
| 18 | use function is_a; |
| 19 | final class DriverManager |
| 20 | { |
| 21 | private const DRIVER_MAP = ['pdo_mysql' => PDO\MySQL\Driver::class, 'pdo_sqlite' => PDO\SQLite\Driver::class, 'pdo_pgsql' => PDO\PgSQL\Driver::class, 'pdo_oci' => PDO\OCI\Driver::class, 'oci8' => OCI8\Driver::class, 'ibm_db2' => IBMDB2\Driver::class, 'pdo_sqlsrv' => PDO\SQLSrv\Driver::class, 'mysqli' => Mysqli\Driver::class, 'pgsql' => PgSQL\Driver::class, 'sqlsrv' => SQLSrv\Driver::class, 'sqlite3' => SQLite3\Driver::class]; |
| 22 | private static array $driverSchemeAliases = [ |
| 23 | 'db2' => 'ibm_db2', |
| 24 | 'mssql' => 'pdo_sqlsrv', |
| 25 | 'mysql' => 'pdo_mysql', |
| 26 | 'mysql2' => 'pdo_mysql', |
| 27 | // Amazon RDS, for some weird reason |
| 28 | 'postgres' => 'pdo_pgsql', |
| 29 | 'postgresql' => 'pdo_pgsql', |
| 30 | 'pgsql' => 'pdo_pgsql', |
| 31 | 'sqlite' => 'pdo_sqlite', |
| 32 | 'sqlite3' => 'pdo_sqlite', |
| 33 | ]; |
| 34 | private function __construct() |
| 35 | { |
| 36 | } |
| 37 | public static function getConnection( array $params, ?Configuration $config = null, ?EventManager $eventManager = null) : Connection |
| 38 | { |
| 39 | // create default config and event manager, if not set |
| 40 | $config ??= new Configuration(); |
| 41 | $eventManager ??= new EventManager(); |
| 42 | $params = self::parseDatabaseUrl($params); |
| 43 | // URL support for PrimaryReplicaConnection |
| 44 | if (isset($params['primary'])) { |
| 45 | $params['primary'] = self::parseDatabaseUrl($params['primary']); |
| 46 | } |
| 47 | if (isset($params['replica'])) { |
| 48 | foreach ($params['replica'] as $key => $replicaParams) { |
| 49 | $params['replica'][$key] = self::parseDatabaseUrl($replicaParams); |
| 50 | } |
| 51 | } |
| 52 | $driver = self::createDriver($params['driver'] ?? null, $params['driverClass'] ?? null); |
| 53 | foreach ($config->getMiddlewares() as $middleware) { |
| 54 | $driver = $middleware->wrap($driver); |
| 55 | } |
| 56 | $wrapperClass = $params['wrapperClass'] ?? Connection::class; |
| 57 | if (!is_a($wrapperClass, Connection::class, \true)) { |
| 58 | throw Exception::invalidWrapperClass($wrapperClass); |
| 59 | } |
| 60 | return new $wrapperClass($params, $driver, $config, $eventManager); |
| 61 | } |
| 62 | public static function getAvailableDrivers() : array |
| 63 | { |
| 64 | return array_keys(self::DRIVER_MAP); |
| 65 | } |
| 66 | private static function createDriver(?string $driver, ?string $driverClass) : Driver |
| 67 | { |
| 68 | if ($driverClass === null) { |
| 69 | if ($driver === null) { |
| 70 | throw Exception::driverRequired(); |
| 71 | } |
| 72 | if (!isset(self::DRIVER_MAP[$driver])) { |
| 73 | throw Exception::unknownDriver($driver, array_keys(self::DRIVER_MAP)); |
| 74 | } |
| 75 | $driverClass = self::DRIVER_MAP[$driver]; |
| 76 | } elseif (!is_a($driverClass, Driver::class, \true)) { |
| 77 | throw Exception::invalidDriverClass($driverClass); |
| 78 | } |
| 79 | return new $driverClass(); |
| 80 | } |
| 81 | private static function parseDatabaseUrl( array $params) : array |
| 82 | { |
| 83 | if (!isset($params['url'])) { |
| 84 | return $params; |
| 85 | } |
| 86 | Deprecation::trigger('doctrine/dbal', 'https://github.com/doctrine/dbal/pull/5843', 'The "url" connection parameter is deprecated. Please use %s to parse a database url before calling %s.', DsnParser::class, self::class); |
| 87 | $parser = new DsnParser(self::$driverSchemeAliases); |
| 88 | try { |
| 89 | $parsedParams = $parser->parse($params['url']); |
| 90 | } catch (MalformedDsnException $e) { |
| 91 | throw new Exception('Malformed parameter "url".', 0, $e); |
| 92 | } |
| 93 | if (isset($parsedParams['driver'])) { |
| 94 | // The requested driver from the URL scheme takes precedence |
| 95 | // over the default custom driver from the connection parameters (if any). |
| 96 | unset($params['driverClass']); |
| 97 | } |
| 98 | $params = array_merge($params, $parsedParams); |
| 99 | // If a schemeless connection URL is given, we require a default driver or default custom driver |
| 100 | // as connection parameter. |
| 101 | if (!isset($params['driverClass']) && !isset($params['driver'])) { |
| 102 | throw Exception::driverRequired($params['url']); |
| 103 | } |
| 104 | return $params; |
| 105 | } |
| 106 | } |
| 107 |