Quellcode durchsuchen

Make command processors accept only instances of Predis\Commands\ICommand.

Daniele Alessandri vor 14 Jahren
Ursprung
Commit
b5ebf48318

+ 3 - 1
lib/Predis/Commands/Processors/ICommandProcessor.php

@@ -2,6 +2,8 @@
 
 
 namespace Predis\Commands\Processors;
 namespace Predis\Commands\Processors;
 
 
+use Predis\Commands\ICommand;
+
 interface ICommandProcessor {
 interface ICommandProcessor {
-    public function process($method, &$arguments);
+    public function process(ICommand $command);
 }
 }

+ 10 - 32
lib/Predis/Commands/Processors/KeyPrefixProcessor.php

@@ -3,6 +3,7 @@
 namespace Predis\Commands\Processors;
 namespace Predis\Commands\Processors;
 
 
 use Predis\ClientException;
 use Predis\ClientException;
+use Predis\Commands\ICommand;
 use Predis\Profiles\IServerProfile;
 use Predis\Profiles\IServerProfile;
 
 
 class KeyPrefixProcessor implements ICommandProcessor {
 class KeyPrefixProcessor implements ICommandProcessor {
@@ -37,39 +38,16 @@ class KeyPrefixProcessor implements ICommandProcessor {
 
 
         $interleavedKeys = function(&$arguments, $prefix) {
         $interleavedKeys = function(&$arguments, $prefix) {
             $length = count($arguments);
             $length = count($arguments);
-            if ($length === 1 && is_array($arguments[0])) {
-                $oldKvs = &$arguments[0];
-                $newKvs = array();
-                foreach ($oldKvs as $key => $value) {
-                    $newKvs["$prefix$key"] = $value;
-                    unset($oldKvs[$key]);
-                }
-                $arguments[0] = $newKvs;
-            }
-            else {
-                for ($i = 0; $i < $length; $i += 2) {
-                    $arguments[$i] = "$prefix{$arguments[$i]}";
-                }
+            for ($i = 0; $i < $length; $i += 2) {
+                $arguments[$i] = "$prefix{$arguments[$i]}";
             }
             }
         };
         };
 
 
         $zunionstore = function(&$arguments, $prefix) {
         $zunionstore = function(&$arguments, $prefix) {
             $arguments[0] = "$prefix{$arguments[0]}";
             $arguments[0] = "$prefix{$arguments[0]}";
-            if (is_array($arguments[1])) {
-                foreach ($arguments[1] as &$destinationKey) {
-                    $destinationKey = "$prefix$destinationKey";
-                }
-                $args = &$arguments[1];
-                $length = count($args);
-                for ($i = 0; $i < $length; $i++) {
-                    $arguments[1][$i] = "$prefix{$args[$i]}";
-                }
-            }
-            else {
-                $length = (int)$arguments[1];
-                for ($i = 2; $i < $length; $i++) {
-                    $arguments[$i] = "$prefix{$arguments[$i]}";
-                }
+            $length = ((int) $arguments[1]) + 2;
+            for ($i = 2; $i < $length; $i++) {
+                $arguments[$i] = "$prefix{$arguments[$i]}";
             }
             }
         };
         };
 
 
@@ -150,9 +128,6 @@ class KeyPrefixProcessor implements ICommandProcessor {
 
 
     public function getMultipleKeysStrategy() {
     public function getMultipleKeysStrategy() {
         return function(&$arguments, $prefix) {
         return function(&$arguments, $prefix) {
-            if (count($arguments) === 1 && is_array($arguments[0])) {
-                $arguments = &$arguments[0];
-            }
             foreach ($arguments as &$key) {
             foreach ($arguments as &$key) {
                 $key = "$prefix$key";
                 $key = "$prefix$key";
             }
             }
@@ -173,9 +148,12 @@ class KeyPrefixProcessor implements ICommandProcessor {
         return $this->_prefix;
         return $this->_prefix;
     }
     }
 
 
-    public function process($method, &$arguments) {
+    public function process(ICommand $command) {
+        $method = strtolower($command->getId());
         if (isset($this->_strategies[$method])) {
         if (isset($this->_strategies[$method])) {
+            $arguments = $command->getArguments();
             $this->_strategies[$method]($arguments, $this->_prefix);
             $this->_strategies[$method]($arguments, $this->_prefix);
+            $command->setArguments($arguments);
         }
         }
     }
     }
 }
 }

+ 4 - 2
lib/Predis/Commands/Processors/ProcessorChain.php

@@ -2,6 +2,8 @@
 
 
 namespace Predis\Commands\Processors;
 namespace Predis\Commands\Processors;
 
 
+use Predis\Commands\ICommand;
+
 class ProcessorChain implements ICommandProcessorChain, \ArrayAccess {
 class ProcessorChain implements ICommandProcessorChain, \ArrayAccess {
     private $_processors;
     private $_processors;
 
 
@@ -22,10 +24,10 @@ class ProcessorChain implements ICommandProcessorChain, \ArrayAccess {
         }
         }
     }
     }
 
 
-    public function process($method, &$arguments) {
+    public function process(ICommand $command) {
         $count = count($this->_processors);
         $count = count($this->_processors);
         for ($i = 0; $i < $count; $i++) {
         for ($i = 0; $i < $count; $i++) {
-            $this->_processors[$i]->process($method, $arguments);
+            $this->_processors[$i]->process($command);
         }
         }
     }
     }
 
 

+ 3 - 3
lib/Predis/Profiles/ServerProfile.php

@@ -76,12 +76,12 @@ abstract class ServerProfile implements IServerProfile, IProcessingSupport {
         if (!isset($this->_registeredCommands[$method])) {
         if (!isset($this->_registeredCommands[$method])) {
             throw new ClientException("'$method' is not a registered Redis command");
             throw new ClientException("'$method' is not a registered Redis command");
         }
         }
-        if (isset($this->_processor)) {
-            $this->_processor->process($method, $arguments);
-        }
         $commandClass = $this->_registeredCommands[$method];
         $commandClass = $this->_registeredCommands[$method];
         $command = new $commandClass();
         $command = new $commandClass();
         $command->setArguments($arguments);
         $command->setArguments($arguments);
+        if (isset($this->_processor)) {
+            $this->_processor->process($command);
+        }
         return $command;
         return $command;
     }
     }