mirror of
https://github.com/predis/predis.git
synced 2026-09-17 22:07:04 +00:00
Accept instances of Predis\IConnection or Predis\ConnectionParameters as parameters for the constructor of Predis\Client.
This commit is contained in:
+16
-6
@@ -61,7 +61,6 @@ class Client {
|
||||
private function setupClient($options) {
|
||||
$this->_responseReader = new ResponseReader();
|
||||
$this->_options = self::filterClientOptions($options);
|
||||
|
||||
$this->setProfile($this->_options->profile);
|
||||
if ($this->_options->iterable_multibulk === true) {
|
||||
$this->_responseReader->setHandler(
|
||||
@@ -78,10 +77,15 @@ class Client {
|
||||
}
|
||||
|
||||
private function setupConnection($parameters) {
|
||||
if ($parameters !== null && !(is_array($parameters) || is_string($parameters))) {
|
||||
throw new ClientException('Invalid parameters type (array or string expected)');
|
||||
if ($parameters === null) {
|
||||
return $this->setConnection($this->createConnection(null));
|
||||
}
|
||||
if (!(is_array($parameters) || is_string($parameters) || $parameters instanceof IConnection
|
||||
|| $parameters instanceof ConnectionParameters)) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Array, String, Predis\ConnectionParameters or Predis\IConnection expected'
|
||||
);
|
||||
}
|
||||
|
||||
if (is_array($parameters) && isset($parameters[0])) {
|
||||
$cluster = new ConnectionCluster($this->_options->key_distribution);
|
||||
foreach ($parameters as $shardParams) {
|
||||
@@ -90,11 +94,17 @@ class Client {
|
||||
$this->setConnection($cluster);
|
||||
}
|
||||
else {
|
||||
$this->setConnection($this->createConnection($parameters));
|
||||
$this->setConnection($parameters instanceof IConnection
|
||||
? $parameters
|
||||
: $this->createConnection($parameters)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private function createConnection($parameters) {
|
||||
if ($parameters instanceof IConnectionSingle) {
|
||||
return $parameters;
|
||||
}
|
||||
$params = $parameters instanceof ConnectionParameters
|
||||
? $parameters
|
||||
: new ConnectionParameters($parameters);
|
||||
@@ -1090,7 +1100,7 @@ class ConnectionParameters {
|
||||
}
|
||||
|
||||
private static function paramsExtractor($params, $kv) {
|
||||
list($k, $v) = explode('=', $kv);
|
||||
@list($k, $v) = explode('=', $kv);
|
||||
$params[$k] = $v;
|
||||
return $params;
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ class PredisClientFeaturesTestSuite extends PHPUnit_Framework_TestCase {
|
||||
parent::onNotSuccessfulTest($exception);
|
||||
}
|
||||
|
||||
|
||||
/* ConnectionParameters */
|
||||
|
||||
function testConnectionParametersDefaultValues() {
|
||||
@@ -377,6 +376,49 @@ class PredisClientFeaturesTestSuite extends PHPUnit_Framework_TestCase {
|
||||
}
|
||||
|
||||
|
||||
/* Client initialization */
|
||||
|
||||
function testClientInitialization_SingleConnectionParameters() {
|
||||
$params1 = array_merge(RC::getConnectionArguments(), array(
|
||||
'connection_timeout' => 10,
|
||||
'read_write_timeout' => 30,
|
||||
'alias' => 'connection_alias',
|
||||
));
|
||||
$params2 = RC::getConnectionParametersArgumentsString($params1);
|
||||
$params3 = new \Predis\ConnectionParameters($params1);
|
||||
$params4 = new \Predis\TcpConnection($params3);
|
||||
foreach (array($params1, $params2, $params3, $params4) as $params) {
|
||||
$client = new \Predis\Client($params);
|
||||
$parameters = $client->getConnection()->getParameters();
|
||||
$this->assertEquals($params1['host'], $parameters->host);
|
||||
$this->assertEquals($params1['port'], $parameters->port);
|
||||
$this->assertEquals($params1['connection_timeout'], $parameters->connection_timeout);
|
||||
$this->assertEquals($params1['read_write_timeout'], $parameters->read_write_timeout);
|
||||
$this->assertEquals($params1['alias'], $parameters->alias);
|
||||
$this->assertNull($parameters->password);
|
||||
}
|
||||
}
|
||||
|
||||
function testClientInitialization_ClusterConnectionParameters() {
|
||||
$params1 = array_merge(RC::getConnectionArguments(), array(
|
||||
'connection_timeout' => 10,
|
||||
'read_write_timeout' => 30,
|
||||
));
|
||||
$params2 = RC::getConnectionParametersArgumentsString($params1);
|
||||
$params3 = new \Predis\ConnectionParameters($params1);
|
||||
$params4 = new \Predis\TcpConnection($params3);
|
||||
|
||||
$client = new \Predis\Client(array($params1, $params2, $params3, $params4));
|
||||
foreach ($client->getConnection() as $connection) {
|
||||
$parameters = $connection->getParameters();
|
||||
$this->assertEquals($params1['host'], $parameters->host);
|
||||
$this->assertEquals($params1['port'], $parameters->port);
|
||||
$this->assertEquals($params1['connection_timeout'], $parameters->connection_timeout);
|
||||
$this->assertEquals($params1['read_write_timeout'], $parameters->read_write_timeout);
|
||||
$this->assertNull($parameters->password);
|
||||
}
|
||||
}
|
||||
|
||||
/* Client + CommandPipeline */
|
||||
|
||||
function testCommandPipeline_Simple() {
|
||||
|
||||
@@ -190,9 +190,12 @@ class RC {
|
||||
public static function getConnectionParametersArgumentsString($arguments = null) {
|
||||
// TODO: must be improved
|
||||
$args = $arguments ?: RC::getConnectionParametersArgumentsArray();
|
||||
$paramsString = "redis://{$args['host']}:{$args['port']}/";
|
||||
$paramsString .= "?connection_timeout={$args['connection_timeout']}&read_write_timeout={$args['read_write_timeout']}";
|
||||
$paramsString .= "&database={$args['database']}&password={$args['password']}&alias={$args['alias']}";
|
||||
$paramsString = "redis://{$args['host']}:{$args['port']}/?";
|
||||
unset($args['host']);
|
||||
unset($args['port']);
|
||||
foreach($args as $k => $v) {
|
||||
$paramsString .= "$k=$v&";
|
||||
}
|
||||
return $paramsString;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user