Pārlūkot izejas kodu

Implement an option to specify which cluster class to use.

Daniele Alessandri 14 gadi atpakaļ
vecāks
revīzija
4af1b7744c

+ 2 - 1
lib/Predis/Client.php

@@ -46,7 +46,8 @@ class Client {
         }
         if (is_array($parameters)) {
             if (isset($parameters[0])) {
-                $cluster = new ConnectionCluster($this->_options->key_distribution);
+                $clusterClass = $this->_options->cluster;
+                $cluster = new $clusterClass($this->_options->key_distribution);
                 foreach ($parameters as $single) {
                     $cluster->add($single instanceof IConnectionSingle
                         ? $single : $this->createConnection($single)

+ 2 - 0
lib/Predis/ClientOptions.php

@@ -4,6 +4,7 @@ namespace Predis;
 
 use Predis\Options\IOption;
 use Predis\Options\ClientProfile;
+use Predis\Options\ClientClusterType;
 use Predis\Options\ClientKeyDistribution;
 use Predis\Options\ClientConnectionFactory;
 
@@ -23,6 +24,7 @@ class ClientOptions {
             'profile' => new ClientProfile(),
             'key_distribution' => new ClientKeyDistribution(),
             'connections' => new ClientConnectionFactory(),
+            'cluster' => new ClientClusterType(),
         );
         return self::$_sharedOptions;
     }

+ 33 - 0
lib/Predis/Options/ClientClusterType.php

@@ -0,0 +1,33 @@
+<?php
+
+namespace Predis\Options;
+
+use Predis\ClientException;
+
+class ClientClusterType extends Option {
+    const CLUSTER_INTERFACE = '\Predis\Network\IConnectionCluster';
+    const CLUSTER_PREDIS = '\Predis\Network\ConnectionCluster';
+
+    public function validate($value) {
+        switch ($value) {
+            case 'client':
+                return self::CLUSTER_PREDIS;
+            default:
+                return $this->checkClass($value);
+        }
+    }
+
+    private function checkClass($class) {
+        $reflection = new \ReflectionClass($class);
+        if (!$reflection->isSubclassOf(self::CLUSTER_INTERFACE)) {
+            throw new ClientException(
+                "The class $class is not a valid cluster connection"
+            );
+        }
+        return $class;
+    }
+
+    public function getDefault() {
+        return self::CLUSTER_PREDIS;
+    }
+}