Browse Source

Add an abstraction to optionally preprocess command arguments.

Daniele Alessandri 14 years ago
parent
commit
07b4238ce7

+ 7 - 0
lib/Predis/Commands/Preprocessors/ICommandPreprocessor.php

@@ -0,0 +1,7 @@
+<?php
+
+namespace Predis\Commands\Preprocessors;
+
+interface ICommandPreprocessor {
+    public function process(&$method, &$arguments);
+}

+ 8 - 0
lib/Predis/Commands/Preprocessors/IPreprocessingSupport.php

@@ -0,0 +1,8 @@
+<?php
+
+namespace Predis\Commands\Preprocessors;
+
+interface IPreprocessingSupport {
+    public function setPreprocessor(ICommandPreprocessor $processor);
+    public function getPreprocessor();
+}

+ 19 - 1
lib/Predis/Profiles/ServerProfile.php

@@ -3,10 +3,13 @@
 namespace Predis\Profiles;
 
 use Predis\ClientException;
+use Predis\Commands\Preprocessors\ICommandPreprocessor;
+use Predis\Commands\Preprocessors\IPreprocessingSupport;
 
-abstract class ServerProfile implements IServerProfile {
+abstract class ServerProfile implements IServerProfile, IPreprocessingSupport {
     private static $_profiles;
     private $_registeredCommands;
+    private $_preprocessor;
 
     public function __construct() {
         $this->_registeredCommands = $this->getSupportedCommands();
@@ -70,6 +73,9 @@ abstract class ServerProfile implements IServerProfile {
     }
 
     public function createCommand($method, $arguments = array()) {
+        if (isset($this->_preprocessor)) {
+            $this->_preprocessor->process($method, $arguments);
+        }
         if (!isset($this->_registeredCommands[$method])) {
             throw new ClientException("'$method' is not a registered Redis command");
         }
@@ -93,6 +99,18 @@ abstract class ServerProfile implements IServerProfile {
         $this->_registeredCommands[$alias] = $command;
     }
 
+    public function setPreprocessor(ICommandPreprocessor $preprocessor) {
+        if (!isset($preprocessor)) {
+            unset($this->_preprocessor);
+            return;
+        }
+        $this->_preprocessor = $preprocessor;
+    }
+
+    public function getPreprocessor() {
+        return $this->_preprocessor;
+    }
+
     public function __toString() {
         return $this->getVersion();
     }