Forráskód Böngészése

Merge branch 'command_preprocessors'

Daniele Alessandri 14 éve
szülő
commit
0396320951

+ 31 - 0
examples/KeyPrefixes.php

@@ -0,0 +1,31 @@
+<?php
+require_once 'SharedConfigurations.php';
+
+// Predis ships with a KeyPrefixPreprocessor class that is used to transparently
+// prefix each key before sending commands to Redis, even for complex commands
+// such as SORT, ZUNIONSTORE and ZINTERSTORE. Key prefixes are useful to create
+// user-level namespaces for you keyspace, thus eliminating the need for separate
+// logical databases.
+
+use Predis\Commands\Preprocessors\KeyPrefixPreprocessor;
+
+$client = new Predis\Client();
+$client->getProfile()->setPreprocessor(new KeyPrefixPreprocessor('nrk:'));
+
+$client->mset(array('foo' => 'bar', 'lol' => 'wut'));
+var_dump($client->mget('foo', 'lol'));
+/*
+array(2) {
+  [0]=> string(3) "bar"
+  [1]=> string(3) "wut"
+}
+*/
+
+var_dump($client->keys('*'));
+/*
+array(2) {
+  [0]=> string(7) "nrk:foo"
+  [1]=> string(7) "nrk:lol"
+}
+*/
+?>

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

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

+ 11 - 0
lib/Predis/Commands/Preprocessors/ICommandPreprocessorChain.php

@@ -0,0 +1,11 @@
+<?php
+
+namespace Predis\Commands\Preprocessors;
+
+interface ICommandPreprocessorChain
+    extends ICommandPreprocessor, \IteratorAggregate, \Countable {
+
+    public function add(ICommandPreprocessor $preprocessor);
+    public function remove(ICommandPreprocessor $preprocessor);
+    public function getPreprocessors();
+}

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

+ 177 - 0
lib/Predis/Commands/Preprocessors/KeyPrefixPreprocessor.php

@@ -0,0 +1,177 @@
+<?php
+
+namespace Predis\Commands\Preprocessors;
+
+use Predis\ClientException;
+use Predis\Profiles\IServerProfile;
+
+class KeyPrefixPreprocessor implements ICommandPreprocessor {
+    private $_prefix;
+    private $_strategies;
+
+    public function __construct($prefix, IServerProfile $profile = null) {
+        if (isset($profile)) {
+            $this->checkProfile($profile);
+        }
+        $this->_prefix = $prefix;
+        $this->_strategies = $this->getPrefixStrategies();
+    }
+
+    protected function checkProfile(IServerProfile $profile) {
+        if (!in_array($profile, $this->getSupportedProfiles())) {
+            throw new ClientException("Unsupported profile: $profile");
+        }
+    }
+
+    protected function getSupportedProfiles() {
+        return array('1.2', '2.0', '2.2');
+    }
+
+    protected function getPrefixStrategies() {
+        $singleKey = function(&$arguments, $prefix) {
+            $arguments[0] = "$prefix{$arguments[0]}";
+        };
+
+        $multiKeys = function(&$arguments, $prefix) {
+            if (count($arguments) === 1 && is_array($arguments[0])) {
+                $arguments = &$arguments[0];
+            }
+            foreach ($arguments as &$key) {
+                $key = "$prefix$key";
+            }
+        };
+
+        $skipLast = function(&$arguments, $prefix) {
+            $length = count($arguments);
+            for ($i = 0; $i < $length - 1; $i++) {
+                $arguments[$i] = "$prefix{$arguments[$i]}";
+            }
+        };
+
+        $interleavedKeys = function(&$arguments, $prefix) {
+            $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]}";
+                }
+            }
+        };
+
+        $zunionstore = function(&$arguments, $prefix) {
+            $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]}";
+                }
+            }
+        };
+
+        $sort = function(&$arguments, $prefix) {
+            $arguments[0] = "$prefix{$arguments[0]}";
+            if (count($arguments) === 1) {
+                return;
+            }
+            foreach ($arguments[1] as $modifier => &$value) {
+                switch (strtoupper($modifier)) {
+                    case 'BY':
+                    case 'STORE':
+                        $value = "$prefix$value";
+                        break;
+                    case 'GET':
+                        if (is_array($value)) {
+                            foreach ($value as &$getItem) {
+                                $getItem = "$prefix$getItem";
+                            }
+                        }
+                        else {
+                            $value = "$prefix$value";
+                        }
+                        break;
+                }
+            }
+        };
+
+        $debug = function(&$arguments, $prefix) {
+            if (count($arguments) === 3 && strtoupper($arguments[1]) == 'OBJECT') {
+                $arguments[2] = "$prefix{$arguments[2]}";
+            }
+        };
+
+        $cmdSingleKey = array(
+            'type', 'exists', 'move', 'expire', 'persist', 'sort', 'expireat', 'ttl', 'append',
+            'getrange', 'setnx', 'decr', 'getset', 'setrange', 'decrby', 'incr', 'set', 'strlen',
+            'get', 'incrby', 'setbit', 'getbit', 'setex', 'hdel', 'hgetall', 'hlen', 'hset',
+            'hexists', 'hincrby', 'hmget', 'hsetnx', 'hget', 'hkeys', 'hmset', 'hvals', 'lindex', 
+            'linsert', 'llen', 'lpop', 'lpush', 'lpushx', 'rpushx', 'lrange', 'lrem', 'lset',
+            'ltrim', 'rpop', 'rpush', 'rpushx', 'sadd', 'scard', 'sismember', 'smembers', 'spop', 
+            'srandmember', 'srem', 'zadd', 'zcard', 'zcount', 'zincrby', 'zrange', 'zrangebyscore', 
+            'zrank', 'zrem', 'zremrangebyrank', 'zremrangebyscore', 'zrevrange', 'zrevrangebyscore', 
+            'zrevrank', 'zscore', 'publish', 'keys',
+        );
+        $cmdMultiKeys = array(
+            'del', 'rename', 'renamenx', 'mget', 'rpoplpush', 'sdiff', 'sdiffstore', 'sinter', 
+            'sinterstore', 'sunion', 'sunionstore', 'subscribe', 'punsubscribe', 'subscribe', 
+            'unsubscribe', 'watch', 
+        );
+
+        return array_merge(
+            array_fill_keys($cmdSingleKey, $singleKey),
+            array_fill_keys($cmdMultiKeys, $multiKeys),
+            array(
+                'blpop' => $skipLast, 'blpop' => $skipLast, 'brpoplpush' => $skipLast, 'smove' => $skipLast,
+                'mset' => $interleavedKeys, 'msetnx' => $interleavedKeys,
+                'zinterstore' => $zunionstore, 'zunionstore' => $zunionstore,
+                'sort' => $sort, 'debug' => $debug
+            )
+        );
+    }
+
+    public function setPrefixStrategy($command, $strategy) {
+        if (!is_callable($callable)) {
+            throw new \InvalidArgumentException(
+                'The command preprocessor strategy must be a callable object'
+            );
+        }
+        $this->_strategies[$command] = $strategy;
+    }
+
+    public function getPrefixStrategy($command) {
+        if (isset($this->_strategies[$command])) {
+            return $this->_strategies[$command];
+        }
+    }
+
+    public function setPrefix($prefix) {
+        $this->_prefix = $prefix;
+    }
+
+    public function getPrefix() {
+        return $this->_prefix;
+    }
+
+    public function process($method, &$arguments) {
+        if (isset($this->_strategies[$method])) {
+            $this->_strategies[$method]($arguments, $this->_prefix);
+        }
+    }
+}

+ 65 - 0
lib/Predis/Commands/Preprocessors/PreprocessorChain.php

@@ -0,0 +1,65 @@
+<?php
+
+namespace Predis\Commands\Preprocessors;
+
+class PreprocessorChain implements ICommandPreprocessorChain, \ArrayAccess {
+    private $_preprocessors;
+
+    public function __construct($preprocessors = array()) {
+        foreach ($preprocessors as $preprocessor) {
+            $this->add($preprocessor);
+        }
+    }
+
+    public function add(ICommandPreprocessor $preprocessor) {
+        $this->_preprocessors[] = $preprocessor;
+    }
+
+    public function remove(ICommandPreprocessor $preprocessor) {
+        $index = array_search($preprocessor, $this->_preprocessors, true);
+        if ($index !== false) {
+            unset($this->_preprocessors);
+        }
+    }
+
+    public function process($method, &$arguments) {
+        $count = count($this->_preprocessors);
+        for ($i = 0; $i < $count; $i++) {
+            $this->_preprocessors[$i]->process($method, $arguments);
+        }
+    }
+
+    public function getPreprocessors() {
+        return $this->_preprocessors;
+    }
+
+    public function getIterator() {
+        return new \ArrayIterator($this->_preprocessors);
+    }
+
+    public function count() {
+        return count($this->_preprocessors);
+    }
+
+    public function offsetExists($index) {
+        return isset($this->_preprocessors[$index]);
+    }
+
+    public function offsetGet($index) {
+        return $this->_preprocessors[$index];
+    }
+
+    public function offsetSet($index, $preprocessor) {
+        if (!$preprocessor instanceof ICommandPreprocessor) {
+            throw new \InvalidArgumentException(
+                'A preprocessor chain can hold only instances of classes implementing '.
+                'the Predis\Commands\Preprocessors\ICommandPreprocessor interface'
+            );
+        }
+        $this->_preprocessors[$index] = $preprocessor;
+    }
+
+    public function offsetUnset($index) {
+        unset($this->_preprocessors[$index]);
+    }
+}

+ 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();
@@ -73,6 +76,9 @@ abstract class ServerProfile implements IServerProfile {
         if (!isset($this->_registeredCommands[$method])) {
             throw new ClientException("'$method' is not a registered Redis command");
         }
+        if (isset($this->_preprocessor)) {
+            $this->_preprocessor->process($method, $arguments);
+        }
         $commandClass = $this->_registeredCommands[$method];
         $command = new $commandClass();
         $command->setArgumentsArray($arguments);
@@ -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();
     }