ScriptCommand.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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;
  11. /**
  12. * Base class used to implement an higher level abstraction for commands based
  13. * on Lua scripting with EVAL and EVALSHA.
  14. *
  15. * @link http://redis.io/commands/eval
  16. *
  17. * @author Daniele Alessandri <suppakilla@gmail.com>
  18. */
  19. abstract class ScriptCommand extends ServerEvalSHA
  20. {
  21. /**
  22. * Gets the body of a Lua script.
  23. *
  24. * @return string
  25. */
  26. abstract public function getScript();
  27. /**
  28. * Specifies the number of arguments that should be considered as keys.
  29. *
  30. * The default behaviour for the base class is to return 0 to indicate that
  31. * all the elements of the arguments array should be considered as keys, but
  32. * subclasses can enforce a static number of keys.
  33. *
  34. * @return int
  35. */
  36. protected function getKeysCount()
  37. {
  38. return 0;
  39. }
  40. /**
  41. * Returns the elements from the arguments that are identified as keys.
  42. *
  43. * @return array
  44. */
  45. public function getKeys()
  46. {
  47. return array_slice($this->getArguments(), 2, $this->getKeysCount());
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. protected function filterArguments(array $arguments)
  53. {
  54. if (($numkeys = $this->getKeysCount()) && $numkeys < 0) {
  55. $numkeys = count($arguments) + $numkeys;
  56. }
  57. return array_merge(array(sha1($this->getScript()), (int) $numkeys), $arguments);
  58. }
  59. /**
  60. * @return array
  61. */
  62. public function getEvalArguments()
  63. {
  64. $arguments = $this->getArguments();
  65. $arguments[0] = $this->getScript();
  66. return $arguments;
  67. }
  68. }