Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 620989d

Browse filesBrowse files
feature #40300 [HttpFoundation] Add support for mysql unix_socket and charset in PdoSessionHandler::buildDsnFromUrl (bcremer, Nyholm)
This PR was merged into the 5.3-dev branch. Discussion ---------- [HttpFoundation] Add support for mysql unix_socket and charset in PdoSessionHandler::buildDsnFromUrl | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #40290 | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Commits ------- f4e50fb [HttpFoundation] Add support for mysql unix_socket and charset in PdoSessionHandler::buildDsnFromUrl
2 parents 3bf6798 + f4e50fb commit 620989d
Copy full SHA for 620989d

File tree

2 files changed

+25
-1
lines changed
Filter options

2 files changed

+25
-1
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Session/Storage/Handler/PdoSessionHandler.php
+23-1Lines 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'].';';

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PdoSessionHandlerTest.php
+2Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.