KeyPrefixProcessor.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace Predis\Commands\Processors;
  3. use Predis\ClientException;
  4. use Predis\Commands\ICommand;
  5. use Predis\Profiles\IServerProfile;
  6. class KeyPrefixProcessor implements ICommandProcessor {
  7. private $_prefix;
  8. private $_strategies;
  9. public function __construct($prefix, IServerProfile $profile = null) {
  10. if (isset($profile)) {
  11. $this->checkProfile($profile);
  12. }
  13. $this->_prefix = $prefix;
  14. $this->_strategies = $this->getPrefixStrategies();
  15. }
  16. protected function checkProfile(IServerProfile $profile) {
  17. if (!in_array($profile, $this->getSupportedProfiles())) {
  18. throw new ClientException("Unsupported profile: $profile");
  19. }
  20. }
  21. protected function getSupportedProfiles() {
  22. return array('1.2', '2.0', '2.2');
  23. }
  24. protected function getPrefixStrategies() {
  25. $skipLast = function(&$arguments, $prefix) {
  26. $length = count($arguments);
  27. for ($i = 0; $i < $length - 1; $i++) {
  28. $arguments[$i] = "$prefix{$arguments[$i]}";
  29. }
  30. };
  31. $interleavedKeys = function(&$arguments, $prefix) {
  32. $length = count($arguments);
  33. for ($i = 0; $i < $length; $i += 2) {
  34. $arguments[$i] = "$prefix{$arguments[$i]}";
  35. }
  36. };
  37. $zunionstore = function(&$arguments, $prefix) {
  38. $arguments[0] = "$prefix{$arguments[0]}";
  39. $length = ((int) $arguments[1]) + 2;
  40. for ($i = 2; $i < $length; $i++) {
  41. $arguments[$i] = "$prefix{$arguments[$i]}";
  42. }
  43. };
  44. $sort = function(&$arguments, $prefix) {
  45. $arguments[0] = "$prefix{$arguments[0]}";
  46. if (count($arguments) === 1) {
  47. return;
  48. }
  49. foreach ($arguments[1] as $modifier => &$value) {
  50. switch (strtoupper($modifier)) {
  51. case 'BY':
  52. case 'STORE':
  53. $value = "$prefix$value";
  54. break;
  55. case 'GET':
  56. if (is_array($value)) {
  57. foreach ($value as &$getItem) {
  58. $getItem = "$prefix$getItem";
  59. }
  60. }
  61. else {
  62. $value = "$prefix$value";
  63. }
  64. break;
  65. }
  66. }
  67. };
  68. $debug = function(&$arguments, $prefix) {
  69. if (count($arguments) === 3 && strtoupper($arguments[1]) == 'OBJECT') {
  70. $arguments[2] = "$prefix{$arguments[2]}";
  71. }
  72. };
  73. $cmdSingleKey = array(
  74. 'type', 'exists', 'move', 'expire', 'persist', 'sort', 'expireat', 'ttl', 'append',
  75. 'getrange', 'setnx', 'decr', 'getset', 'setrange', 'decrby', 'incr', 'set', 'strlen',
  76. 'get', 'incrby', 'setbit', 'getbit', 'setex', 'hdel', 'hgetall', 'hlen', 'hset',
  77. 'hexists', 'hincrby', 'hmget', 'hsetnx', 'hget', 'hkeys', 'hmset', 'hvals', 'lindex',
  78. 'linsert', 'llen', 'lpop', 'lpush', 'lpushx', 'rpushx', 'lrange', 'lrem', 'lset',
  79. 'ltrim', 'rpop', 'rpush', 'rpushx', 'sadd', 'scard', 'sismember', 'smembers', 'spop',
  80. 'srandmember', 'srem', 'zadd', 'zcard', 'zcount', 'zincrby', 'zrange', 'zrangebyscore',
  81. 'zrank', 'zrem', 'zremrangebyrank', 'zremrangebyscore', 'zrevrange', 'zrevrangebyscore',
  82. 'zrevrank', 'zscore', 'publish', 'keys',
  83. );
  84. $cmdMultiKeys = array(
  85. 'del', 'rename', 'renamenx', 'mget', 'rpoplpush', 'sdiff', 'sdiffstore', 'sinter',
  86. 'sinterstore', 'sunion', 'sunionstore', 'subscribe', 'punsubscribe', 'subscribe',
  87. 'unsubscribe', 'watch',
  88. );
  89. return array_merge(
  90. array_fill_keys($cmdSingleKey, $this->getSingleKeyStrategy()),
  91. array_fill_keys($cmdMultiKeys, $this->getMultipleKeysStrategy()),
  92. array(
  93. 'blpop' => $skipLast, 'brpop' => $skipLast, 'brpoplpush' => $skipLast, 'smove' => $skipLast,
  94. 'mset' => $interleavedKeys, 'msetnx' => $interleavedKeys,
  95. 'zinterstore' => $zunionstore, 'zunionstore' => $zunionstore,
  96. 'sort' => $sort, 'debug' => $debug
  97. )
  98. );
  99. }
  100. public function setPrefixStrategy($command, $strategy) {
  101. if (!is_callable($callable)) {
  102. throw new \InvalidArgumentException(
  103. 'The command preprocessor strategy must be a callable object'
  104. );
  105. }
  106. $this->_strategies[$command] = $strategy;
  107. }
  108. public function getSingleKeyStrategy() {
  109. return function(&$arguments, $prefix) {
  110. $arguments[0] = "$prefix{$arguments[0]}";
  111. };
  112. }
  113. public function getMultipleKeysStrategy() {
  114. return function(&$arguments, $prefix) {
  115. foreach ($arguments as &$key) {
  116. $key = "$prefix$key";
  117. }
  118. };
  119. }
  120. public function getPrefixStrategy($command) {
  121. if (isset($this->_strategies[$command])) {
  122. return $this->_strategies[$command];
  123. }
  124. }
  125. public function setPrefix($prefix) {
  126. $this->_prefix = $prefix;
  127. }
  128. public function getPrefix() {
  129. return $this->_prefix;
  130. }
  131. public function process(ICommand $command) {
  132. $method = strtolower($command->getId());
  133. if (isset($this->_strategies[$method])) {
  134. $arguments = $command->getArguments();
  135. $this->_strategies[$method]($arguments, $this->_prefix);
  136. $command->setArguments($arguments);
  137. }
  138. }
  139. }