diff --git a/CHANGELOG.md b/CHANGELOG.md index 9fb261b8..767f3823 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ v0.8.0 (201x-xx-xx) =============================================================================== +- Cluster and replication connections now extend a new common interface, + `Predis\Connection\AggregatedConnectionInterface`. + - Some namespaces have been renamed: - `Predis\Network` => `Predis\Connection` diff --git a/lib/Predis/Client.php b/lib/Predis/Client.php index 8a7e335b..e6259a84 100644 --- a/lib/Predis/Client.php +++ b/lib/Predis/Client.php @@ -93,11 +93,9 @@ class Client if (is_array($parameters) && isset($parameters[0])) { $replication = isset($this->options->replication) && $this->options->replication; - $connection = $this->options->{$replication ? 'replication' : 'cluster'}; - $initializer = $replication ? 'createReplication' : 'createCluster'; - return $this->connections->$initializer($connection, $parameters, $this->profile); + return $this->connections->createAggregated($connection, $parameters, $this->profile); } return $this->connections->create($parameters, $this->profile); diff --git a/lib/Predis/Connection/AggregatedConnectionInterface.php b/lib/Predis/Connection/AggregatedConnectionInterface.php new file mode 100644 index 00000000..bd1065c0 --- /dev/null +++ b/lib/Predis/Connection/AggregatedConnectionInterface.php @@ -0,0 +1,55 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Predis\Connection; + +use Predis\Command\CommandInterface; + +/** + * Defines a virtual connection composed by multiple connection objects. + * + * @author Daniele Alessandri + */ +interface AggregatedConnectionInterface extends ConnectionInterface +{ + /** + * Adds a connection instance to the aggregated connection. + * + * @param SingleConnectionInterface $connection Instance of a connection. + */ + public function add(SingleConnectionInterface $connection); + + /** + * Removes the specified connection instance from the aggregated + * connection. + * + * @param SingleConnectionInterface $connection Instance of a connection. + * @return Boolean Returns true if the connection was in the pool. + */ + public function remove(SingleConnectionInterface $connection); + + /** + * Gets the actual connection instance in charge of the specified command. + * + * @param CommandInterface $command Instance of a Redis command. + * @return SingleConnectionInterface + */ + public function getConnection(CommandInterface $command); + + /** + * Retrieves a connection instance from the aggregated connection + * using an alias. + * + * @param string $connectionId Alias of a connection + * @return SingleConnectionInterface + */ + public function getConnectionById($connectionId); +} diff --git a/lib/Predis/Connection/ClusterConnectionInterface.php b/lib/Predis/Connection/ClusterConnectionInterface.php index 5f184aba..0cf46260 100644 --- a/lib/Predis/Connection/ClusterConnectionInterface.php +++ b/lib/Predis/Connection/ClusterConnectionInterface.php @@ -19,36 +19,6 @@ use Predis\Command\CommandInterface; * * @author Daniele Alessandri */ -interface ClusterConnectionInterface extends ConnectionInterface +interface ClusterConnectionInterface extends AggregatedConnectionInterface { - /** - * Adds a connection instance to the cluster. - * - * @param SingleConnectionInterface $connection Instance of a connection. - */ - public function add(SingleConnectionInterface $connection); - - /** - * Removes the specified connection instance from the cluster. - * - * @param SingleConnectionInterface $connection Instance of a connection. - * @return Boolean Returns true if the connection was in the pool. - */ - public function remove(SingleConnectionInterface $connection); - - /** - * Gets the actual connection instance in charge of the specified command. - * - * @param CommandInterface $command Instance of a Redis command. - * @return SingleConnectionInterface - */ - public function getConnection(CommandInterface $command); - - /** - * Retrieves a connection instance from the cluster using an alias. - * - * @param string $connectionId Alias of a connection - * @return SingleConnectionInterface - */ - public function getConnectionById($connectionId); } diff --git a/lib/Predis/Connection/ReplicationConnectionInterface.php b/lib/Predis/Connection/ReplicationConnectionInterface.php index 0f8b91cd..a98a8112 100644 --- a/lib/Predis/Connection/ReplicationConnectionInterface.php +++ b/lib/Predis/Connection/ReplicationConnectionInterface.php @@ -18,39 +18,8 @@ use Predis\Command\CommandInterface; * * @author Daniele Alessandri */ -interface ReplicationConnectionInterface extends ConnectionInterface +interface ReplicationConnectionInterface extends AggregatedConnectionInterface { - /** - * Adds a connection instance to the cluster. - * - * @param SingleConnectionInterface $connection Instance of a connection. - */ - public function add(SingleConnectionInterface $connection); - - /** - * Removes the specified connection instance from the cluster. - * - * @param SingleConnectionInterface $connection Instance of a connection. - * @return Boolean Returns true if the connection was in the pool. - */ - public function remove(SingleConnectionInterface $connection); - - /** - * Gets the actual connection instance in charge of the specified command. - * - * @param CommandInterface $command Instance of a Redis command. - * @return SingleConnectionInterface - */ - public function getConnection(CommandInterface $command); - - /** - * Retrieves a connection instance from the cluster using an alias. - * - * @param string $connectionId Alias of a connection - * @return SingleConnectionInterface - */ - public function getConnectionById($connectionId); - /** * Switches the internal connection object being used. * diff --git a/lib/Predis/ConnectionFactory.php b/lib/Predis/ConnectionFactory.php index b0ee1969..862bbbf8 100644 --- a/lib/Predis/ConnectionFactory.php +++ b/lib/Predis/ConnectionFactory.php @@ -13,8 +13,7 @@ namespace Predis; use Predis\Profile\ServerProfileInterface; use Predis\Connection\SingleConnectionInterface; -use Predis\Connection\ClusterConnectionInterface; -use Predis\Connection\ReplicationConnectionInterface; +use Predis\Connection\AggregatedConnectionInterface; use Predis\Profile\ServerProfile; /** @@ -126,25 +125,13 @@ class ConnectionFactory implements ConnectionFactoryInterface /** * {@inheritdoc} */ - public function createCluster(ClusterConnectionInterface $cluster, $parameters, ServerProfileInterface $profile = null) + public function createAggregated(AggregatedConnectionInterface $connection, $parameters, ServerProfileInterface $profile = null) { foreach ($parameters as $node) { - $cluster->add($node instanceof SingleConnectionInterface ? $node : $this->create($node, $profile)); + $connection->add($node instanceof SingleConnectionInterface ? $node : $this->create($node, $profile)); } - return $cluster; - } - - /** - * {@inheritdoc} - */ - public function createReplication(ReplicationConnectionInterface $replication, $parameters, ServerProfileInterface $profile = null) - { - foreach ($parameters as $node) { - $replication->add($node instanceof SingleConnectionInterface ? $node : $this->create($node, $profile)); - } - - return $replication; + return $connection; } /** diff --git a/lib/Predis/ConnectionFactoryInterface.php b/lib/Predis/ConnectionFactoryInterface.php index 80efead4..5b5baaa9 100644 --- a/lib/Predis/ConnectionFactoryInterface.php +++ b/lib/Predis/ConnectionFactoryInterface.php @@ -12,8 +12,7 @@ namespace Predis; use Predis\Profile\ServerProfileInterface; -use Predis\Connection\ClusterConnectionInterface; -use Predis\Connection\ReplicationConnectionInterface; +use Predis\Connection\AggregatedConnectionInterface; /** * Interface that must be implemented by classes that provide their own mechanism @@ -47,20 +46,11 @@ interface ConnectionFactoryInterface public function create($parameters, ServerProfileInterface $profile = null); /** - * Prepares a cluster of connection objects. + * Prepares an aggregation of connection objects. * - * @param ClusterConnectionInterface Instance of a connection cluster class. + * @param AggregatedConnectionInterface Instance of an aggregated connection class. * @param array $parameters List of parameters for each connection object. - * @return Predis\Connection\ClusterConnectionInterface + * @return Predis\Connection\AggregatedConnectionInterface */ - public function createCluster(ClusterConnectionInterface $cluster, $parameters, ServerProfileInterface $profile = null); - - /** - * Prepares a master / slave replication configuration. - * - * @param ReplicationConnectionInterface Instance of a connection cluster class. - * @param array $parameters List of parameters for each connection object. - * @return Predis\Connection\ReplicationConnectionInterface - */ - public function createReplication(ReplicationConnectionInterface $replication, $parameters, ServerProfileInterface $profile = null); + public function createAggregated(AggregatedConnectionInterface $cluster, $parameters, ServerProfileInterface $profile = null); } diff --git a/lib/Predis/Helpers.php b/lib/Predis/Helpers.php index b1b2b606..4c07eb3d 100644 --- a/lib/Predis/Helpers.php +++ b/lib/Predis/Helpers.php @@ -13,7 +13,7 @@ namespace Predis; use Predis\Connection\ConnectionInterface; use Predis\Connection\ClusterConnectionInterface; -use Predis\Connection\ReplicationConnectionInterface; +use Predis\Connection\AggregatedConnectionInterface; /** * Defines a few helper methods. @@ -30,7 +30,7 @@ class Helpers */ public static function isAggregated(ConnectionInterface $connection) { - return $connection instanceof ClusterConnectionInterface || $connection instanceof ReplicationConnectionInterface; + return $connection instanceof AggregatedConnectionInterface; } /** diff --git a/tests/Predis/ClientTest.php b/tests/Predis/ClientTest.php index 5b650a1e..2b5829c4 100644 --- a/tests/Predis/ClientTest.php +++ b/tests/Predis/ClientTest.php @@ -172,7 +172,7 @@ class ClientTest extends StandardTestCase $cluster = new PredisCluster(); $factory = new ConnectionFactory(); - $factory->createCluster($cluster, array('tcp://localhost:7000', 'tcp://localhost:7001')); + $factory->createAggregated($cluster, array('tcp://localhost:7000', 'tcp://localhost:7001')); $client = new Client($cluster); @@ -188,7 +188,7 @@ class ClientTest extends StandardTestCase $replication = new MasterSlaveReplication(); $factory = new ConnectionFactory(); - $factory->createReplication($replication, array('tcp://host1?alias=master', 'tcp://host2?alias=slave')); + $factory->createAggregated($replication, array('tcp://host1?alias=master', 'tcp://host2?alias=slave')); $client = new Client($replication); diff --git a/tests/Predis/ConnectionFactoryTest.php b/tests/Predis/ConnectionFactoryTest.php index 09f69e9f..e2c1f065 100644 --- a/tests/Predis/ConnectionFactoryTest.php +++ b/tests/Predis/ConnectionFactoryTest.php @@ -260,7 +260,7 @@ class ConnectionFactoryTest extends StandardTestCase /** * @group disconnected */ - public function testClusterSkipCreationOnConnectionInstance() + public function testAggregatedConnectionSkipCreationOnConnectionInstance() { list(, $connectionClass) = $this->getMockConnectionClass(); @@ -273,13 +273,13 @@ class ConnectionFactoryTest extends StandardTestCase $factory->expects($this->never()) ->method('create'); - $factory->createCluster($cluster, array(new $connectionClass(), new $connectionClass())); + $factory->createAggregated($cluster, array(new $connectionClass(), new $connectionClass())); } /** * @group disconnected */ - public function testClusterWithMixedConnectionParameters() + public function testAggregatedConnectionWithMixedConnectionParameters() { list(, $connectionClass) = $this->getMockConnectionClass(); @@ -295,13 +295,13 @@ class ConnectionFactoryTest extends StandardTestCase return new $connectionClass; })); - $factory->createCluster($cluster, array(null, 'tcp://127.0.0.1', array('scheme' => 'tcp'), new $connectionClass())); + $factory->createAggregated($cluster, array(null, 'tcp://127.0.0.1', array('scheme' => 'tcp'), new $connectionClass())); } /** * @group disconnected */ - public function testClusterWithEmptyListOfParameters() + public function testAggregatedConnectionWithEmptyListOfParameters() { $cluster = $this->getMock('Predis\Connection\ClusterConnectionInterface'); $cluster->expects($this->never())->method('add'); @@ -309,14 +309,14 @@ class ConnectionFactoryTest extends StandardTestCase $factory = $this->getMock('Predis\ConnectionFactory', array('create')); $factory->expects($this->never())->method('create'); - $factory->createCluster($cluster, array()); + $factory->createAggregated($cluster, array()); } /** * @group disconnected * @todo We might want to add a test for SingleConnectionInterface::pushInitCommand(). */ - public function testClusterWithServerProfileArgument() + public function testAggregatedConnectionWithServerProfileArgument() { list(, $connectionClass) = $this->getMockConnectionClass(); @@ -332,29 +332,7 @@ class ConnectionFactoryTest extends StandardTestCase })); $nodes = array('tcp://127.0.0.1:7001?password=foo', 'tcp://127.0.0.1:7002?password=bar'); - $factory->createCluster($cluster, $nodes, $profile); - } - - /** - * @group disconnected - */ - public function testReplicationWithMixedConnectionParameters() - { - list(, $connectionClass) = $this->getMockConnectionClass(); - - $replication = $this->getMock('Predis\Connection\ReplicationConnectionInterface'); - $replication->expects($this->exactly(4)) - ->method('add') - ->with($this->isInstanceOf('Predis\Connection\SingleConnectionInterface')); - - $factory = $this->getMock('Predis\ConnectionFactory', array('create')); - $factory->expects($this->exactly(3)) - ->method('create') - ->will($this->returnCallback(function($_, $_) use($connectionClass) { - return new $connectionClass; - })); - - $factory->createReplication($replication, array(null, 'tcp://127.0.0.1', array('scheme' => 'tcp'), new $connectionClass())); + $factory->createAggregated($cluster, $nodes, $profile); }