Command.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. use Predis\Helpers;
  12. use Predis\Distribution\INodeKeyGenerator;
  13. /**
  14. * Base class for Redis commands.
  15. *
  16. * @author Daniele Alessandri <suppakilla@gmail.com>
  17. */
  18. abstract class Command implements ICommand
  19. {
  20. private $hash;
  21. private $arguments = array();
  22. /**
  23. * Returns a filtered array of the arguments.
  24. *
  25. * @param array $arguments List of arguments.
  26. * @return array
  27. */
  28. protected function filterArguments(Array $arguments)
  29. {
  30. return $arguments;
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function setArguments(Array $arguments)
  36. {
  37. $this->arguments = $this->filterArguments($arguments);
  38. unset($this->hash);
  39. }
  40. /**
  41. * Sets the arguments array without filtering.
  42. *
  43. * @param array $arguments List of arguments.
  44. */
  45. public function setRawArguments(Array $arguments)
  46. {
  47. $this->arguments = $arguments;
  48. unset($this->hash);
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function getArguments()
  54. {
  55. return $this->arguments;
  56. }
  57. /**
  58. * Gets the argument from the arguments list at the specified index.
  59. *
  60. * @param array $arguments Position of the argument.
  61. */
  62. public function getArgument($index = 0)
  63. {
  64. if (isset($this->arguments[$index]) === true) {
  65. return $this->arguments[$index];
  66. }
  67. }
  68. /**
  69. * Checks if the command can return an hash for client-side sharding.
  70. *
  71. * @return Boolean
  72. */
  73. protected function canBeHashed()
  74. {
  75. return isset($this->arguments[0]);
  76. }
  77. /**
  78. * Checks if the specified array of keys will generate the same hash.
  79. *
  80. * @param array $keys Array of keys.
  81. * @return Boolean
  82. */
  83. protected function checkSameHashForKeys(Array $keys)
  84. {
  85. if (($count = count($keys)) === 0) {
  86. return false;
  87. }
  88. $currentKey = Helpers::extractKeyTag($keys[0]);
  89. for ($i = 1; $i < $count; $i++) {
  90. $nextKey = Helpers::extractKeyTag($keys[$i]);
  91. if ($currentKey !== $nextKey) {
  92. return false;
  93. }
  94. $currentKey = $nextKey;
  95. }
  96. return true;
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function getHash(INodeKeyGenerator $distributor)
  102. {
  103. if (isset($this->hash)) {
  104. return $this->hash;
  105. }
  106. if ($this->canBeHashed()) {
  107. $key = Helpers::extractKeyTag($this->arguments[0]);
  108. $this->hash = $distributor->generateKey($key);
  109. return $this->hash;
  110. }
  111. return null;
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function parseResponse($data)
  117. {
  118. return $data;
  119. }
  120. /**
  121. * Helper function used to reduce a list of arguments to a string.
  122. *
  123. * @param string $accumulator Temporary string.
  124. * @param string $argument Current argument.
  125. * @return string
  126. */
  127. protected function toStringArgumentReducer($accumulator, $argument)
  128. {
  129. if (strlen($argument) > 32) {
  130. $argument = substr($argument, 0, 32) . '[...]';
  131. }
  132. $accumulator .= " $argument";
  133. return $accumulator;
  134. }
  135. /**
  136. * Returns a partial string representation of the command with its arguments.
  137. *
  138. * @return string
  139. */
  140. public function __toString()
  141. {
  142. return array_reduce(
  143. $this->getArguments(),
  144. array($this, 'toStringArgumentReducer'),
  145. $this->getId()
  146. );
  147. }
  148. }