Command.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. * Implements the rule that is used to prefix the keys and returns a new
  70. * array of arguments with the modified keys.
  71. *
  72. * @param array $arguments Arguments of the command.
  73. * @param string $prefix Prefix appended to each key in the arguments.
  74. * @return array
  75. */
  76. protected function onPrefixKeys(Array $arguments, $prefix)
  77. {
  78. $arguments[0] = "$prefix{$arguments[0]}";
  79. return $arguments;
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function prefixKeys($prefix)
  85. {
  86. $arguments = $this->onPrefixKeys($this->arguments, $prefix);
  87. if (isset($arguments)) {
  88. $this->arguments = $arguments;
  89. unset($this->hash);
  90. }
  91. }
  92. /**
  93. * Checks if the command can return an hash for client-side sharding.
  94. *
  95. * @return Boolean
  96. */
  97. protected function canBeHashed()
  98. {
  99. return isset($this->arguments[0]);
  100. }
  101. /**
  102. * Checks if the specified array of keys will generate the same hash.
  103. *
  104. * @param array $keys Array of keys.
  105. * @return Boolean
  106. */
  107. protected function checkSameHashForKeys(Array $keys)
  108. {
  109. if (($count = count($keys)) === 0) {
  110. return false;
  111. }
  112. $currentKey = Helpers::extractKeyTag($keys[0]);
  113. for ($i = 1; $i < $count; $i++) {
  114. $nextKey = Helpers::extractKeyTag($keys[$i]);
  115. if ($currentKey !== $nextKey) {
  116. return false;
  117. }
  118. $currentKey = $nextKey;
  119. }
  120. return true;
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function getHash(INodeKeyGenerator $distributor)
  126. {
  127. if (isset($this->hash)) {
  128. return $this->hash;
  129. }
  130. if ($this->canBeHashed()) {
  131. $key = Helpers::extractKeyTag($this->arguments[0]);
  132. $this->hash = $distributor->generateKey($key);
  133. return $this->hash;
  134. }
  135. return null;
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. public function parseResponse($data)
  141. {
  142. return $data;
  143. }
  144. /**
  145. * Helper function used to reduce a list of arguments to a string.
  146. *
  147. * @param string $accumulator Temporary string.
  148. * @param string $argument Current argument.
  149. * @return string
  150. */
  151. protected function toStringArgumentReducer($accumulator, $argument)
  152. {
  153. if (strlen($argument) > 32) {
  154. $argument = substr($argument, 0, 32) . '[...]';
  155. }
  156. $accumulator .= " $argument";
  157. return $accumulator;
  158. }
  159. /**
  160. * Returns a partial string representation of the command with its arguments.
  161. *
  162. * @return string
  163. */
  164. public function __toString()
  165. {
  166. return array_reduce(
  167. $this->getArguments(),
  168. array($this, 'toStringArgumentReducer'),
  169. $this->getId()
  170. );
  171. }
  172. }