Просмотр исходного кода

Make cluster and replication classes extend from a common interface.

Daniele Alessandri 13 лет назад
Родитель
Сommit
b67af29eb6

+ 3 - 0
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`

+ 1 - 3
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);

+ 55 - 0
lib/Predis/Connection/AggregatedConnectionInterface.php

@@ -0,0 +1,55 @@
+<?php
+
+/*
+ * This file is part of the Predis package.
+ *
+ * (c) Daniele Alessandri <suppakilla@gmail.com>
+ *
+ * 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 <suppakilla@gmail.com>
+ */
+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);
+}

+ 1 - 31
lib/Predis/Connection/ClusterConnectionInterface.php

@@ -19,36 +19,6 @@ use Predis\Command\CommandInterface;
  *
  * @author Daniele Alessandri <suppakilla@gmail.com>
  */
-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);
 }

+ 1 - 32
lib/Predis/Connection/ReplicationConnectionInterface.php

@@ -18,39 +18,8 @@ use Predis\Command\CommandInterface;
  *
  * @author Daniele Alessandri <suppakilla@gmail.com>
  */
-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.
      *

+ 4 - 17
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;
     }
 
     /**

+ 5 - 15
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);
 }

+ 2 - 2
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;
     }
 
     /**

+ 2 - 2
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);
 

+ 8 - 30
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);
     }