@@ -142,7 +142,7 @@ public static function getConnection(
142142 }
143143
144144 $ params = self ::parseDatabaseUrl ($ params );
145-
145+
146146 // check for existing pdo object
147147 if (isset ($ params ['pdo ' ]) && ! $ params ['pdo ' ] instanceof \PDO ) {
148148 throw DBALException::invalidPdoInstance ();
@@ -228,23 +228,23 @@ private static function parseDatabaseUrl(array $params)
228228 if (!isset ($ params ['url ' ])) {
229229 return $ params ;
230230 }
231-
231+
232232 // (pdo_)?sqlite3?:///... => (pdo_)?sqlite3?://localhost/... or else the URL will be invalid
233233 $ url = preg_replace ('#^((?:pdo_)?sqlite3?):///# ' , '$1://localhost/ ' , $ params ['url ' ]);
234-
234+
235235 $ url = parse_url ($ url );
236-
236+
237237 if ($ url === false ) {
238238 throw new DBALException ('Malformed parameter "url". ' );
239239 }
240-
240+
241241 if (isset ($ url ['scheme ' ])) {
242242 $ params ['driver ' ] = str_replace ('- ' , '_ ' , $ url ['scheme ' ]); // URL schemes must not contain underscores, but dashes are ok
243243 if (isset (self ::$ driverSchemeAliases [$ params ['driver ' ]])) {
244244 $ params ['driver ' ] = self ::$ driverSchemeAliases [$ params ['driver ' ]]; // use alias like "postgres", else we just let checkParams decide later if the driver exists (for literal "pdo-pgsql" etc)
245245 }
246246 }
247-
247+
248248 if (isset ($ url ['host ' ])) {
249249 $ params ['host ' ] = $ url ['host ' ];
250250 }
@@ -257,25 +257,65 @@ private static function parseDatabaseUrl(array $params)
257257 if (isset ($ url ['pass ' ])) {
258258 $ params ['password ' ] = $ url ['pass ' ];
259259 }
260-
261- if (isset ($ url ['path ' ])) {
262- $ nameKey = isset ($ url ['scheme ' ]) && strpos ($ url ['scheme ' ], 'sqlite ' ) !== false
263- ? 'path ' // pdo_sqlite driver uses 'path' instead of 'dbname' key
264- : 'dbname ' ;
265260
266- if (!isset ($ url ['scheme ' ]) || (strpos ($ url ['scheme ' ], 'sqlite ' ) !== false && $ url ['path ' ] == ':memory: ' )) {
267- $ params [$ nameKey ] = $ url ['path ' ]; // if the URL was just "sqlite::memory:", which parses to scheme and path only
268- } else {
269- $ params [$ nameKey ] = substr ($ url ['path ' ], 1 ); // strip the leading slash from the URL
270- }
261+ if (isset ($ url ['path ' ])) {
262+ $ params = self ::parseDatabaseUrlPath ($ url , $ params );
271263 }
272-
264+
273265 if (isset ($ url ['query ' ])) {
274266 $ query = array ();
275267 parse_str ($ url ['query ' ], $ query ); // simply ingest query as extra params, e.g. charset or sslmode
276268 $ params = array_merge ($ params , $ query ); // parse_str wipes existing array elements
277269 }
278-
270+
271+ return $ params ;
272+ }
273+
274+ /**
275+ * Parses the given URL and resolves the given connection parameters.
276+ *
277+ * @param array $url The URL parts to evaluate.
278+ * @param array $params The connection parameters to resolve.
279+ *
280+ * @return array The resolved connection parameters.
281+ */
282+ private static function parseDatabaseUrlPath (array $ url , array $ params )
283+ {
284+ if (!isset ($ url ['scheme ' ])) {
285+ $ params ['dbname ' ] = $ url ['path ' ];
286+
287+ return $ params ;
288+ }
289+
290+ $ url ['path ' ] = substr ($ url ['path ' ], 1 );
291+
292+ if (strpos ($ url ['scheme ' ], 'sqlite ' ) !== false ) {
293+ return self ::parseSqliteDatabaseUrlPath ($ url , $ params );
294+ }
295+
296+ $ params ['dbname ' ] = $ url ['path ' ];
297+
298+ return $ params ;
299+ }
300+
301+ /**
302+ * Parses the given SQLite URL and resolves the given connection parameters.
303+ *
304+ * @param array $url The SQLite URL parts to evaluate.
305+ * @param array $params The connection parameters to resolve.
306+ *
307+ * @return array The resolved connection parameters.
308+ */
309+ private static function parseSqliteDatabaseUrlPath (array $ url , array $ params )
310+ {
311+ if ($ url ['path ' ] === ':memory: ' ) {
312+ $ params ['memory ' ] = true ;
313+
314+ return $ params ;
315+ }
316+
317+ $ params ['path ' ] = $ url ['path ' ]; // pdo_sqlite driver uses 'path' instead of 'dbname' key
318+
279319 return $ params ;
280320 }
281321}
0 commit comments