function PdoSessionHandler::buildDsnFromUrl
Builds a PDO DSN from a URL-like connection string.
@todo implement missing support for oci DSN (which look totally different from other PDO ones)
1 call to PdoSessionHandler::buildDsnFromUrl()
- PdoSessionHandler::__construct in vendor/
symfony/ http-foundation/ Session/ Storage/ Handler/ PdoSessionHandler.php - You can either pass an existing database connection as PDO instance or pass a DSN string that will be used to lazy-connect to the database when the session is actually used. Furthermore it's possible to pass null which will then use theā¦
File
-
vendor/
symfony/ http-foundation/ Session/ Storage/ Handler/ PdoSessionHandler.php, line 434
Class
- PdoSessionHandler
- Session handler using a PDO connection to read and write data.
Namespace
Symfony\Component\HttpFoundation\Session\Storage\HandlerCode
private function buildDsnFromUrl(string $dsnOrUrl) : string {
// (pdo_)?sqlite3?:///... => (pdo_)?sqlite3?://localhost/... or else the URL will be invalid
$url = preg_replace('#^((?:pdo_)?sqlite3?):///#', '$1://localhost/', $dsnOrUrl);
$params = parse_url($url);
if (false === $params) {
return $dsnOrUrl;
// If the URL is not valid, let's assume it might be a DSN already.
}
$params = array_map('rawurldecode', $params);
// Override the default username and password. Values passed through options will still win over these in the constructor.
if (isset($params['user'])) {
$this->username = $params['user'];
}
if (isset($params['pass'])) {
$this->password = $params['pass'];
}
if (!isset($params['scheme'])) {
throw new \InvalidArgumentException('URLs without scheme are not supported to configure the PdoSessionHandler.');
}
$driverAliasMap = [
'mssql' => 'sqlsrv',
'mysql2' => 'mysql',
// Amazon RDS, for some weird reason
'postgres' => 'pgsql',
'postgresql' => 'pgsql',
'sqlite3' => 'sqlite',
];
$driver = $driverAliasMap[$params['scheme']] ?? $params['scheme'];
// Doctrine DBAL supports passing its internal pdo_* driver names directly too (allowing both dashes and underscores). This allows supporting the same here.
if (str_starts_with($driver, 'pdo_') || str_starts_with($driver, 'pdo-')) {
$driver = substr($driver, 4);
}
$dsn = null;
switch ($driver) {
case 'mysql':
$dsn = 'mysql:';
if ('' !== ($params['query'] ?? '')) {
$queryParams = [];
parse_str($params['query'], $queryParams);
if ('' !== ($queryParams['charset'] ?? '')) {
$dsn .= 'charset=' . $queryParams['charset'] . ';';
}
if ('' !== ($queryParams['unix_socket'] ?? '')) {
$dsn .= 'unix_socket=' . $queryParams['unix_socket'] . ';';
if (isset($params['path'])) {
$dbName = substr($params['path'], 1);
// Remove the leading slash
$dsn .= 'dbname=' . $dbName . ';';
}
return $dsn;
}
}
// If "unix_socket" is not in the query, we continue with the same process as pgsql
// no break
case 'pgsql':
$dsn ??= 'pgsql:';
if (isset($params['host']) && '' !== $params['host']) {
$dsn .= 'host=' . $params['host'] . ';';
}
if (isset($params['port']) && '' !== $params['port']) {
$dsn .= 'port=' . $params['port'] . ';';
}
if (isset($params['path'])) {
$dbName = substr($params['path'], 1);
// Remove the leading slash
$dsn .= 'dbname=' . $dbName . ';';
}
return $dsn;
case 'sqlite':
return 'sqlite:' . substr($params['path'], 1);
case 'sqlsrv':
$dsn = 'sqlsrv:server=';
if (isset($params['host'])) {
$dsn .= $params['host'];
}
if (isset($params['port']) && '' !== $params['port']) {
$dsn .= ',' . $params['port'];
}
if (isset($params['path'])) {
$dbName = substr($params['path'], 1);
// Remove the leading slash
$dsn .= ';Database=' . $dbName;
}
return $dsn;
default:
throw new \InvalidArgumentException(\sprintf('The scheme "%s" is not supported by the PdoSessionHandler URL configuration. Pass a PDO DSN directly.', $params['scheme']));
}
}