Commit e8056f53 authored by David Zuelke's avatar David Zuelke

first stab at supporting database 'url' for connection

tests to come
parent e35ce048
......@@ -51,6 +51,22 @@ final class DriverManager
'sqlsrv' => 'Doctrine\DBAL\Driver\SQLSrv\Driver',
);
/**
* List of URL schemes from a database URL and their mappings to driver.
*/
private static $_driverSchemeAliases = array(
'db2' => 'ibm_db2',
'mssql' => 'pdo_sqlsrv',
'pdo_mssql' => 'pdo_sqlsrv',
'mysql' => 'pdo_mysql',
'mysql2' => 'pdo_mysql', // Amazon RDS, for some weird reason
'postgres' => 'pdo_pgsql',
'postgresql' => 'pdo_pgsql',
'pgsql' => 'pdo_pgsql',
'sqlite' => 'pdo_sqlite',
'sqlite3' => 'pdo_sqlite',
);
/**
* Private constructor. This class cannot be instantiated.
*/
......@@ -126,6 +142,8 @@ final class DriverManager
$eventManager = new EventManager();
}
$params = self::_parseDatabaseUrl($params);
// check for existing pdo object
if (isset($params['pdo']) && ! $params['pdo'] instanceof \PDO) {
throw DBALException::invalidPdoInstance();
......@@ -194,4 +212,65 @@ final class DriverManager
throw DBALException::invalidDriverClass($params['driverClass']);
}
}
/**
* Extracts parts from a database URL, if present, and returns an
* updated list of parameters.
*
* @param array $params The list of parameters.
*
* @param array A modified list of parameters with info from a database
* URL extracted into indidivual parameter parts.
*
*/
private static function _parseDatabaseUrl(array $params)
{
if (!isset($params['url'])) {
return;
}
// (pdo_)?sqlite3?:///... => (pdo_)?sqlite3?://localhost/... or else the URL will be invalid
$url = preg_replace('#^((?:pdo_)?sqlite3?):///#', '$1://localhost/', $url);
$url = parse_url($params['url']);
if($url === false) {
throw new DBALException('Malformed parameter "url".');
}
if (isset($url['scheme'])) {
if (isset(self::$_driverSchemeAliases[$url['scheme']])) {
$params['driver'] = self::$_driverSchemeAliases[$url['scheme']]; // use alias
} else {
$params['driver'] = $url['scheme']; // let's see what checkParams() says about it later
}
}
if (isset($url['host'])) {
$params['host'] = $url['host'];
}
if (isset($url['port'])) {
$params['port'] = $url['port'];
}
if (isset($url['user'])) {
$params['user'] = $url['user'];
}
if (isset($url['pass'])) {
$params['password'] = $url['pass'];
}
if (isset($url['path'])) {
if (!isset($url['scheme']) || (strpos($url['scheme'], 'sqlite') !== false && $url['path'] == ':memory:')) {
$params['dbname'] = $url['path']; // if the URL was just "sqlite::memory:", which parses to scheme and path only
} else {
$params['dbname'] = substr($url['path'], 1); // strip the leading slash from the URL
}
}
if (isset($url['query'])) {
$query = array();
parse_str($url['query'], $query); // simply ingest query as extra params, e.g. charset or sslmode
$params = array_merge($params, $query); // parse_str wipes existing array elements
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment