KeyPrefixProcessor.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. );
  151. }
  152. /**
  153. * Sets a prefix that is applied to all the keys.
  154. *
  155. * @param string $prefix Prefix for the keys.
  156. */
  157. public function setPrefix($prefix)
  158. {
  159. $this->prefix = $prefix;
  160. }
  161. /**
  162. * Gets the current prefix.
  163. *
  164. * @return string
  165. */
  166. public function getPrefix()
  167. {
  168. return $this->prefix;
  169. }
  170. /**
  171. * {@inheritdoc}
  172. */
  173. public function process(CommandInterface $command)
  174. {
  175. if ($command instanceof PrefixableCommandInterface) {
  176. $command->prefixKeys($this->prefix);
  177. } elseif (isset($this->commands[$commandID = strtoupper($command->getId())])) {
  178. call_user_func($this->commands[$commandID], $command, $this->prefix);
  179. }
  180. }
  181. /**
  182. * Sets an handler for the specified command ID.
  183. *
  184. * The callback signature must have 2 parameters of the following types:
  185. *
  186. * - Predis\Command\CommandInterface (command instance)
  187. * - String (prefix)
  188. *
  189. * When the callback argument is omitted or NULL, the previously
  190. * associated handler for the specified command ID is removed.
  191. *
  192. * @param string $commandID The ID of the command to be handled.
  193. * @param mixed $callback A valid callable object or NULL.
  194. */
  195. public function setCommandHandler($commandID, $callback = null)
  196. {
  197. $commandID = strtoupper($commandID);
  198. if (!isset($callback)) {
  199. unset($this->commands[$commandID]);
  200. return;
  201. }
  202. if (!is_callable($callback)) {
  203. throw new InvalidArgumentException(
  204. "Callback must be a valid callable object or NULL"
  205. );
  206. }
  207. $this->commands[$commandID] = $callback;
  208. }
  209. /**
  210. * {@inheritdoc}
  211. */
  212. public function __toString()
  213. {
  214. return $this->getPrefix();
  215. }
  216. /**
  217. * Applies the specified prefix only the first argument.
  218. *
  219. * @param CommandInterface $command Command instance.
  220. * @param string $prefix Prefix string.
  221. */
  222. public static function first(CommandInterface $command, $prefix)
  223. {
  224. if ($arguments = $command->getArguments()) {
  225. $arguments[0] = "$prefix{$arguments[0]}";
  226. $command->setRawArguments($arguments);
  227. }
  228. }
  229. /**
  230. * Applies the specified prefix to all the arguments.
  231. *
  232. * @param CommandInterface $command Command instance.
  233. * @param string $prefix Prefix string.
  234. */
  235. public static function all(CommandInterface $command, $prefix)
  236. {
  237. if ($arguments = $command->getArguments()) {
  238. foreach ($arguments as &$key) {
  239. $key = "$prefix$key";
  240. }
  241. $command->setRawArguments($arguments);
  242. }
  243. }
  244. /**
  245. * Applies the specified prefix only to even arguments in the list.
  246. *
  247. * @param CommandInterface $command Command instance.
  248. * @param string $prefix Prefix string.
  249. */
  250. public static function interleaved(CommandInterface $command, $prefix)
  251. {
  252. if ($arguments = $command->getArguments()) {
  253. $length = count($arguments);
  254. for ($i = 0; $i < $length; $i += 2) {
  255. $arguments[$i] = "$prefix{$arguments[$i]}";
  256. }
  257. $command->setRawArguments($arguments);
  258. }
  259. }
  260. /**
  261. * Applies the specified prefix to all the arguments but the first one.
  262. *
  263. * @param CommandInterface $command Command instance.
  264. * @param string $prefix Prefix string.
  265. */
  266. public static function skipFirst(CommandInterface $command, $prefix)
  267. {
  268. if ($arguments = $command->getArguments()) {
  269. $length = count($arguments);
  270. for ($i = 1; $i < $length; $i++) {
  271. $arguments[$i] = "$prefix{$arguments[$i]}";
  272. }
  273. $command->setRawArguments($arguments);
  274. }
  275. }
  276. /**
  277. * Applies the specified prefix to all the arguments but the last one.
  278. *
  279. * @param CommandInterface $command Command instance.
  280. * @param string $prefix Prefix string.
  281. */
  282. public static function skipLast(CommandInterface $command, $prefix)
  283. {
  284. if ($arguments = $command->getArguments()) {
  285. $length = count($arguments);
  286. for ($i = 0; $i < $length - 1; $i++) {
  287. $arguments[$i] = "$prefix{$arguments[$i]}";
  288. }
  289. $command->setRawArguments($arguments);
  290. }
  291. }
  292. /**
  293. * Applies the specified prefix to the keys of a SORT command.
  294. *
  295. * @param CommandInterface $command Command instance.
  296. * @param string $prefix Prefix string.
  297. */
  298. public static function sort(CommandInterface $command, $prefix)
  299. {
  300. if ($arguments = $command->getArguments()) {
  301. $arguments[0] = "$prefix{$arguments[0]}";
  302. if (($count = count($arguments)) > 1) {
  303. for ($i = 1; $i < $count; $i++) {
  304. switch ($arguments[$i]) {
  305. case 'BY':
  306. case 'STORE':
  307. $arguments[$i] = "$prefix{$arguments[++$i]}";
  308. break;
  309. case 'GET':
  310. $value = $arguments[++$i];
  311. if ($value !== '#') {
  312. $arguments[$i] = "$prefix$value";
  313. }
  314. break;
  315. case 'LIMIT';
  316. $i += 2;
  317. break;
  318. }
  319. }
  320. }
  321. $command->setRawArguments($arguments);
  322. }
  323. }
  324. /**
  325. * Applies the specified prefix to the keys of an EVAL-based command.
  326. *
  327. * @param CommandInterface $command Command instance.
  328. * @param string $prefix Prefix string.
  329. */
  330. public static function evalKeys(CommandInterface $command, $prefix)
  331. {
  332. if ($arguments = $command->getArguments()) {
  333. for ($i = 2; $i < $arguments[1] + 2; $i++) {
  334. $arguments[$i] = "$prefix{$arguments[$i]}";
  335. }
  336. $command->setRawArguments($arguments);
  337. }
  338. }
  339. /**
  340. * Applies the specified prefix to the keys of Z[INTERSECTION|UNION]STORE.
  341. *
  342. * @param CommandInterface $command Command instance.
  343. * @param string $prefix Prefix string.
  344. */
  345. public static function zsetStore(CommandInterface $command, $prefix)
  346. {
  347. if ($arguments = $command->getArguments()) {
  348. $arguments[0] = "$prefix{$arguments[0]}";
  349. $length = ((int) $arguments[1]) + 2;
  350. for ($i = 2; $i < $length; $i++) {
  351. $arguments[$i] = "$prefix{$arguments[$i]}";
  352. }
  353. $command->setRawArguments($arguments);
  354. }
  355. }
  356. }