@@ -26,38 +26,38 @@ class ConnectionParameters
26
26
/**
27
27
* Return common connection parameters.
28
28
*
29
- * @return array{driver: string , user: string, password: null|string, host: string, dbname: null|string, port: string , unix_socket: null|string, driverOptions: array<string, string>}
29
+ * @return array{driver: ('ibm_db2'|'mysqli'|'oci8'|'pdo_mysql'|'pdo_oci'|'pdo_pgsql'|'pdo_sqlite'|'pdo_sqlsrv'|'pgsql'|'sqlite3'|'sqlsrv') , user: string, password: null|string, host: string, dbname: null|string, port: int , unix_socket: null|string, driverOptions: array<string, string>}
30
30
*/
31
31
public static function getCommonConnectionParameters (): array
32
32
{
33
33
// phpcs:enable Generic.Files.LineLength.MaxExceeded
34
34
$ connectionParams = [
35
- 'driver ' => $ GLOBALS [ ' db_type ' ] ,
35
+ 'driver ' => static :: checkDriver () ,
36
36
'user ' => $ GLOBALS ['db_username ' ],
37
- 'password ' => null ,
37
+ 'password ' => '' ,
38
38
'host ' => $ GLOBALS ['db_host ' ],
39
- 'dbname ' => null ,
40
- 'port ' => $ GLOBALS ['db_port ' ],
39
+ 'dbname ' => ' main ' ,
40
+ 'port ' => ( int ) $ GLOBALS ['db_port ' ],
41
41
];
42
42
43
- if (isset ( $ GLOBALS ['db_name ' ]) ) {
43
+ if (null !== $ GLOBALS ['db_name ' ]) {
44
44
$ connectionParams ['dbname ' ] = $ GLOBALS ['db_name ' ];
45
45
}
46
46
47
47
if (isset ($ GLOBALS ['db_server ' ])) {
48
- $ connectionParams ['server ' ] = $ GLOBALS ['db_server ' ];
48
+ $ connectionParams ['server ' ] = ( string ) $ GLOBALS ['db_server ' ];
49
49
}
50
50
51
51
if (!empty ($ GLOBALS ['db_password ' ])) {
52
- $ connectionParams ['password ' ] = $ GLOBALS ['db_password ' ];
52
+ $ connectionParams ['password ' ] = ( string ) $ GLOBALS ['db_password ' ];
53
53
}
54
54
55
55
if (isset ($ GLOBALS ['db_unix_socket ' ])) {
56
56
$ connectionParams ['unix_socket ' ] = $ GLOBALS ['db_unix_socket ' ];
57
57
}
58
58
59
59
if (isset ($ GLOBALS ['db_version ' ])) {
60
- $ connectionParams ['driverOptions ' ]['server_version ' ] = ( string ) $ GLOBALS ['db_version ' ];
60
+ $ connectionParams ['driverOptions ' ]['server_version ' ] = $ GLOBALS ['db_version ' ];
61
61
}
62
62
63
63
return $ connectionParams ;
@@ -66,7 +66,7 @@ public static function getCommonConnectionParameters(): array
66
66
/**
67
67
* Return connection parameters.
68
68
*
69
- * @return array<string, string>
69
+ * @return array{driver: ('ibm_db2'|'mysqli'|'oci8'|'pdo_mysql'|'pdo_oci'|'pdo_pgsql'|'pdo_sqlite'|'pdo_sqlsrv'|'pgsql'|'sqlite3'|'sqlsrv'), user: string, password: string, host: string, dbname: string, port: int, unix_socket: string, driverOptions: array <string, string>}
70
70
*
71
71
* @throws Exception when connection is not successful
72
72
*/
@@ -77,22 +77,36 @@ public static function getConnectionParameters(): array
77
77
78
78
$ connection = DriverManager::getConnection ($ parameters );
79
79
$ manager = $ connection ->createSchemaManager ();
80
- $ dbName = $ GLOBALS ['db_name ' ];
80
+ $ dbName = ( string ) $ GLOBALS ['db_name ' ];
81
81
$ manager ->dropDatabase ($ dbName );
82
82
$ manager ->createDatabase ($ dbName );
83
83
$ parameters ['dbname ' ] = $ dbName ;
84
84
85
85
return $ parameters ;
86
86
}
87
87
88
+ /**
89
+ * @return ('ibm_db2'|'mysqli'|'oci8'|'pdo_mysql'|'pdo_oci'|'pdo_pgsql'|'pdo_sqlite'|'pdo_sqlsrv'|'pgsql'|'sqlite3'|'sqlsrv') driver
90
+ */
91
+ private static function checkDriver (): string
92
+ {
93
+ $ drivers = DriverManager::getAvailableDrivers ();
94
+
95
+ if (in_array ($ GLOBALS ['db_type ' ], $ drivers )) {
96
+ return $ GLOBALS ['db_type ' ];
97
+ }
98
+
99
+ throw new \InvalidArgumentException (sprintf ('Driver %s is not available. ' , $ GLOBALS ['driver ' ]));
100
+ }
101
+
88
102
/**
89
103
* Return connection parameters for alternate database.
90
104
*
91
105
* Alternate database is used with PostgreSQL and doctrine/orm3.0,
92
106
* because we cannot drop database as long as we are connected to it.
93
107
*/
94
- private static function getAlternateDatabaseName (): ? string
108
+ private static function getAlternateDatabaseName (): string
95
109
{
96
- return $ GLOBALS ['db_alternate ' ] ?? $ GLOBALS ['db_name ' ] ?? static ::getCommonConnectionParameters ()['dbname ' ];
110
+ return $ GLOBALS ['db_alternate ' ] ?? $ GLOBALS ['db_name ' ] ?? static ::getCommonConnectionParameters ()['dbname ' ] ?? ' main ' ;
97
111
}
98
112
}
0 commit comments