KeyPrefixProcessor.php 14 KB

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