Command.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace Predis;
  3. use Predis\Distribution\IDistributionStrategy;
  4. abstract class Command implements ICommand {
  5. private $_hash;
  6. private $_arguments = array();
  7. public function canBeHashed() {
  8. return true;
  9. }
  10. public function getHash(IDistributionStrategy $distributor) {
  11. if (isset($this->_hash)) {
  12. return $this->_hash;
  13. }
  14. if (isset($this->_arguments[0])) {
  15. // TODO: should we throw an exception if the command does not
  16. // support sharding?
  17. $key = $this->_arguments[0];
  18. $start = strpos($key, '{');
  19. if ($start !== false) {
  20. $end = strpos($key, '}', $start);
  21. if ($end !== false) {
  22. $key = substr($key, ++$start, $end - $start);
  23. }
  24. }
  25. $this->_hash = $distributor->generateKey($key);
  26. return $this->_hash;
  27. }
  28. return null;
  29. }
  30. public function closesConnection() {
  31. return false;
  32. }
  33. protected function filterArguments(Array $arguments) {
  34. return $arguments;
  35. }
  36. public function setArguments(/* arguments */) {
  37. $this->_arguments = $this->filterArguments(func_get_args());
  38. unset($this->_hash);
  39. }
  40. public function setArgumentsArray(Array $arguments) {
  41. $this->_arguments = $this->filterArguments($arguments);
  42. unset($this->_hash);
  43. }
  44. public function getArguments() {
  45. return $this->_arguments;
  46. }
  47. public function getArgument($index = 0) {
  48. if (isset($this->_arguments[$index]) === true) {
  49. return $this->_arguments[$index];
  50. }
  51. }
  52. public function parseResponse($data) {
  53. return $data;
  54. }
  55. }