ServerProfile.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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\Profiles;
  11. use Predis\ClientException;
  12. use Predis\Commands\Processors\ICommandProcessor;
  13. use Predis\Commands\Processors\IProcessingSupport;
  14. /**
  15. * Base class that implements common functionalities of server profiles.
  16. *
  17. * @author Daniele Alessandri <suppakilla@gmail.com>
  18. */
  19. abstract class ServerProfile implements IServerProfile, IProcessingSupport
  20. {
  21. private static $profiles;
  22. private $commands;
  23. private $processor;
  24. /**
  25. *
  26. */
  27. public function __construct()
  28. {
  29. $this->commands = $this->getSupportedCommands();
  30. }
  31. /**
  32. * Returns a map of all the commands supported by the profile and their
  33. * actual PHP classes.
  34. *
  35. * @return array
  36. */
  37. protected abstract function getSupportedCommands();
  38. /**
  39. * Returns the default server profile.
  40. *
  41. * @return IServerProfile
  42. */
  43. public static function getDefault()
  44. {
  45. return self::get('default');
  46. }
  47. /**
  48. * Returns the development server profile.
  49. *
  50. * @return IServerProfile
  51. */
  52. public static function getDevelopment()
  53. {
  54. return self::get('dev');
  55. }
  56. /**
  57. * Returns a map of all the server profiles supported by default and their
  58. * actual PHP classes.
  59. *
  60. * @return array
  61. */
  62. private static function getDefaultProfiles()
  63. {
  64. return array(
  65. '1.2' => 'Predis\Profiles\ServerVersion12',
  66. '2.0' => 'Predis\Profiles\ServerVersion20',
  67. '2.2' => 'Predis\Profiles\ServerVersion22',
  68. '2.4' => 'Predis\Profiles\ServerVersion24',
  69. '2.6' => 'Predis\Profiles\ServerVersion26',
  70. 'default' => 'Predis\Profiles\ServerVersion24',
  71. 'dev' => 'Predis\Profiles\ServerVersionNext',
  72. );
  73. }
  74. /**
  75. * Registers a new server profile.
  76. *
  77. * @param string $alias Profile version or alias.
  78. * @param string $profileClass FQN of a class implementing Predis\Profiles\IServerProfile.
  79. */
  80. public static function define($alias, $profileClass)
  81. {
  82. if (!isset(self::$profiles)) {
  83. self::$profiles = self::getDefaultProfiles();
  84. }
  85. $profileReflection = new \ReflectionClass($profileClass);
  86. if (!$profileReflection->isSubclassOf('Predis\Profiles\IServerProfile')) {
  87. throw new \InvalidArgumentException("Cannot register '$profileClass' as it is not a valid profile class");
  88. }
  89. self::$profiles[$alias] = $profileClass;
  90. }
  91. /**
  92. * Returns the specified server profile.
  93. *
  94. * @param string $version Profile version or alias.
  95. * @return IServerProfile
  96. */
  97. public static function get($version)
  98. {
  99. if (!isset(self::$profiles)) {
  100. self::$profiles = self::getDefaultProfiles();
  101. }
  102. if (!isset(self::$profiles[$version])) {
  103. throw new ClientException("Unknown server profile: $version");
  104. }
  105. $profile = self::$profiles[$version];
  106. return new $profile();
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function supportsCommands(Array $commands)
  112. {
  113. foreach ($commands as $command) {
  114. if ($this->supportsCommand($command) === false) {
  115. return false;
  116. }
  117. }
  118. return true;
  119. }
  120. /**
  121. * {@inheritdoc}
  122. */
  123. public function supportsCommand($command)
  124. {
  125. return isset($this->commands[strtolower($command)]);
  126. }
  127. /**
  128. * Returns the FQN of the class that represent the specified command ID
  129. * registered in the current server profile.
  130. *
  131. * @param string $command Command ID.
  132. * @return string
  133. */
  134. public function getCommandClass($command)
  135. {
  136. if (isset($this->commands[$command = strtolower($command)])) {
  137. return $this->commands[$command];
  138. }
  139. }
  140. /**
  141. * {@inheritdoc}
  142. */
  143. public function createCommand($method, $arguments = array())
  144. {
  145. $method = strtolower($method);
  146. if (!isset($this->commands[$method])) {
  147. throw new ClientException("'$method' is not a registered Redis command");
  148. }
  149. $commandClass = $this->commands[$method];
  150. $command = new $commandClass();
  151. $command->setArguments($arguments);
  152. if (isset($this->processor)) {
  153. $this->processor->process($command);
  154. }
  155. return $command;
  156. }
  157. /**
  158. * Defines new commands in the server profile.
  159. *
  160. * @param array $commands Named list of command IDs and their classes.
  161. */
  162. public function defineCommands(Array $commands)
  163. {
  164. foreach ($commands as $alias => $command) {
  165. $this->defineCommand($alias, $command);
  166. }
  167. }
  168. /**
  169. * Defines a new commands in the server profile.
  170. *
  171. * @param string $alias Command ID.
  172. * @param string $command FQN of a class implementing Predis\Commands\ICommand.
  173. */
  174. public function defineCommand($alias, $command)
  175. {
  176. $commandReflection = new \ReflectionClass($command);
  177. if (!$commandReflection->isSubclassOf('Predis\Commands\ICommand')) {
  178. throw new \InvalidArgumentException("Cannot register '$command' as it is not a valid Redis command");
  179. }
  180. $this->commands[strtolower($alias)] = $command;
  181. }
  182. /**
  183. * {@inheritdoc}
  184. */
  185. public function setProcessor(ICommandProcessor $processor = null)
  186. {
  187. $this->processor = $processor;
  188. }
  189. /**
  190. * {@inheritdoc}
  191. */
  192. public function getProcessor()
  193. {
  194. return $this->processor;
  195. }
  196. /**
  197. * Returns the version of server profile as its string representation.
  198. *
  199. * @return string
  200. */
  201. public function __toString()
  202. {
  203. return $this->getVersion();
  204. }
  205. }