5
5
use Doctrine \Common \EventManager ;
6
6
use Doctrine \DBAL \Configuration ;
7
7
use Doctrine \DBAL \Connection ;
8
+ use Doctrine \DBAL \Connection \StaticServerVersionProvider ;
9
+ use Doctrine \DBAL \ConnectionException ;
8
10
use Doctrine \DBAL \DriverManager ;
9
- use Doctrine \DBAL \Exception ;
10
11
use Doctrine \DBAL \Exception as DBALException ;
11
12
use Doctrine \DBAL \Exception \DriverException ;
13
+ use Doctrine \DBAL \Exception \DriverRequired ;
14
+ use Doctrine \DBAL \Exception \InvalidWrapperClass ;
12
15
use Doctrine \DBAL \Exception \MalformedDsnException ;
13
16
use Doctrine \DBAL \Platforms \AbstractMySQLPlatform ;
14
17
use Doctrine \DBAL \Platforms \AbstractPlatform ;
15
18
use Doctrine \DBAL \Tools \DsnParser ;
16
19
use Doctrine \DBAL \Types \Type ;
17
20
use Doctrine \Deprecations \Deprecation ;
21
+ use InvalidArgumentException ;
18
22
19
23
use function array_merge ;
24
+ use function class_exists ;
20
25
use function is_subclass_of ;
26
+ use function method_exists ;
21
27
use function trigger_deprecation ;
22
28
23
29
use const PHP_EOL ;
@@ -63,6 +69,10 @@ public function __construct(array $typesConfig, ?DsnParser $dsnParser = null)
63
69
*/
64
70
public function createConnection (array $ params , ?Configuration $ config = null , ?EventManager $ eventManager = null , array $ mappingTypes = [])
65
71
{
72
+ if (! method_exists (Connection::class, 'getEventManager ' ) && $ eventManager !== null ) {
73
+ throw new InvalidArgumentException ('Passing an EventManager instance is not supported with DBAL > 3 ' );
74
+ }
75
+
66
76
if (! $ this ->initialized ) {
67
77
$ this ->initializeTypes ();
68
78
}
@@ -92,17 +102,23 @@ public function createConnection(array $params, ?Configuration $config = null, ?
92
102
93
103
if (isset ($ params ['wrapperClass ' ])) {
94
104
if (! is_subclass_of ($ params ['wrapperClass ' ], Connection::class)) {
105
+ if (class_exists (InvalidWrapperClass::class)) {
106
+ throw InvalidWrapperClass::new ($ params ['wrapperClass ' ]);
107
+ }
108
+
95
109
throw DBALException::invalidWrapperClass ($ params ['wrapperClass ' ]);
96
110
}
97
111
98
112
$ wrapperClass = $ params ['wrapperClass ' ];
99
113
$ params ['wrapperClass ' ] = null ;
100
114
}
101
115
102
- $ connection = DriverManager::getConnection ($ params , $ config , $ eventManager );
116
+ $ connection = DriverManager::getConnection (... array_merge ([ $ params , $ config] , $ eventManager ? [ $ eventManager ] : []) );
103
117
$ params = $ this ->addDatabaseSuffix (array_merge ($ connection ->getParams (), $ overriddenOptions ));
104
118
$ driver = $ connection ->getDriver ();
105
- $ platform = $ driver ->getDatabasePlatform ();
119
+ $ platform = $ driver ->getDatabasePlatform (
120
+ ...(class_exists (StaticServerVersionProvider::class) ? [new StaticServerVersionProvider ($ params ['serverVersion ' ] ?? '' )] : [])
121
+ );
106
122
107
123
if (! isset ($ params ['charset ' ])) {
108
124
if ($ platform instanceof AbstractMySQLPlatform) {
@@ -134,7 +150,7 @@ public function createConnection(array $params, ?Configuration $config = null, ?
134
150
135
151
$ connection = new $ wrapperClass ($ params , $ driver , $ config , $ eventManager );
136
152
} else {
137
- $ connection = DriverManager::getConnection ($ params , $ config , $ eventManager );
153
+ $ connection = DriverManager::getConnection (... array_merge ([ $ params , $ config] , $ eventManager ? [ $ eventManager ] : []) );
138
154
}
139
155
140
156
if (! empty ($ mappingTypes )) {
@@ -162,7 +178,9 @@ private function getDatabasePlatform(Connection $connection): AbstractPlatform
162
178
try {
163
179
return $ connection ->getDatabasePlatform ();
164
180
} catch (DriverException $ driverException ) {
165
- throw new DBALException (
181
+ $ class = class_exists (DBALException::class) ? DBALException::class : ConnectionException::class;
182
+
183
+ throw new $ class (
166
184
'An exception occurred while establishing a connection to figure out your platform version. ' . PHP_EOL .
167
185
"You can circumvent this by setting a 'server_version' configuration value " . PHP_EOL . PHP_EOL .
168
186
'For further information have a look at: ' . PHP_EOL .
@@ -226,7 +244,7 @@ private function addDatabaseSuffix(array $params): array
226
244
* URL extracted into individual parameter parts.
227
245
* @psalm-return Params
228
246
*
229
- * @throws Exception
247
+ * @throws DBALException
230
248
*/
231
249
private function parseDatabaseUrl (array $ params ): array
232
250
{
@@ -237,7 +255,7 @@ private function parseDatabaseUrl(array $params): array
237
255
try {
238
256
$ parsedParams = $ this ->dsnParser ->parse ($ params ['url ' ]);
239
257
} catch (MalformedDsnException $ e ) {
240
- throw new Exception ('Malformed parameter "url". ' , 0 , $ e );
258
+ throw new MalformedDsnException ('Malformed parameter "url". ' , 0 , $ e );
241
259
}
242
260
243
261
if (isset ($ parsedParams ['driver ' ])) {
@@ -251,7 +269,11 @@ private function parseDatabaseUrl(array $params): array
251
269
// If a schemeless connection URL is given, we require a default driver or default custom driver
252
270
// as connection parameter.
253
271
if (! isset ($ params ['driverClass ' ]) && ! isset ($ params ['driver ' ])) {
254
- throw Exception::driverRequired ($ params ['url ' ]);
272
+ if (class_exists (DriverRequired::class)) {
273
+ throw DriverRequired::new ($ params ['url ' ]);
274
+ }
275
+
276
+ throw DBALException::driverRequired ($ params ['url ' ]);
255
277
}
256
278
257
279
unset($ params ['url ' ]);
0 commit comments