2
2
3
3
namespace Nettrine \DBAL ;
4
4
5
- use Doctrine \Common \EventManager ;
6
5
use Doctrine \DBAL \Configuration ;
7
6
use Doctrine \DBAL \Connection ;
7
+ use Doctrine \DBAL \ConnectionException ;
8
8
use Doctrine \DBAL \DriverManager ;
9
+ use Doctrine \DBAL \Exception \DriverException ;
10
+ use Doctrine \DBAL \Platforms \AbstractPlatform ;
9
11
use Doctrine \DBAL \Types \Type ;
10
12
13
+ /**
14
+ * @see https://github.com/doctrine/DoctrineBundle
15
+ * @phpstan-import-type Params from DriverManager
16
+ */
11
17
class ConnectionFactory
12
18
{
13
19
@@ -30,25 +36,52 @@ public function __construct(array $typesConfig = [], array $typesMapping = [])
30
36
}
31
37
32
38
/**
33
- * @param mixed[] $params
39
+ * @phpstan-param Params $params
40
+ * @param array<string, string> $typesMapping
34
41
*/
35
- public function createConnection (array $ params , ?Configuration $ config = null , ?EventManager $ em = null ): Connection
42
+ public function createConnection (
43
+ array $ params ,
44
+ ?Configuration $ config = null ,
45
+ array $ typesMapping = []
46
+ ): Connection
36
47
{
37
48
if (!$ this ->initialized ) {
38
49
$ this ->initializeTypes ();
39
50
}
40
51
41
- /** @phpstan-ignore-next-line */
42
- $ connection = DriverManager::getConnection ($ params , $ config, $ em );
43
- $ platform = $ connection ->getDatabasePlatform ();
52
+ $ config ??= new Configuration ();
53
+ $ connection = DriverManager::getConnection ($ params , $ config );
54
+ $ platform = $ this ->getDatabasePlatform ($ connection );
44
55
56
+ // Register types mapping (global)
45
57
foreach ($ this ->typesMapping as $ dbType => $ doctrineType ) {
46
58
$ platform ->registerDoctrineTypeMapping ($ dbType , $ doctrineType );
47
59
}
48
60
61
+ // Register types mapping (local)
62
+ foreach ($ typesMapping as $ dbType => $ doctrineType ) {
63
+ $ platform ->registerDoctrineTypeMapping ($ dbType , $ doctrineType );
64
+ }
65
+
49
66
return $ connection ;
50
67
}
51
68
69
+ private function getDatabasePlatform (Connection $ connection ): AbstractPlatform
70
+ {
71
+ try {
72
+ return $ connection ->getDatabasePlatform ();
73
+ } catch (DriverException $ driverException ) {
74
+ throw new ConnectionException (
75
+ 'An exception occurred while establishing a connection to figure out your platform version. ' . PHP_EOL .
76
+ "You can circumvent this by setting a 'serverVersion' configuration value " . PHP_EOL . PHP_EOL .
77
+ 'For further information have a look at: ' . PHP_EOL .
78
+ 'https://github.com/doctrine/DoctrineBundle/issues/673 ' ,
79
+ 0 ,
80
+ $ driverException ,
81
+ );
82
+ }
83
+ }
84
+
52
85
private function initializeTypes (): void
53
86
{
54
87
foreach ($ this ->typesConfig as $ type => $ class ) {
0 commit comments