2
2
3
3
namespace Nettrine \DBAL \DI ;
4
4
5
+ use Doctrine \DBAL \Tools \DsnParser ;
5
6
use Nette \DI \CompilerExtension ;
6
7
use Nette \DI \Definitions \Statement ;
7
8
use Nette \PhpGenerator \ClassType ;
21
22
* charset: string,
22
23
* connectstring: string,
23
24
* dbname: string,
25
+ * defaultTableOptions: array<string, mixed>,
24
26
* driver: string,
27
+ * driverClass: string,
25
28
* driverOptions: mixed[],
26
29
* exclusive: bool,
27
30
* gssencmode: string,
28
31
* host: string,
29
32
* instancename: string,
33
+ * keepReplica: bool,
30
34
* memory: bool,
31
35
* middlewares: array<string, string|array<string>|Statement>,
32
36
* password: string,
33
37
* path: string,
34
38
* persistent: bool,
35
39
* pooled: bool,
36
40
* port: int,
41
+ * primary: array<string, scalar>,
37
42
* protocol: string,
38
43
* resultCache: mixed,
39
44
* schemaAssetsFilter: mixed,
40
45
* schemaManagerFactory: mixed,
41
46
* serverVersion: string,
42
47
* service: bool,
43
48
* servicename: string,
49
+ * sessionMode: int,
44
50
* ssl_ca: string,
45
51
* ssl_capath: string,
46
52
* ssl_cert: string,
52
58
* sslmode: string,
53
59
* sslrootcert: string,
54
60
* unix_socket: string,
55
- * user: string
61
+ * user: string,
62
+ * wrapperClass: string,
56
63
* }
57
64
*/
58
65
class DbalExtension extends CompilerExtension
@@ -61,6 +68,13 @@ class DbalExtension extends CompilerExtension
61
68
public const MIDDLEWARE_TAG = 'nettrine.dbal.middleware ' ;
62
69
public const MIDDLEWARE_INTERNAL_TAG = 'nettrine.dbal.middleware.internal ' ;
63
70
public const CONNECTION_TAG = 'nettrine.dbal.connection ' ;
71
+ public const DSN_MAPPING = [
72
+ 'mysql ' => 'mysqli ' ,
73
+ 'mariadb ' => 'mysqli ' ,
74
+ 'postgres ' => 'pdo_pgsql ' ,
75
+ 'postgresql ' => 'pdo_pgsql ' ,
76
+ 'sqlite ' => 'pdo_sqlite ' ,
77
+ ];
64
78
65
79
/** @var AbstractPass[] */
66
80
protected array $ passes = [];
@@ -79,6 +93,18 @@ public function getConfigSchema(): Schema
79
93
Expect::type (Statement::class)->required (),
80
94
);
81
95
96
+ $ dsnTransformer = static function (mixed $ connection ) {
97
+ if (is_array ($ connection )) {
98
+ if (isset ($ connection ['url ' ])) {
99
+ assert (is_string ($ connection ['url ' ]));
100
+ $ params = (new DsnParser (self ::DSN_MAPPING ))->parse ($ connection ['url ' ]);
101
+ $ connection = array_merge ($ connection , $ params );
102
+ }
103
+ }
104
+
105
+ return $ connection ;
106
+ };
107
+
82
108
return Expect::structure ([
83
109
'debug ' => Expect::structure ([
84
110
'panel ' => Expect::bool (false ),
@@ -92,12 +118,15 @@ public function getConfigSchema(): Schema
92
118
'charset ' => Expect::string (),
93
119
'connectstring ' => Expect::string (),
94
120
'dbname ' => Expect::string (),
95
- 'driver ' => Expect::anyOf ('pdo_sqlite ' , 'sqlite3 ' , 'pdo_mysql ' , 'mysqli ' , 'pdo_pgsql ' , 'pgsql ' , 'pdo_oci ' , 'oci8 ' , 'pdo_sqlsrv ' , 'sqlsrv ' , 'ibm_db2 ' ),
121
+ 'defaultTableOptions ' => Expect::arrayOf (Expect::mixed (), Expect::string ()),
122
+ 'driver ' => Expect::anyOf ('pdo_sqlite ' , 'sqlite3 ' , 'pdo_mysql ' , 'mysqli ' , 'pdo_pgsql ' , 'pgsql ' , 'pdo_oci ' , 'oci8 ' , 'pdo_sqlsrv ' , 'sqlsrv ' , 'ibm_db2 ' )->required (),
123
+ 'driverClass ' => Expect::string (),
96
124
'driverOptions ' => Expect::anyOf (Expect::null (), Expect::array ()),
97
125
'exclusive ' => Expect::bool (),
98
126
'gssencmode ' => Expect::string (),
99
127
'host ' => Expect::string (),
100
128
'instancename ' => Expect::string (),
129
+ 'keepReplica ' => Expect::bool (),
101
130
'memory ' => Expect::bool (),
102
131
'password ' => Expect::string (),
103
132
'path ' => Expect::string (),
@@ -108,6 +137,7 @@ public function getConfigSchema(): Schema
108
137
'serverVersion ' => Expect::string (),
109
138
'service ' => Expect::bool (),
110
139
'servicename ' => Expect::string (),
140
+ 'sessionMode ' => Expect::int (),
111
141
'ssl_ca ' => Expect::string (),
112
142
'ssl_capath ' => Expect::string (),
113
143
'ssl_cert ' => Expect::string (),
@@ -119,14 +149,39 @@ public function getConfigSchema(): Schema
119
149
'sslmode ' => Expect::string (),
120
150
'sslrootcert ' => Expect::string (),
121
151
'unix_socket ' => Expect::string (),
152
+ 'url ' => Expect::string (),
122
153
'user ' => Expect::string (),
154
+ 'wrapperClass ' => Expect::string (),
155
+ 'replica ' => Expect::arrayOf (
156
+ Expect::arrayOf (
157
+ Expect::scalar (),
158
+ Expect::string ()
159
+ )->before ($ dsnTransformer ),
160
+ Expect::string ()->required ()
161
+ ),
162
+ 'primary ' => Expect::arrayOf (
163
+ Expect::scalar (),
164
+ Expect::string ()
165
+ )->before ($ dsnTransformer ),
123
166
// Configuration
124
167
'middlewares ' => Expect::arrayOf ($ expectService , Expect::string ()->required ()),
125
168
'resultCache ' => (clone $ expectService ),
126
169
'schemaAssetsFilter ' => (clone $ expectService ),
127
170
'schemaManagerFactory ' => (clone $ expectService ),
128
171
'autoCommit ' => Expect::bool (true ),
129
- ]),
172
+ ])
173
+ ->assert (
174
+ fn (stdClass $ connection ) => !(
175
+ $ connection ->url === null
176
+ && $ connection ->host === null
177
+ && $ connection ->port === null
178
+ && $ connection ->path === null
179
+ && $ connection ->user === null
180
+ && $ connection ->password === null
181
+ ),
182
+ 'Configure DNS url or explicit host, port, user, password, dbname and others. '
183
+ )
184
+ ->before ($ dsnTransformer ),
130
185
Expect::string ()->required (),
131
186
)->min (1 )->required (),
132
187
]);
0 commit comments