Selaa lähdekoodia

Added the new abstract class 'RedisServerCompatibility'. Its subclassess will be used to define a set compatibility profiles for each version of the Redis server. The only drawback is that the selected compatibility profile will be shared among multiple connections when using client-side sharding.

Daniele Alessandri 15 vuotta sitten
vanhempi
commit
a70c5b0800
1 muutettua tiedostoa jossa 50 lisäystä ja 0 poistoa
  1. 50 0
      lib/Predis.php

+ 50 - 0
lib/Predis.php

@@ -789,6 +789,56 @@ class ConnectionCluster implements IConnection  {
 
 /* ------------------------------------------------------------------------- */
 
+abstract class RedisServerCompatibility { 
+    private $_registeredCommands;
+
+    public function __construct() {
+        $this->_registeredCommands = $this->getSupportedCommands();
+    }
+
+    public abstract function getVersion();
+
+    protected abstract function getSupportedCommands();
+
+    public function createCommandInstance($method, $arguments) {
+        $commandClass = $this->_registeredCommands[$method];
+
+        if ($commandClass === null) {
+            throw new ClientException("'$method' is not a registered Redis command");
+        }
+
+        $command = new $commandClass();
+        $command->setArgumentsArray($arguments);
+        return $command;
+    }
+
+
+    public function registerCommands(Array $commands) {
+        foreach ($commands as $command => $aliases) {
+            $this->registerCommand($command, $aliases);
+        }
+    }
+
+    public function registerCommand($command, $aliases) {
+        $commandReflection = new \ReflectionClass($command);
+
+        if (!$commandReflection->isSubclassOf('\Predis\Command')) {
+            throw new ClientException("Cannot register '$command' as it is not a valid Redis command");
+        }
+
+        if (is_array($aliases)) {
+            foreach ($aliases as $alias) {
+                $this->_registeredCommands[$alias] = $command;
+            }
+        }
+        else {
+            $this->_registeredCommands[$aliases] = $command;
+        }
+    }
+}
+
+/* ------------------------------------------------------------------------- */
+
 namespace Predis\Utilities;
 
 class HashRing {