summaryrefslogtreecommitdiff
path: root/vendor/doctrine/dbal/src/DriverManager.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/doctrine/dbal/src/DriverManager.php')
-rw-r--r--vendor/doctrine/dbal/src/DriverManager.php191
1 files changed, 191 insertions, 0 deletions
diff --git a/vendor/doctrine/dbal/src/DriverManager.php b/vendor/doctrine/dbal/src/DriverManager.php
new file mode 100644
index 0000000..8b41cbb
--- /dev/null
+++ b/vendor/doctrine/dbal/src/DriverManager.php
@@ -0,0 +1,191 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\DBAL;
6
7use Doctrine\DBAL\Driver\IBMDB2;
8use Doctrine\DBAL\Driver\Mysqli;
9use Doctrine\DBAL\Driver\OCI8;
10use Doctrine\DBAL\Driver\PDO;
11use Doctrine\DBAL\Driver\PgSQL;
12use Doctrine\DBAL\Driver\SQLite3;
13use Doctrine\DBAL\Driver\SQLSrv;
14use Doctrine\DBAL\Exception\DriverRequired;
15use Doctrine\DBAL\Exception\InvalidDriverClass;
16use Doctrine\DBAL\Exception\InvalidWrapperClass;
17use Doctrine\DBAL\Exception\UnknownDriver;
18use SensitiveParameter;
19
20use function array_keys;
21use function is_a;
22
23/**
24 * Factory for creating {@see Connection} instances.
25 *
26 * @psalm-type OverrideParams = array{
27 * application_name?: string,
28 * charset?: string,
29 * dbname?: string,
30 * defaultTableOptions?: array<string, mixed>,
31 * driver?: key-of<self::DRIVER_MAP>,
32 * driverClass?: class-string<Driver>,
33 * driverOptions?: array<mixed>,
34 * host?: string,
35 * memory?: bool,
36 * password?: string,
37 * path?: string,
38 * persistent?: bool,
39 * port?: int,
40 * serverVersion?: string,
41 * sessionMode?: int,
42 * user?: string,
43 * unix_socket?: string,
44 * wrapperClass?: class-string<Connection>,
45 * }
46 * @psalm-type Params = array{
47 * application_name?: string,
48 * charset?: string,
49 * dbname?: string,
50 * defaultTableOptions?: array<string, mixed>,
51 * driver?: key-of<self::DRIVER_MAP>,
52 * driverClass?: class-string<Driver>,
53 * driverOptions?: array<mixed>,
54 * host?: string,
55 * keepReplica?: bool,
56 * memory?: bool,
57 * password?: string,
58 * path?: string,
59 * persistent?: bool,
60 * port?: int,
61 * primary?: OverrideParams,
62 * replica?: array<OverrideParams>,
63 * serverVersion?: string,
64 * sessionMode?: int,
65 * user?: string,
66 * wrapperClass?: class-string<Connection>,
67 * unix_socket?: string,
68 * }
69 */
70final 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 {@see 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 'pgsql' => PgSQL\Driver::class,
87 'sqlsrv' => SQLSrv\Driver::class,
88 'sqlite3' => SQLite3\Driver::class,
89 ];
90
91 /**
92 * Private constructor. This class cannot be instantiated.
93 *
94 * @codeCoverageIgnore
95 */
96 private function __construct()
97 {
98 }
99
100 /**
101 * Creates a connection object based on the specified parameters.
102 * This method returns a Doctrine\DBAL\Connection which wraps the underlying
103 * driver connection.
104 *
105 * $params must contain at least one of the following.
106 *
107 * Either 'driver' with one of the array keys of {@see DRIVER_MAP},
108 * OR 'driverClass' that contains the full class name (with namespace) of the
109 * driver class to instantiate.
110 *
111 * Other (optional) parameters:
112 *
113 * <b>user (string)</b>:
114 * The username to use when connecting.
115 *
116 * <b>password (string)</b>:
117 * The password to use when connecting.
118 *
119 * <b>driverOptions (array)</b>:
120 * Any additional driver-specific options for the driver. These are just passed
121 * through to the driver.
122 *
123 * <b>wrapperClass</b>:
124 * You may specify a custom wrapper class through the 'wrapperClass'
125 * parameter but this class MUST inherit from Doctrine\DBAL\Connection.
126 *
127 * <b>driverClass</b>:
128 * The driver class to use.
129 *
130 * @param Configuration|null $config The configuration to use.
131 * @psalm-param Params $params
132 *
133 * @psalm-return ($params is array{wrapperClass: class-string<T>} ? T : Connection)
134 *
135 * @template T of Connection
136 */
137 public static function getConnection(
138 #[SensitiveParameter]
139 array $params,
140 ?Configuration $config = null,
141 ): Connection {
142 $config ??= new Configuration();
143 $driver = self::createDriver($params['driver'] ?? null, $params['driverClass'] ?? null);
144
145 foreach ($config->getMiddlewares() as $middleware) {
146 $driver = $middleware->wrap($driver);
147 }
148
149 /** @var class-string<Connection> $wrapperClass */
150 $wrapperClass = $params['wrapperClass'] ?? Connection::class;
151 if (! is_a($wrapperClass, Connection::class, true)) {
152 throw InvalidWrapperClass::new($wrapperClass);
153 }
154
155 return new $wrapperClass($params, $driver, $config);
156 }
157
158 /**
159 * Returns the list of supported drivers.
160 *
161 * @return string[]
162 * @psalm-return list<key-of<self::DRIVER_MAP>>
163 */
164 public static function getAvailableDrivers(): array
165 {
166 return array_keys(self::DRIVER_MAP);
167 }
168
169 /**
170 * @param class-string<Driver>|null $driverClass
171 * @param key-of<self::DRIVER_MAP>|null $driver
172 */
173 private static function createDriver(?string $driver, ?string $driverClass): Driver
174 {
175 if ($driverClass === null) {
176 if ($driver === null) {
177 throw DriverRequired::new();
178 }
179
180 if (! isset(self::DRIVER_MAP[$driver])) {
181 throw UnknownDriver::new($driver, array_keys(self::DRIVER_MAP));
182 }
183
184 $driverClass = self::DRIVER_MAP[$driver];
185 } elseif (! is_a($driverClass, Driver::class, true)) {
186 throw InvalidDriverClass::new($driverClass);
187 }
188
189 return new $driverClass();
190 }
191}