Skip to content

Commit bdc03bc

Browse files
committed
Simplify the logic
1 parent fff16a3 commit bdc03bc

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/Symfony/Component/HttpFoundation/Session/Storage/Handler/PdoSessionHandler.php

+17-10
Original file line numberDiff line numberDiff line change
@@ -486,26 +486,33 @@ private function buildDsnFromUrl(string $dsnOrUrl): string
486486
$driver = substr($driver, 4);
487487
}
488488

489+
$dsn = null;
489490
switch ($driver) {
490491
case 'mysql':
491-
case 'pgsql':
492-
$dsn = $driver.':';
493-
494-
$hasSocket = false;
495-
if ('mysql' === $driver && isset($params['query']) && '' !== $params['query']) {
492+
$dsn ='mysql:';
493+
if (isset($params['query']) && '' !== $params['query']) {
496494
$queryParams = [];
497495
parse_str($params['query'], $queryParams);
496+
if (isset($queryParams['charset']) && '' !== $queryParams['charset']) {
497+
$dsn .= 'charset='.$queryParams['charset'].';';
498+
}
499+
498500
if (isset($queryParams['unix_socket']) && '' !== $queryParams['unix_socket']) {
499501
$dsn .= 'unix_socket='.$queryParams['unix_socket'].';';
500-
$hasSocket = true;
501-
}
502502

503-
if (isset($queryParams['charset']) && '' !== $queryParams['charset']) {
504-
$dsn .= 'charset='.$queryParams['charset'].';';
503+
if (isset($params['path'])) {
504+
$dbName = substr($params['path'], 1); // Remove the leading slash
505+
$dsn .= 'dbname='.$dbName.';';
506+
}
507+
508+
return $dsn;
505509
}
506510
}
511+
// If "unix_socket" is not in the query, we continue with the same process as pgsql
512+
case 'pgsql':
513+
$dsn = $dsn ?? 'pgsql:';
507514

508-
if (!$hasSocket && isset($params['host']) && '' !== $params['host']) {
515+
if (isset($params['host']) && '' !== $params['host']) {
509516
$dsn .= 'host='.$params['host'].';';
510517
}
511518

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PdoSessionHandlerTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ public function testUrlDsn($url, $expectedDsn, $expectedUser = null, $expectedPa
332332
public function provideUrlDsnPairs()
333333
{
334334
yield ['mysql://localhost/test', 'mysql:host=localhost;dbname=test;'];
335-
yield ['mysql://localhost/test?unix_socket=socket.sock&charset=utf8mb4', 'mysql:unix_socket=socket.sock;charset=utf8mb4;dbname=test;'];
335+
yield ['mysql://localhost/test?unix_socket=socket.sock&charset=utf8mb4', 'mysql:charset=utf8mb4;unix_socket=socket.sock;dbname=test;'];
336336
yield ['mysql://localhost:56/test', 'mysql:host=localhost;port=56;dbname=test;'];
337337
yield ['mysql2://root:pwd@localhost/test', 'mysql:host=localhost;dbname=test;', 'root', 'pwd'];
338338
yield ['postgres://localhost/test', 'pgsql:host=localhost;dbname=test;'];

0 commit comments

Comments
 (0)