KeyPrefixProcessor.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Command\Processor;
  11. use Predis\Command\CommandInterface;
  12. use Predis\Command\PrefixableCommandInterface;
  13. /**
  14. * Command processor used to prefix the keys contained in the arguments of a
  15. * Redis command.
  16. *
  17. * @author Daniele Alessandri <suppakilla@gmail.com>
  18. */
  19. class KeyPrefixProcessor implements CommandProcessorInterface
  20. {
  21. private $prefix;
  22. private $commands;
  23. /**
  24. * @param string $prefix Prefix for the keys.
  25. */
  26. public function __construct($prefix)
  27. {
  28. $this->prefix = $prefix;
  29. $this->commands = array(
  30. /* ---------------- Redis 1.2 ---------------- */
  31. 'EXISTS' => 'self::first',
  32. 'DEL' => 'self::all',
  33. 'TYPE' => 'self::first',
  34. 'KEYS' => 'self::first',
  35. 'RENAME' => 'self::all',
  36. 'RENAMENX' => 'self::all',
  37. 'EXPIRE' => 'self::first',
  38. 'EXPIREAT' => 'self::first',
  39. 'TTL' => 'self::first',
  40. 'MOVE' => 'self::first',
  41. 'SORT' => 'self::sort',
  42. 'DUMP' => 'self::first',
  43. 'RESTORE' => 'self::first',
  44. 'SET' => 'self::first',
  45. 'SETNX' => 'self::first',
  46. 'MSET' => 'self::interleaved',
  47. 'MSETNX' => 'self::interleaved',
  48. 'GET' => 'self::first',
  49. 'MGET' => 'self::all',
  50. 'GETSET' => 'self::first',
  51. 'INCR' => 'self::first',
  52. 'INCRBY' => 'self::first',
  53. 'DECR' => 'self::first',
  54. 'DECRBY' => 'self::first',
  55. 'RPUSH' => 'self::first',
  56. 'LPUSH' => 'self::first',
  57. 'LLEN' => 'self::first',
  58. 'LRANGE' => 'self::first',
  59. 'LTRIM' => 'self::first',
  60. 'LINDEX' => 'self::first',
  61. 'LSET' => 'self::first',
  62. 'LREM' => 'self::first',
  63. 'LPOP' => 'self::first',
  64. 'RPOP' => 'self::first',
  65. 'RPOPLPUSH' => 'self::all',
  66. 'SADD' => 'self::first',
  67. 'SREM' => 'self::first',
  68. 'SPOP' => 'self::first',
  69. 'SMOVE' => 'self::skipLast',
  70. 'SCARD' => 'self::first',
  71. 'SISMEMBER' => 'self::first',
  72. 'SINTER' => 'self::all',
  73. 'SINTERSTORE' => 'self::all',
  74. 'SUNION' => 'self::all',
  75. 'SUNIONSTORE' => 'self::all',
  76. 'SDIFF' => 'self::all',
  77. 'SDIFFSTORE' => 'self::all',
  78. 'SMEMBERS' => 'self::first',
  79. 'SRANDMEMBER' => 'self::first',
  80. 'ZADD' => 'self::first',
  81. 'ZINCRBY' => 'self::first',
  82. 'ZREM' => 'self::first',
  83. 'ZRANGE' => 'self::first',
  84. 'ZREVRANGE' => 'self::first',
  85. 'ZRANGEBYSCORE' => 'self::first',
  86. 'ZCARD' => 'self::first',
  87. 'ZSCORE' => 'self::first',
  88. 'ZREMRANGEBYSCORE' => 'self::first',
  89. /* ---------------- Redis 2.0 ---------------- */
  90. 'SETEX' => 'self::first',
  91. 'APPEND' => 'self::first',
  92. 'SUBSTR' => 'self::first',
  93. 'BLPOP' => 'self::skipLast',
  94. 'BRPOP' => 'self::skipLast',
  95. 'ZUNIONSTORE' => 'self::zsetStore',
  96. 'ZINTERSTORE' => 'self::zsetStore',
  97. 'ZCOUNT' => 'self::first',
  98. 'ZRANK' => 'self::first',
  99. 'ZREVRANK' => 'self::first',
  100. 'ZREMRANGEBYRANK' => 'self::first',
  101. 'HSET' => 'self::first',
  102. 'HSETNX' => 'self::first',
  103. 'HMSET' => 'self::first',
  104. 'HINCRBY' => 'self::first',
  105. 'HGET' => 'self::first',
  106. 'HMGET' => 'self::first',
  107. 'HDEL' => 'self::first',
  108. 'HEXISTS' => 'self::first',
  109. 'HLEN' => 'self::first',
  110. 'HKEYS' => 'self::first',
  111. 'HVALS' => 'self::first',
  112. 'HGETALL' => 'self::first',
  113. 'SUBSCRIBE' => 'self::all',
  114. 'UNSUBSCRIBE' => 'self::all',
  115. 'PSUBSCRIBE' => 'self::all',
  116. 'PUNSUBSCRIBE' => 'self::all',
  117. 'PUBLISH' => 'self::first',
  118. /* ---------------- Redis 2.2 ---------------- */
  119. 'PERSIST' => 'self::first',
  120. 'STRLEN' => 'self::first',
  121. 'SETRANGE' => 'self::first',
  122. 'GETRANGE' => 'self::first',
  123. 'SETBIT' => 'self::first',
  124. 'GETBIT' => 'self::first',
  125. 'RPUSHX' => 'self::first',
  126. 'LPUSHX' => 'self::first',
  127. 'LINSERT' => 'self::first',
  128. 'BRPOPLPUSH' => 'self::skipLast',
  129. 'ZREVRANGEBYSCORE' => 'self::first',
  130. 'WATCH' => 'self::all',
  131. /* ---------------- Redis 2.6 ---------------- */
  132. 'PTTL' => 'self::first',
  133. 'PEXPIRE' => 'self::first',
  134. 'PEXPIREAT' => 'self::first',
  135. 'PSETEX' => 'self::first',
  136. 'INCRBYFLOAT' => 'self::first',
  137. 'BITOP' => 'self::skipFirst',
  138. 'BITCOUNT' => 'self::first',
  139. 'HINCRBYFLOAT' => 'self::first',
  140. 'EVAL' => 'self::evalKeys',
  141. 'EVALSHA' => 'self::evalKeys',
  142. /* ---------------- Redis 2.8 ---------------- */
  143. 'SSCAN' => 'self::first',
  144. 'ZSCAN' => 'self::first',
  145. 'HSCAN' => 'self::first',
  146. );
  147. }
  148. /**
  149. * Sets a prefix that is applied to all the keys.
  150. *
  151. * @param string $prefix Prefix for the keys.
  152. */
  153. public function setPrefix($prefix)
  154. {
  155. $this->prefix = $prefix;
  156. }
  157. /**
  158. * Gets the current prefix.
  159. *
  160. * @return string
  161. */
  162. public function getPrefix()
  163. {
  164. return $this->prefix;
  165. }
  166. /**
  167. * {@inheritdoc}
  168. */
  169. public function process(CommandInterface $command)
  170. {
  171. if ($command instanceof PrefixableCommandInterface) {
  172. $command->prefixKeys($this->prefix);
  173. } else if (isset($this->commands[$commandID = strtoupper($command->getId())])) {
  174. call_user_func($this->commands[$commandID], $command, $this->prefix);
  175. }
  176. }
  177. /**
  178. * Sets an handler for the specified command ID.
  179. *
  180. * The callback signature must have 2 parameters of the following types:
  181. *
  182. * - Predis\Command\CommandInterface (command instance)
  183. * - String (prefix)
  184. *
  185. * When the callback argument is omitted or NULL, the previously
  186. * associated handler for the specified command ID is removed.
  187. *
  188. * @param string $commandID The ID of the command to be handled.
  189. * @param mixed $callback A valid callable object or NULL.
  190. */
  191. public function setCommandHandler($commandID, $callback = null)
  192. {
  193. $commandID = strtoupper($commandID);
  194. if (!isset($callback)) {
  195. unset($this->commands[$commandID]);
  196. return;
  197. }
  198. if (!is_callable($callback)) {
  199. throw new InvalidArgumentException(
  200. "Callback must be a valid callable object or NULL"
  201. );
  202. }
  203. $this->commands[$commandID] = $callback;
  204. }
  205. /**
  206. * {@inheritdoc}
  207. */
  208. public function __toString()
  209. {
  210. return $this->getPrefix();
  211. }
  212. /**
  213. * Applies the specified prefix only the first argument.
  214. *
  215. * @param CommandInterface $command Command instance.
  216. * @param string $prefix Prefix string.
  217. */
  218. public static function first(CommandInterface $command, $prefix)
  219. {
  220. if ($arguments = $command->getArguments()) {
  221. $arguments[0] = "$prefix{$arguments[0]}";
  222. $command->setRawArguments($arguments);
  223. }
  224. }
  225. /**
  226. * Applies the specified prefix to all the arguments.
  227. *
  228. * @param CommandInterface $command Command instance.
  229. * @param string $prefix Prefix string.
  230. */
  231. public static function all(CommandInterface $command, $prefix)
  232. {
  233. if ($arguments = $command->getArguments()) {
  234. foreach ($arguments as &$key) {
  235. $key = "$prefix$key";
  236. }
  237. $command->setRawArguments($arguments);
  238. }
  239. }
  240. /**
  241. * Applies the specified prefix only to even arguments in the list.
  242. *
  243. * @param CommandInterface $command Command instance.
  244. * @param string $prefix Prefix string.
  245. */
  246. public static function interleaved(CommandInterface $command, $prefix)
  247. {
  248. if ($arguments = $command->getArguments()) {
  249. $length = count($arguments);
  250. for ($i = 0; $i < $length; $i += 2) {
  251. $arguments[$i] = "$prefix{$arguments[$i]}";
  252. }
  253. $command->setRawArguments($arguments);
  254. }
  255. }
  256. /**
  257. * Applies the specified prefix to all the arguments but the first one.
  258. *
  259. * @param CommandInterface $command Command instance.
  260. * @param string $prefix Prefix string.
  261. */
  262. public static function skipFirst(CommandInterface $command, $prefix)
  263. {
  264. if ($arguments = $command->getArguments()) {
  265. $length = count($arguments);
  266. for ($i = 1; $i < $length; $i++) {
  267. $arguments[$i] = "$prefix{$arguments[$i]}";
  268. }
  269. $command->setRawArguments($arguments);
  270. }
  271. }
  272. /**
  273. * Applies the specified prefix to all the arguments but the last one.
  274. *
  275. * @param CommandInterface $command Command instance.
  276. * @param string $prefix Prefix string.
  277. */
  278. public static function skipLast(CommandInterface $command, $prefix)
  279. {
  280. if ($arguments = $command->getArguments()) {
  281. $length = count($arguments);
  282. for ($i = 0; $i < $length - 1; $i++) {
  283. $arguments[$i] = "$prefix{$arguments[$i]}";
  284. }
  285. $command->setRawArguments($arguments);
  286. }
  287. }
  288. /**
  289. * Applies the specified prefix to the keys of a SORT command.
  290. *
  291. * @param CommandInterface $command Command instance.
  292. * @param string $prefix Prefix string.
  293. */
  294. public static function sort(CommandInterface $command, $prefix)
  295. {
  296. if ($arguments = $command->getArguments()) {
  297. $arguments[0] = "$prefix{$arguments[0]}";
  298. if (($count = count($arguments)) > 1) {
  299. for ($i = 1; $i < $count; $i++) {
  300. switch ($arguments[$i]) {
  301. case 'BY':
  302. case 'STORE':
  303. $arguments[$i] = "$prefix{$arguments[++$i]}";
  304. break;
  305. case 'GET':
  306. $value = $arguments[++$i];
  307. if ($value !== '#') {
  308. $arguments[$i] = "$prefix$value";
  309. }
  310. break;
  311. case 'LIMIT';
  312. $i += 2;
  313. break;
  314. }
  315. }
  316. }
  317. $command->setRawArguments($arguments);
  318. }
  319. }
  320. /**
  321. * Applies the specified prefix to the keys of an EVAL-based command.
  322. *
  323. * @param CommandInterface $command Command instance.
  324. * @param string $prefix Prefix string.
  325. */
  326. public static function evalKeys(CommandInterface $command, $prefix)
  327. {
  328. if ($arguments = $command->getArguments()) {
  329. for ($i = 2; $i < $arguments[1] + 2; $i++) {
  330. $arguments[$i] = "$prefix{$arguments[$i]}";
  331. }
  332. $command->setRawArguments($arguments);
  333. }
  334. }
  335. /**
  336. * Applies the specified prefix to the keys of Z[INTERSECTION|UNION]STORE.
  337. *
  338. * @param CommandInterface $command Command instance.
  339. * @param string $prefix Prefix string.
  340. */
  341. public static function zsetStore(CommandInterface $command, $prefix)
  342. {
  343. if ($arguments = $command->getArguments()) {
  344. $arguments[0] = "$prefix{$arguments[0]}";
  345. $length = ((int) $arguments[1]) + 2;
  346. for ($i = 2; $i < $length; $i++) {
  347. $arguments[$i] = "$prefix{$arguments[$i]}";
  348. }
  349. $command->setRawArguments($arguments);
  350. }
  351. }
  352. }