@@ -141,30 +141,21 @@ private function skimUri(string $uri): string
141
141
if (false === $ parsedUrl = parse_url ($ uri )) {
142
142
throw new InvalidArgumentException (sprintf ('The given MongoDB Connection URI "%s" is invalid. ' , $ uri ));
143
143
}
144
-
145
- $ query = [];
146
- if (isset ($ parsedUrl ['query ' ])) {
147
- parse_str ($ parsedUrl ['query ' ], $ query );
148
- }
149
-
150
- if (isset ($ query ['collection ' ])) {
151
- $ this ->options ['collection ' ] = $ query ['collection ' ];
152
- $ queryStringPos = strrpos ($ uri , $ parsedUrl ['query ' ]);
153
- unset($ query ['collection ' ]);
154
- $ prefix = substr ($ uri , 0 , $ queryStringPos );
155
- $ newQuery = http_build_query ($ query , '' , '& ' , PHP_QUERY_RFC3986 );
156
- if (empty ($ newQuery )) {
157
- $ prefix = rtrim ($ prefix , '? ' );
158
- }
159
- $ suffix = substr ($ uri , $ queryStringPos + \strlen ($ parsedUrl ['query ' ]));
160
- $ uri = $ prefix .$ newQuery .$ suffix ;
161
- }
162
-
163
144
$ pathDb = ltrim ($ parsedUrl ['path ' ] ?? '' , '/ ' ) ?: null ;
164
145
if (null !== $ pathDb ) {
165
146
$ this ->options ['database ' ] = $ pathDb ;
166
147
}
167
148
149
+ $ matches = [];
150
+ if (preg_match ('/^(.*[\?&])collection=([^&#]*)&?(([^#]*).*)$/ ' , $ uri , $ matches )) {
151
+ $ prefix = $ matches [1 ];
152
+ $ this ->options ['collection ' ] = $ matches [2 ];
153
+ if (empty ($ matches [4 ])) {
154
+ $ prefix = substr ($ prefix , 0 , -1 );
155
+ }
156
+ $ uri = $ prefix .$ matches [3 ];
157
+ }
158
+
168
159
return $ uri ;
169
160
}
170
161
0 commit comments