Skip to content

Commit 950dd45

Browse files
bcremernicolas-grekas
authored andcommitted
[HttpFoundation] Add support for mysql unix_socket and charset in PdoSessionHandler::buildDsnFromUrl
1 parent 75446eb commit 950dd45

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

Session/Storage/Handler/PdoSessionHandler.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,10 +486,32 @@ private function buildDsnFromUrl(string $dsnOrUrl): string
486486
$driver = substr($driver, 4);
487487
}
488488

489+
$dsn = null;
489490
switch ($driver) {
490491
case 'mysql':
492+
$dsn = 'mysql:';
493+
if ('' !== ($params['query'] ?? '')) {
494+
$queryParams = [];
495+
parse_str($params['query'], $queryParams);
496+
if ('' !== ($queryParams['charset'] ?? '')) {
497+
$dsn .= 'charset='.$queryParams['charset'].';';
498+
}
499+
500+
if ('' !== ($queryParams['unix_socket'] ?? '')) {
501+
$dsn .= 'unix_socket='.$queryParams['unix_socket'].';';
502+
503+
if (isset($params['path'])) {
504+
$dbName = substr($params['path'], 1); // Remove the leading slash
505+
$dsn .= 'dbname='.$dbName.';';
506+
}
507+
508+
return $dsn;
509+
}
510+
}
511+
// If "unix_socket" is not in the query, we continue with the same process as pgsql
512+
// no break
491513
case 'pgsql':
492-
$dsn = $driver.':';
514+
$dsn ?? $dsn = 'pgsql:';
493515

494516
if (isset($params['host']) && '' !== $params['host']) {
495517
$dsn .= 'host='.$params['host'].';';

Tests/Session/Storage/Handler/PdoSessionHandlerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ 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?charset=utf8mb4', 'mysql:charset=utf8mb4;host=localhost;dbname=test;'];
336+
yield ['mysql://localhost/test?unix_socket=socket.sock&charset=utf8mb4', 'mysql:charset=utf8mb4;unix_socket=socket.sock;dbname=test;'];
335337
yield ['mysql://localhost:56/test', 'mysql:host=localhost;port=56;dbname=test;'];
336338
yield ['mysql2://root:pwd@localhost/test', 'mysql:host=localhost;dbname=test;', 'root', 'pwd'];
337339
yield ['postgres://localhost/test', 'pgsql:host=localhost;dbname=test;'];

0 commit comments

Comments
 (0)