Db: add parse_pdo_dsn() helper method

This commit is contained in:
Benjamin Renard 2023-11-22 11:28:08 +01:00
parent 9030a2ba79
commit a3156d4d05
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC

View file

@ -469,4 +469,22 @@ class Db {
$row[$field] = $this -> date2time($row[$field]);
return $row;
}
/**
* Helper method to parse PHP PDO DSN info
* @param string $dsn PHP PDO DSN to parse
* @return array|false
*/
public static function parse_pdo_dsn($dsn) {
if (!preg_match('/^([^:]+):(.+)$/', $dsn, $dsn_parts))
return false;
$info = ['protocol' => $dsn_parts[1]];
foreach(preg_split('/;+/', $dsn_parts[2]) as $part) {
if (preg_match('/^([^=]+)=(.*)$/', $part, $dsn_part))
$info[$dsn_part[1]] = $dsn_part[2];
}
Log::trace('parse_pdo_dsn(%s): %s', $dsn, vardump($info));
return $info;
}
}