DriverManager.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. <?php
  2. namespace Doctrine\DBAL;
  3. use Doctrine\Common\EventManager;
  4. use Doctrine\DBAL\Driver\DrizzlePDOMySql;
  5. use Doctrine\DBAL\Driver\IBMDB2;
  6. use Doctrine\DBAL\Driver\Mysqli;
  7. use Doctrine\DBAL\Driver\OCI8;
  8. use Doctrine\DBAL\Driver\PDO;
  9. use Doctrine\DBAL\Driver\PDO\Statement as PDODriverStatement;
  10. use Doctrine\DBAL\Driver\SQLAnywhere;
  11. use Doctrine\DBAL\Driver\SQLSrv;
  12. use Doctrine\Deprecations\Deprecation;
  13. use function array_keys;
  14. use function array_merge;
  15. use function assert;
  16. use function class_implements;
  17. use function in_array;
  18. use function is_string;
  19. use function is_subclass_of;
  20. use function parse_str;
  21. use function parse_url;
  22. use function preg_replace;
  23. use function rawurldecode;
  24. use function str_replace;
  25. use function strpos;
  26. use function substr;
  27. /**
  28. * Factory for creating {@link Connection} instances.
  29. *
  30. * @psalm-type OverrideParams = array{
  31. * charset?: string,
  32. * dbname?: string,
  33. * default_dbname?: string,
  34. * driver?: key-of<self::DRIVER_MAP>,
  35. * driverClass?: class-string<Driver>,
  36. * driverOptions?: array<mixed>,
  37. * host?: string,
  38. * password?: string,
  39. * path?: string,
  40. * pdo?: \PDO,
  41. * platform?: Platforms\AbstractPlatform,
  42. * port?: int,
  43. * user?: string,
  44. * }
  45. * @psalm-type Params = array{
  46. * charset?: string,
  47. * dbname?: string,
  48. * default_dbname?: string,
  49. * driver?: key-of<self::DRIVER_MAP>,
  50. * driverClass?: class-string<Driver>,
  51. * driverOptions?: array<mixed>,
  52. * host?: string,
  53. * keepSlave?: bool,
  54. * keepReplica?: bool,
  55. * master?: OverrideParams,
  56. * memory?: bool,
  57. * password?: string,
  58. * path?: string,
  59. * pdo?: \PDO,
  60. * platform?: Platforms\AbstractPlatform,
  61. * port?: int,
  62. * primary?: OverrideParams,
  63. * replica?: array<OverrideParams>,
  64. * sharding?: array<string,mixed>,
  65. * slaves?: array<OverrideParams>,
  66. * user?: string,
  67. * wrapperClass?: class-string<Connection>,
  68. * }
  69. */
  70. final class DriverManager
  71. {
  72. /**
  73. * List of supported drivers and their mappings to the driver classes.
  74. *
  75. * To add your own driver use the 'driverClass' parameter to {@link DriverManager::getConnection()}.
  76. */
  77. private const DRIVER_MAP = [
  78. 'pdo_mysql' => PDO\MySQL\Driver::class,
  79. 'pdo_sqlite' => PDO\SQLite\Driver::class,
  80. 'pdo_pgsql' => PDO\PgSQL\Driver::class,
  81. 'pdo_oci' => PDO\OCI\Driver::class,
  82. 'oci8' => OCI8\Driver::class,
  83. 'ibm_db2' => IBMDB2\Driver::class,
  84. 'pdo_sqlsrv' => PDO\SQLSrv\Driver::class,
  85. 'mysqli' => Mysqli\Driver::class,
  86. 'drizzle_pdo_mysql' => DrizzlePDOMySql\Driver::class,
  87. 'sqlanywhere' => SQLAnywhere\Driver::class,
  88. 'sqlsrv' => SQLSrv\Driver::class,
  89. ];
  90. /**
  91. * List of URL schemes from a database URL and their mappings to driver.
  92. *
  93. * @var string[]
  94. */
  95. private static $driverSchemeAliases = [
  96. 'db2' => 'ibm_db2',
  97. 'mssql' => 'pdo_sqlsrv',
  98. 'mysql' => 'pdo_mysql',
  99. 'mysql2' => 'pdo_mysql', // Amazon RDS, for some weird reason
  100. 'postgres' => 'pdo_pgsql',
  101. 'postgresql' => 'pdo_pgsql',
  102. 'pgsql' => 'pdo_pgsql',
  103. 'sqlite' => 'pdo_sqlite',
  104. 'sqlite3' => 'pdo_sqlite',
  105. ];
  106. /**
  107. * Private constructor. This class cannot be instantiated.
  108. *
  109. * @codeCoverageIgnore
  110. */
  111. private function __construct()
  112. {
  113. }
  114. /**
  115. * Creates a connection object based on the specified parameters.
  116. * This method returns a Doctrine\DBAL\Connection which wraps the underlying
  117. * driver connection.
  118. *
  119. * $params must contain at least one of the following.
  120. *
  121. * Either 'driver' with one of the array keys of {@link DRIVER_MAP},
  122. * OR 'driverClass' that contains the full class name (with namespace) of the
  123. * driver class to instantiate.
  124. *
  125. * Other (optional) parameters:
  126. *
  127. * <b>user (string)</b>:
  128. * The username to use when connecting.
  129. *
  130. * <b>password (string)</b>:
  131. * The password to use when connecting.
  132. *
  133. * <b>driverOptions (array)</b>:
  134. * Any additional driver-specific options for the driver. These are just passed
  135. * through to the driver.
  136. *
  137. * <b>pdo</b>:
  138. * You can pass an existing PDO instance through this parameter. The PDO
  139. * instance will be wrapped in a Doctrine\DBAL\Connection.
  140. * This feature is deprecated and no longer supported in 3.0.x version.
  141. *
  142. * <b>wrapperClass</b>:
  143. * You may specify a custom wrapper class through the 'wrapperClass'
  144. * parameter but this class MUST inherit from Doctrine\DBAL\Connection.
  145. *
  146. * <b>driverClass</b>:
  147. * The driver class to use.
  148. *
  149. * @param array<string,mixed> $params
  150. * @param Configuration|null $config The configuration to use.
  151. * @param EventManager|null $eventManager The event manager to use.
  152. *
  153. * @throws Exception
  154. *
  155. * @phpstan-param array<string,mixed> $params
  156. * @psalm-param Params $params
  157. * @psalm-return ($params is array{wrapperClass:mixed} ? T : Connection)
  158. * @template T of Connection
  159. */
  160. public static function getConnection(
  161. array $params,
  162. ?Configuration $config = null,
  163. ?EventManager $eventManager = null
  164. ): Connection {
  165. // create default config and event manager, if not set
  166. if (! $config) {
  167. $config = new Configuration();
  168. }
  169. if (! $eventManager) {
  170. $eventManager = new EventManager();
  171. }
  172. $params = self::parseDatabaseUrl($params);
  173. // @todo: deprecated, notice thrown by connection constructor
  174. if (isset($params['master'])) {
  175. $params['master'] = self::parseDatabaseUrl($params['master']);
  176. }
  177. // @todo: deprecated, notice thrown by connection constructor
  178. if (isset($params['slaves'])) {
  179. foreach ($params['slaves'] as $key => $slaveParams) {
  180. $params['slaves'][$key] = self::parseDatabaseUrl($slaveParams);
  181. }
  182. }
  183. // URL support for PrimaryReplicaConnection
  184. if (isset($params['primary'])) {
  185. $params['primary'] = self::parseDatabaseUrl($params['primary']);
  186. }
  187. if (isset($params['replica'])) {
  188. foreach ($params['replica'] as $key => $replicaParams) {
  189. $params['replica'][$key] = self::parseDatabaseUrl($replicaParams);
  190. }
  191. }
  192. // URL support for PoolingShardConnection
  193. if (isset($params['global'])) {
  194. $params['global'] = self::parseDatabaseUrl($params['global']);
  195. }
  196. if (isset($params['shards'])) {
  197. foreach ($params['shards'] as $key => $shardParams) {
  198. $params['shards'][$key] = self::parseDatabaseUrl($shardParams);
  199. }
  200. }
  201. // check for existing pdo object
  202. if (isset($params['pdo']) && ! $params['pdo'] instanceof \PDO) {
  203. throw Exception::invalidPdoInstance();
  204. }
  205. if (isset($params['pdo'])) {
  206. Deprecation::trigger(
  207. 'doctrine/dbal',
  208. 'https://github.com/doctrine/dbal/pull/3554',
  209. 'Passing a user provided PDO instance directly to Doctrine is deprecated.'
  210. );
  211. $params['pdo']->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
  212. $params['pdo']->setAttribute(\PDO::ATTR_STATEMENT_CLASS, [PDODriverStatement::class, []]);
  213. $params['driver'] = 'pdo_' . $params['pdo']->getAttribute(\PDO::ATTR_DRIVER_NAME);
  214. }
  215. $driver = self::createDriver($params);
  216. $wrapperClass = Connection::class;
  217. if (isset($params['wrapperClass'])) {
  218. if (! is_subclass_of($params['wrapperClass'], $wrapperClass)) {
  219. throw Exception::invalidWrapperClass($params['wrapperClass']);
  220. }
  221. /** @var class-string<Connection> $wrapperClass */
  222. $wrapperClass = $params['wrapperClass'];
  223. }
  224. return new $wrapperClass($params, $driver, $config, $eventManager);
  225. }
  226. /**
  227. * Returns the list of supported drivers.
  228. *
  229. * @return string[]
  230. */
  231. public static function getAvailableDrivers(): array
  232. {
  233. return array_keys(self::DRIVER_MAP);
  234. }
  235. /**
  236. * @param array<string,mixed> $params
  237. *
  238. * @throws Exception
  239. *
  240. * @phpstan-param array<string,mixed> $params
  241. * @psalm-param Params $params
  242. */
  243. private static function createDriver(array $params): Driver
  244. {
  245. if (isset($params['driverClass'])) {
  246. $interfaces = class_implements($params['driverClass'], true);
  247. if ($interfaces === false || ! in_array(Driver::class, $interfaces)) {
  248. throw Exception::invalidDriverClass($params['driverClass']);
  249. }
  250. return new $params['driverClass']();
  251. }
  252. if (isset($params['driver'])) {
  253. if (! isset(self::DRIVER_MAP[$params['driver']])) {
  254. throw Exception::unknownDriver($params['driver'], array_keys(self::DRIVER_MAP));
  255. }
  256. $class = self::DRIVER_MAP[$params['driver']];
  257. return new $class();
  258. }
  259. throw Exception::driverRequired();
  260. }
  261. /**
  262. * Normalizes the given connection URL path.
  263. *
  264. * @return string The normalized connection URL path
  265. */
  266. private static function normalizeDatabaseUrlPath(string $urlPath): string
  267. {
  268. // Trim leading slash from URL path.
  269. return substr($urlPath, 1);
  270. }
  271. /**
  272. * Extracts parts from a database URL, if present, and returns an
  273. * updated list of parameters.
  274. *
  275. * @param mixed[] $params The list of parameters.
  276. *
  277. * @return mixed[] A modified list of parameters with info from a database
  278. * URL extracted into indidivual parameter parts.
  279. *
  280. * @throws Exception
  281. *
  282. * @phpstan-param array<string,mixed> $params
  283. * @phpstan-return array<string,mixed>
  284. * @psalm-param Params $params
  285. * @psalm-return Params
  286. */
  287. private static function parseDatabaseUrl(array $params): array
  288. {
  289. if (! isset($params['url'])) {
  290. return $params;
  291. }
  292. // (pdo_)?sqlite3?:///... => (pdo_)?sqlite3?://localhost/... or else the URL will be invalid
  293. $url = preg_replace('#^((?:pdo_)?sqlite3?):///#', '$1://localhost/', $params['url']);
  294. assert(is_string($url));
  295. $url = parse_url($url);
  296. if ($url === false) {
  297. throw new Exception('Malformed parameter "url".');
  298. }
  299. foreach ($url as $param => $value) {
  300. if (! is_string($value)) {
  301. continue;
  302. }
  303. $url[$param] = rawurldecode($value);
  304. }
  305. // If we have a connection URL, we have to unset the default PDO instance connection parameter (if any)
  306. // as we cannot merge connection details from the URL into the PDO instance (URL takes precedence).
  307. unset($params['pdo']);
  308. $params = self::parseDatabaseUrlScheme($url['scheme'] ?? null, $params);
  309. if (isset($url['host'])) {
  310. $params['host'] = $url['host'];
  311. }
  312. if (isset($url['port'])) {
  313. $params['port'] = $url['port'];
  314. }
  315. if (isset($url['user'])) {
  316. $params['user'] = $url['user'];
  317. }
  318. if (isset($url['pass'])) {
  319. $params['password'] = $url['pass'];
  320. }
  321. $params = self::parseDatabaseUrlPath($url, $params);
  322. $params = self::parseDatabaseUrlQuery($url, $params);
  323. return $params;
  324. }
  325. /**
  326. * Parses the given connection URL and resolves the given connection parameters.
  327. *
  328. * Assumes that the connection URL scheme is already parsed and resolved into the given connection parameters
  329. * via {@link parseDatabaseUrlScheme}.
  330. *
  331. * @see parseDatabaseUrlScheme
  332. *
  333. * @param mixed[] $url The URL parts to evaluate.
  334. * @param mixed[] $params The connection parameters to resolve.
  335. *
  336. * @return mixed[] The resolved connection parameters.
  337. */
  338. private static function parseDatabaseUrlPath(array $url, array $params): array
  339. {
  340. if (! isset($url['path'])) {
  341. return $params;
  342. }
  343. $url['path'] = self::normalizeDatabaseUrlPath($url['path']);
  344. // If we do not have a known DBAL driver, we do not know any connection URL path semantics to evaluate
  345. // and therefore treat the path as regular DBAL connection URL path.
  346. if (! isset($params['driver'])) {
  347. return self::parseRegularDatabaseUrlPath($url, $params);
  348. }
  349. if (strpos($params['driver'], 'sqlite') !== false) {
  350. return self::parseSqliteDatabaseUrlPath($url, $params);
  351. }
  352. return self::parseRegularDatabaseUrlPath($url, $params);
  353. }
  354. /**
  355. * Parses the query part of the given connection URL and resolves the given connection parameters.
  356. *
  357. * @param mixed[] $url The connection URL parts to evaluate.
  358. * @param mixed[] $params The connection parameters to resolve.
  359. *
  360. * @return mixed[] The resolved connection parameters.
  361. */
  362. private static function parseDatabaseUrlQuery(array $url, array $params): array
  363. {
  364. if (! isset($url['query'])) {
  365. return $params;
  366. }
  367. $query = [];
  368. parse_str($url['query'], $query); // simply ingest query as extra params, e.g. charset or sslmode
  369. return array_merge($params, $query); // parse_str wipes existing array elements
  370. }
  371. /**
  372. * Parses the given regular connection URL and resolves the given connection parameters.
  373. *
  374. * Assumes that the "path" URL part is already normalized via {@link normalizeDatabaseUrlPath}.
  375. *
  376. * @see normalizeDatabaseUrlPath
  377. *
  378. * @param mixed[] $url The regular connection URL parts to evaluate.
  379. * @param mixed[] $params The connection parameters to resolve.
  380. *
  381. * @return mixed[] The resolved connection parameters.
  382. */
  383. private static function parseRegularDatabaseUrlPath(array $url, array $params): array
  384. {
  385. $params['dbname'] = $url['path'];
  386. return $params;
  387. }
  388. /**
  389. * Parses the given SQLite connection URL and resolves the given connection parameters.
  390. *
  391. * Assumes that the "path" URL part is already normalized via {@link normalizeDatabaseUrlPath}.
  392. *
  393. * @see normalizeDatabaseUrlPath
  394. *
  395. * @param mixed[] $url The SQLite connection URL parts to evaluate.
  396. * @param mixed[] $params The connection parameters to resolve.
  397. *
  398. * @return mixed[] The resolved connection parameters.
  399. */
  400. private static function parseSqliteDatabaseUrlPath(array $url, array $params): array
  401. {
  402. if ($url['path'] === ':memory:') {
  403. $params['memory'] = true;
  404. return $params;
  405. }
  406. $params['path'] = $url['path']; // pdo_sqlite driver uses 'path' instead of 'dbname' key
  407. return $params;
  408. }
  409. /**
  410. * Parses the scheme part from given connection URL and resolves the given connection parameters.
  411. *
  412. * @param string|null $scheme The connection URL scheme, if available
  413. * @param mixed[] $params The connection parameters to resolve.
  414. *
  415. * @return mixed[] The resolved connection parameters.
  416. *
  417. * @throws Exception If parsing failed or resolution is not possible.
  418. */
  419. private static function parseDatabaseUrlScheme($scheme, array $params): array
  420. {
  421. if ($scheme !== null) {
  422. // The requested driver from the URL scheme takes precedence
  423. // over the default custom driver from the connection parameters (if any).
  424. unset($params['driverClass']);
  425. // URL schemes must not contain underscores, but dashes are ok
  426. $driver = str_replace('-', '_', $scheme);
  427. // The requested driver from the URL scheme takes precedence over the
  428. // default driver from the connection parameters. If the driver is
  429. // an alias (e.g. "postgres"), map it to the actual name ("pdo-pgsql").
  430. // Otherwise, let checkParams decide later if the driver exists.
  431. $params['driver'] = self::$driverSchemeAliases[$driver] ?? $driver;
  432. return $params;
  433. }
  434. // If a schemeless connection URL is given, we require a default driver or default custom driver
  435. // as connection parameter.
  436. if (! isset($params['driverClass']) && ! isset($params['driver'])) {
  437. throw Exception::driverRequired($params['url']);
  438. }
  439. return $params;
  440. }
  441. }