Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
separate connection string parsing from connection
- Loading branch information
Showing
6 changed files
with
263 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
namespace ActiveRecord; | ||
|
||
class ConnectionInfo { | ||
|
||
public $protocol = null; | ||
public $host = null; | ||
public $port = null; | ||
public $database = null; | ||
|
||
public $username = null; | ||
public $password = null; | ||
|
||
public function __construct($input = array()){ | ||
foreach($input as $prop => $value){ | ||
$this->{$prop} = $value; | ||
} | ||
} | ||
|
||
/** | ||
* Parses a connection url and return a ConnectionInfo object | ||
* | ||
* Use this for any adapters that can take connection info in the form below | ||
* to set the adapters connection info. | ||
* | ||
* <code> | ||
* protocol://username:password@host[:port]/dbname | ||
* protocol://urlencoded%20username:urlencoded%20password@host[:port]/dbname?decode=true | ||
* protocol://username:password@unix(/some/file/path)/dbname | ||
* </code> | ||
* | ||
* Sqlite has a special syntax, as it does not need a database name or user authentication: | ||
* | ||
* <code> | ||
* sqlite://file.db | ||
* sqlite://../relative/path/to/file.db | ||
* sqlite://unix(/absolute/path/to/file.db) | ||
* sqlite://windows(c%2A/absolute/path/to/file.db) | ||
* </code> | ||
* | ||
* @param string $connection_url A connection URL | ||
* @return object the parsed URL as an object. | ||
*/ | ||
public static function from_connection_url($connection_url){ | ||
$url = @parse_url($connection_url); | ||
|
||
if (!isset($url['host'])) | ||
throw new DatabaseException('Database host must be specified in the connection string. If you want to specify an absolute filename, use e.g. sqlite://unix(/path/to/file)'); | ||
|
||
$info = new self(); | ||
$info->protocol = $url['scheme']; | ||
$info->host = $url['host']; | ||
if(isset($url['path'])){ | ||
$info->database = substr($url['path'], 1); | ||
} | ||
$info->username = isset($url['user']) ? $url['user'] : null; | ||
$info->password = isset($url['pass']) ? $url['pass'] : null; | ||
|
||
$allow_blank_db = ($info->protocol == 'sqlite'); | ||
|
||
if ($info->host == 'unix(') | ||
{ | ||
$socket_database = $info->host . '/' . $info->database; | ||
|
||
if ($allow_blank_db) | ||
$unix_regex = '/^unix\((.+)\)\/?().*$/'; | ||
else | ||
$unix_regex = '/^unix\((.+)\)\/(.+)$/'; | ||
|
||
if (preg_match_all($unix_regex, $socket_database, $matches) > 0) | ||
{ | ||
$info->host = $matches[1][0]; | ||
$info->database = $matches[2][0]; | ||
} | ||
} elseif (substr($info->host, 0, 8) == 'windows(') | ||
{ | ||
$info->host = urldecode(substr($info->host, 8) . '/' . substr($info->database, 0, -1)); | ||
$info->database = null; | ||
} | ||
|
||
if ($allow_blank_db && $info->database) | ||
$info->host .= '/' . $info->database; | ||
|
||
if (isset($url['port'])) | ||
$info->port = $url['port']; | ||
|
||
if (strpos($connection_url, 'decode=true') !== false) | ||
{ | ||
if ($info->username) | ||
$info->username = urldecode($info->username); | ||
|
||
if ($info->password) | ||
$info->password = urldecode($info->password); | ||
} | ||
|
||
if (isset($url['query'])) | ||
{ | ||
|
||
foreach (explode('/&/', $url['query']) as $pair) { | ||
list($name, $value) = explode('=', $pair); | ||
|
||
if ($name == 'charset') | ||
$info->charset = $value; | ||
} | ||
} | ||
|
||
return $info; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
use ActiveRecord\ConnectionInfo; | ||
|
||
class ConnectionInfoTest extends SnakeCase_PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @expectedException ActiveRecord\DatabaseException | ||
*/ | ||
public function test_connection_info_from_should_throw_exception_when_no_host() | ||
{ | ||
ConnectionInfo::from_connection_url('mysql://user:pass@'); | ||
} | ||
|
||
public function test_connection_info() | ||
{ | ||
$info = ConnectionInfo::from_connection_url('mysql://user:pass@127.0.0.1:3306/dbname'); | ||
$this->assert_equals('mysql',$info->protocol); | ||
$this->assert_equals('user',$info->username); | ||
$this->assert_equals('pass',$info->password); | ||
$this->assert_equals('127.0.0.1',$info->host); | ||
$this->assert_equals(3306,$info->port); | ||
$this->assert_equals('dbname',$info->database); | ||
} | ||
|
||
public function test_gh_103_sqlite_connection_string_relative() | ||
{ | ||
$info = ConnectionInfo::from_connection_url('sqlite://../some/path/to/file.db'); | ||
$this->assert_equals('../some/path/to/file.db', $info->host); | ||
} | ||
|
||
/** | ||
* @expectedException ActiveRecord\DatabaseException | ||
*/ | ||
public function test_gh_103_sqlite_connection_string_absolute() | ||
{ | ||
$info = ConnectionInfo::from_connection_url('sqlite:///some/path/to/file.db'); | ||
} | ||
|
||
public function test_gh_103_sqlite_connection_string_unix() | ||
{ | ||
$info = ConnectionInfo::from_connection_url('sqlite://unix(/some/path/to/file.db)'); | ||
$this->assert_equals('/some/path/to/file.db', $info->host); | ||
|
||
$info = ConnectionInfo::from_connection_url('sqlite://unix(/some/path/to/file.db)/'); | ||
$this->assert_equals('/some/path/to/file.db', $info->host); | ||
|
||
$info = ConnectionInfo::from_connection_url('sqlite://unix(/some/path/to/file.db)/dummy'); | ||
$this->assert_equals('/some/path/to/file.db', $info->host); | ||
} | ||
|
||
public function test_gh_103_sqlite_connection_string_windows() | ||
{ | ||
$info = ConnectionInfo::from_connection_url('sqlite://windows(c%3A/some/path/to/file.db)'); | ||
$this->assert_equals('c:/some/path/to/file.db', $info->host); | ||
} | ||
|
||
public function test_parse_connection_url_with_unix_sockets() | ||
{ | ||
$info = ConnectionInfo::from_connection_url('mysql://user:password@unix(/tmp/mysql.sock)/database'); | ||
$this->assert_equals('/tmp/mysql.sock',$info->host); | ||
} | ||
|
||
public function test_parse_connection_url_with_decode_option() | ||
{ | ||
$info = ConnectionInfo::from_connection_url('mysql://h%20az:h%40i@127.0.0.1/test?decode=true'); | ||
$this->assert_equals('h az',$info->username); | ||
$this->assert_equals('h@i',$info->password); | ||
} | ||
|
||
public function test_encoding() | ||
{ | ||
$info = ConnectionInfo::from_connection_url('mysql://test:test@127.0.0.1/test?charset=utf8'); | ||
$this->assert_equals('utf8', $info->charset); | ||
} | ||
|
||
public function test_connection_info_from_array(){ | ||
$info = new ConnectionInfo(array( | ||
'protocol' => 'mysql', | ||
'host' => '127.0.0.1', | ||
'port' => 3306, | ||
'database' => 'dbname', | ||
'username' => 'user', | ||
'password' => 'pass' | ||
)); | ||
$this->assert_equals('mysql',$info->protocol); | ||
$this->assert_equals('user',$info->username); | ||
$this->assert_equals('pass',$info->password); | ||
$this->assert_equals('127.0.0.1',$info->host); | ||
$this->assert_equals(3306,$info->port); | ||
$this->assert_equals('dbname',$info->database); | ||
} | ||
|
||
} |
Oops, something went wrong.