DriverManager.php 15 KB

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