解析DSN路径格式
array Module_Database::parse_dsn( string $dsn )
参数列表
参数 类型 描述 默认值 $dsn
string
DSN string
array
public static function parse_dsn($dsn)
{
$db = array
(
'type' => false,
'username' => false,
'password' => false,
'hostname' => false,
'port' => false,
'persistent' => false,
'database' => false,
);
// Get the protocol and arguments
list ($db['type'], $connection) = explode('://', $dsn, 2);
if ($connection[0] === '/')
{
// Strip leading slash
$db['database'] = substr($connection, 1);
}
else
{
$connection = parse_url('http://' . $connection);
if (isset($connection['user']))
{
$db['username'] = $connection['user'];
}
if (isset($connection['pass']))
{
$db['password'] = $connection['pass'];
}
if (isset($connection['port']))
{
$db['port'] = $connection['port'];
}
if (isset($connection['host']))
{
if ($connection['host'] === 'unix(')
{
list ($db['persistent'], $connection['path']) = explode(')', $connection['path'], 2);
}
else
{
$db['hostname'] = $connection['host'];
}
}
if (isset($connection['path']) && $connection['path'])
{
// Strip leading slash
$db['database'] = trim(trim(substr($connection['path'], 1),'/'));
}
}
return $db;
}