From bdbbe18e6ce0d1b38b51eb123ef9ebb38d8c346d Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Sun, 18 Dec 2011 15:07:21 +0100 Subject: [PATCH] Add the ability to get a connection by alias from aggregated connections. Previously it was possible to create a new instance of Predis\Client using the alias of a single connection in a cluster of connections. Now we added the ability to do this also when using master/slave replication. --- lib/Predis/Client.php | 4 ++-- lib/Predis/Helpers.php | 12 ++++++++++++ tests/Predis/ClientTest.php | 2 +- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/Predis/Client.php b/lib/Predis/Client.php index c09a3461..6edd9bac 100644 --- a/lib/Predis/Client.php +++ b/lib/Predis/Client.php @@ -196,8 +196,8 @@ class Client public function getConnection($id = null) { if (isset($id)) { - if (!Helpers::isCluster($this->connection)) { - $message = 'Retrieving connections by alias is supported only with clustered connections'; + if (!Helpers::isAggregated($this->connection)) { + $message = 'Retrieving connections by alias is supported only with aggregated connections (cluster or replication)'; throw new NotSupportedException($message); } return $this->connection->getConnectionById($id); diff --git a/lib/Predis/Helpers.php b/lib/Predis/Helpers.php index a841b0b0..3038f129 100644 --- a/lib/Predis/Helpers.php +++ b/lib/Predis/Helpers.php @@ -13,6 +13,7 @@ namespace Predis; use Predis\Network\IConnection; use Predis\Network\IConnectionCluster; +use Predis\Network\IConnectionReplication; /** * Defines a few helper methods. @@ -21,6 +22,17 @@ use Predis\Network\IConnectionCluster; */ class Helpers { + /** + * Checks if the specified connection represents an aggregation of connections. + * + * @param IConnection $connection Connection object. + * @return Boolean + */ + public static function isAggregated(IConnection $connection) + { + return $connection instanceof IConnectionCluster || $connection instanceof IConnectionReplication; + } + /** * Checks if the specified connection represents a cluster. * diff --git a/tests/Predis/ClientTest.php b/tests/Predis/ClientTest.php index 6628e715..22489d8f 100644 --- a/tests/Predis/ClientTest.php +++ b/tests/Predis/ClientTest.php @@ -385,7 +385,7 @@ class ClientTest extends StandardTestCase /** * @group disconnected * @expectedException Predis\NotSupportedException - * @expectedExceptionMessage Retrieving connections by alias is supported only with clustered connections + * @expectedExceptionMessage Retrieving connections by alias is supported only with aggregated connections (cluster or replication) */ public function testGetConnectionWithAliasWorksOnlyWithCluster() {