ScriptedCommand.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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\Commands;
  11. /**
  12. * Base class used to implement an higher level abstraction for "virtual"
  13. * commands based on EVAL.
  14. *
  15. * @link http://redis.io/commands/eval
  16. * @author Daniele Alessandri <suppakilla@gmail.com>
  17. */
  18. abstract class ScriptedCommand extends ServerEval
  19. {
  20. /**
  21. * Gets the body of a Lua script.
  22. *
  23. * @return string
  24. */
  25. public abstract function getScript();
  26. /**
  27. * Gets the number of arguments that should be considered as keys.
  28. *
  29. * @todo Should we make a scripted command act by default as a variadic
  30. * command where the first argument is the key (KEYS[1]) and the
  31. * rest is the list of values (ARGV)?
  32. *
  33. * @return int
  34. */
  35. public function getKeysCount()
  36. {
  37. // The default behaviour for the base class is to use all the arguments
  38. // passed to a scripted command to populate the KEYS table in Lua.
  39. return count($this->getArguments());
  40. }
  41. /**
  42. * Returns the elements from the arguments that are identified as keys.
  43. *
  44. * @return array
  45. */
  46. public function getKeys()
  47. {
  48. return array_slice($this->getArguments(), 2, $this->getKeysCount());
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. protected function filterArguments(Array $arguments)
  54. {
  55. return array_merge(array($this->getScript(), $this->getKeysCount()), $arguments);
  56. }
  57. }