KeyPrefixProcessor.php 15 KB

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