ScriptedCommand.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. * @return int
  30. */
  31. protected function keysCount()
  32. {
  33. // The default behaviour for the base class is to use all the arguments
  34. // passed to a scripted command to populate the KEYS table in Lua.
  35. return count($this->getArguments());
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. protected function filterArguments(Array $arguments)
  41. {
  42. return array_merge(array($this->getScript(), $this->keysCount()), $arguments);
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. protected function getKeys()
  48. {
  49. return array_slice($this->getArguments(), 2, $this->keysCount());
  50. }
  51. }