PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/rediska/vendor/rediska/Rediska/Commands.php

https://bitbucket.org/sudak/rating
PHP | 204 lines | 123 code | 25 blank | 56 comment | 3 complexity | 5f1d7e9760abebea56893f3941cd13ab MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Rediska commands
  4. *
  5. * @author Ivan Shumkov
  6. * @package Rediska
  7. * @subpackage Commands
  8. * @version @package_version@
  9. * @link http://rediska.geometria-lab.net
  10. * @license http://www.opensource.org/licenses/bsd-license.php
  11. */
  12. class Rediska_Commands
  13. {
  14. /**
  15. * Redis commands
  16. *
  17. * @var array
  18. */
  19. protected static $_commands = array(
  20. // Connection handling
  21. 'quit' => 'Rediska_Command_Quit',
  22. // Commands operating on all value types
  23. 'exists' => 'Rediska_Command_Exists',
  24. 'delete' => 'Rediska_Command_Delete',
  25. 'gettype' => 'Rediska_Command_GetType',
  26. 'getkeysbypattern' => 'Rediska_Command_GetKeysByPattern',
  27. 'getrandomkey' => 'Rediska_Command_GetRandomKey',
  28. 'rename' => 'Rediska_Command_Rename',
  29. 'getkeyscount' => 'Rediska_Command_GetKeysCount',
  30. 'expire' => 'Rediska_Command_Expire',
  31. 'getlifetime' => 'Rediska_Command_GetLifetime',
  32. 'selectdb' => 'Rediska_Command_SelectDb',
  33. 'movetodb' => 'Rediska_Command_MoveToDb',
  34. 'flushdb' => 'Rediska_Command_FlushDb',
  35. // Commands operating on string values
  36. 'set' => 'Rediska_Command_Set',
  37. 'setandexpire' => 'Rediska_Command_SetAndExpire',
  38. 'setandget' => 'Rediska_Command_SetAndGet',
  39. 'get' => 'Rediska_Command_Get',
  40. 'append' => 'Rediska_Command_Append',
  41. 'increment' => 'Rediska_Command_Increment',
  42. 'decrement' => 'Rediska_Command_Decrement',
  43. 'setrange' => 'Rediska_Command_SetRange',
  44. 'getrange' => 'Rediska_Command_GetRange',
  45. 'substring' => 'Rediska_Command_GetRange',
  46. 'setbit' => 'Rediska_Command_SetBit',
  47. 'getbit' => 'Rediska_Command_GetBit',
  48. 'getlength' => 'Rediska_Command_GetLength',
  49. // Commands operating on lists
  50. 'appendtolist' => 'Rediska_Command_AppendToList',
  51. 'prependtolist' => 'Rediska_Command_PrependToList',
  52. 'getlistlength' => 'Rediska_Command_GetListLength',
  53. 'getlist' => 'Rediska_Command_GetList',
  54. 'truncatelist' => 'Rediska_Command_TruncateList',
  55. 'getfromlist' => 'Rediska_Command_GetFromList',
  56. 'settolist' => 'Rediska_Command_SetToList',
  57. 'deletefromlist' => 'Rediska_Command_DeleteFromList',
  58. 'shiftfromlist' => 'Rediska_Command_ShiftFromList',
  59. 'shiftfromlistblocking' => 'Rediska_Command_ShiftFromListBlocking',
  60. 'popfromlist' => 'Rediska_Command_PopFromList',
  61. 'popfromlistblocking' => 'Rediska_Command_PopFromListBlocking',
  62. 'inserttolist' => 'Rediska_Command_InsertToList',
  63. 'inserttolistafter' => 'Rediska_Command_InsertToListAfter',
  64. 'inserttolistbefore' => 'Rediska_Command_InsertToListBefore',
  65. // Commands operating on sets
  66. 'addtoset' => 'Rediska_Command_AddToSet',
  67. 'deletefromset' => 'Rediska_Command_DeleteFromSet',
  68. 'getrandomfromset' => 'Rediska_Command_GetRandomFromSet',
  69. 'getsetlength' => 'Rediska_Command_GetSetLength',
  70. 'existsinset' => 'Rediska_Command_ExistsInSet',
  71. 'intersectsets' => 'Rediska_Command_IntersectSets',
  72. 'unionsets' => 'Rediska_Command_UnionSets',
  73. 'diffsets' => 'Rediska_Command_DiffSets',
  74. 'getset' => 'Rediska_Command_GetSet',
  75. 'movetoset' => 'Rediska_Command_MoveToSet',
  76. // Commands operating on sorted sets
  77. 'addtosortedset' => 'Rediska_Command_AddToSortedSet',
  78. 'deletefromsortedset' => 'Rediska_Command_DeleteFromSortedSet',
  79. 'getsortedset' => 'Rediska_Command_GetSortedSet',
  80. 'getfromsortedsetbyscore' => 'Rediska_Command_GetFromSortedSetByScore',
  81. 'getsortedsetlength' => 'Rediska_Command_GetSortedSetLength',
  82. 'getsortedsetlengthbyscore' => 'Rediska_Command_GetSortedSetLengthByScore',
  83. 'incrementscoreinsortedset' => 'Rediska_Command_IncrementScoreInSortedSet',
  84. 'deletefromsortedsetbyscore' => 'Rediska_Command_DeleteFromSortedSetByScore',
  85. 'deletefromsortedsetbyrank' => 'Rediska_Command_DeleteFromSortedSetByRank',
  86. 'getscorefromsortedset' => 'Rediska_Command_GetScoreFromSortedSet',
  87. 'getrankfromsortedset' => 'Rediska_Command_GetRankFromSortedSet',
  88. 'unionsortedsets' => 'Rediska_Command_UnionSortedSets',
  89. 'intersectsortedsets' => 'Rediska_Command_IntersectSortedSets',
  90. // Commands operating on hashes
  91. 'settohash' => 'Rediska_Command_SetToHash',
  92. 'getfromhash' => 'Rediska_Command_GetFromHash',
  93. 'incrementinhash' => 'Rediska_Command_IncrementInHash',
  94. 'existsinhash' => 'Rediska_Command_ExistsInHash',
  95. 'deletefromhash' => 'Rediska_Command_DeleteFromHash',
  96. 'gethashlength' => 'Rediska_Command_GetHashLength',
  97. 'gethash' => 'Rediska_Command_GetHash',
  98. 'gethashfields' => 'Rediska_Command_GetHashFields',
  99. 'gethashvalues' => 'Rediska_Command_GetHashValues',
  100. // Sorting
  101. 'sort' => 'Rediska_Command_Sort',
  102. // Publish/Subscribe
  103. 'publish' => 'Rediska_Command_Publish',
  104. // Persistence control commands
  105. 'save' => 'Rediska_Command_Save',
  106. 'getlastsavetime' => 'Rediska_Command_GetLastSaveTime',
  107. 'shutdown' => 'Rediska_Command_Shutdown',
  108. 'rewriteappendonlyfile' => 'Rediska_Command_RewriteAppendOnlyFile',
  109. // Remote server control commands
  110. 'info' => 'Rediska_Command_Info',
  111. 'slaveof' => 'Rediska_Command_SlaveOf',
  112. );
  113. /**
  114. * Add command
  115. *
  116. * @param string $name Command name
  117. * @param string $className Name of class
  118. */
  119. public static function add($name, $className)
  120. {
  121. if (!class_exists($className)) {
  122. throw new Rediska_Exception("Class '$className' not found. You must include before or setup autoload");
  123. }
  124. $classReflection = new ReflectionClass($className);
  125. if (!in_array('Rediska_Command_Interface', $classReflection->getInterfaceNames())) {
  126. throw new Rediska_Exception("Class '$className' must implement Rediska_Command_Interface interface");
  127. }
  128. $lowerName = strtolower($name);
  129. self::$_commands[$lowerName] = $className;
  130. return true;
  131. }
  132. /**
  133. * Remove command
  134. *
  135. * @param string $name Command name
  136. */
  137. public static function remove($name)
  138. {
  139. $lowerName = self::_getCommandLowerNameAndThrowIfNotPresent($name);
  140. unset(self::$_commands[$lowerName]);
  141. return true;
  142. }
  143. /**
  144. * Get command instance
  145. *
  146. * @param Rediska $rediska Rediska instance
  147. * @param string $name Command name
  148. * @param array $arguments Command arguments
  149. * @return Rediska_Command_Abstract
  150. */
  151. public static function get(Rediska $rediska, $name, $arguments)
  152. {
  153. $lowerName = self::_getCommandLowerNameAndThrowIfNotPresent($name);
  154. return new self::$_commands[$lowerName]($rediska, $name, $arguments);
  155. }
  156. /**
  157. * Get command list
  158. *
  159. * @return array
  160. */
  161. public static function getList()
  162. {
  163. return self::$_commands;
  164. }
  165. /**
  166. * Get command lower name and throw exception if command not present
  167. *
  168. * @param <type> $name
  169. * @return <type>
  170. */
  171. protected static function _getCommandLowerNameAndThrowIfNotPresent($name)
  172. {
  173. $lowerName = strtolower($name);
  174. if (!isset(self::$_commands[$lowerName])) {
  175. throw new Rediska_Exception("Command '$name' not found");
  176. }
  177. return $lowerName;
  178. }
  179. }