ServerProfile.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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\Profile;
  11. use Predis\ClientException;
  12. use Predis\Command\Processor\CommandProcessingInterface;
  13. use Predis\Command\Processor\CommandProcessorInterface;
  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 ServerProfileInterface, CommandProcessingInterface
  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. abstract protected function getSupportedCommands();
  38. /**
  39. * Returns the default server profile.
  40. *
  41. * @return ServerProfileInterface
  42. */
  43. public static function getDefault()
  44. {
  45. return self::get('default');
  46. }
  47. /**
  48. * Returns the development server profile.
  49. *
  50. * @return ServerProfileInterface
  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\Profile\ServerVersion12',
  66. '2.0' => 'Predis\Profile\ServerVersion20',
  67. '2.2' => 'Predis\Profile\ServerVersion22',
  68. '2.4' => 'Predis\Profile\ServerVersion24',
  69. '2.6' => 'Predis\Profile\ServerVersion26',
  70. '2.8' => 'Predis\Profile\ServerVersion28',
  71. '3.0' => 'Predis\Profile\ServerVersion30',
  72. 'default' => 'Predis\Profile\ServerVersion28',
  73. 'dev' => 'Predis\Profile\ServerVersionNext',
  74. );
  75. }
  76. /**
  77. * Registers a new server profile.
  78. *
  79. * @param string $alias Profile version or alias.
  80. * @param string $profileClass FQN of a class implementing Predis\Profile\ServerProfileInterface.
  81. */
  82. public static function define($alias, $profileClass)
  83. {
  84. if (!isset(self::$profiles)) {
  85. self::$profiles = self::getDefaultProfiles();
  86. }
  87. $profileReflection = new \ReflectionClass($profileClass);
  88. if (!$profileReflection->isSubclassOf('Predis\Profile\ServerProfileInterface')) {
  89. throw new \InvalidArgumentException("Cannot register '$profileClass' as it is not a valid profile class");
  90. }
  91. self::$profiles[$alias] = $profileClass;
  92. }
  93. /**
  94. * Returns the specified server profile.
  95. *
  96. * @param string $version Profile version or alias.
  97. * @return ServerProfileInterface
  98. */
  99. public static function get($version)
  100. {
  101. if (!isset(self::$profiles)) {
  102. self::$profiles = self::getDefaultProfiles();
  103. }
  104. if (!isset(self::$profiles[$version])) {
  105. throw new ClientException("Unknown server profile: $version");
  106. }
  107. $profile = self::$profiles[$version];
  108. return new $profile();
  109. }
  110. /**
  111. * {@inheritdoc}
  112. */
  113. public function supportsCommands(Array $commands)
  114. {
  115. foreach ($commands as $command) {
  116. if (!$this->supportsCommand($command)) {
  117. return false;
  118. }
  119. }
  120. return true;
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function supportsCommand($command)
  126. {
  127. return isset($this->commands[strtolower($command)]);
  128. }
  129. /**
  130. * Returns the FQN of the class that represent the specified command ID
  131. * registered in the current server profile.
  132. *
  133. * @param string $command Command ID.
  134. * @return string
  135. */
  136. public function getCommandClass($command)
  137. {
  138. if (isset($this->commands[$command = strtolower($command)])) {
  139. return $this->commands[$command];
  140. }
  141. }
  142. /**
  143. * {@inheritdoc}
  144. */
  145. public function createCommand($method, $arguments = array())
  146. {
  147. $method = strtolower($method);
  148. if (!isset($this->commands[$method])) {
  149. throw new ClientException("'$method' is not a registered Redis command");
  150. }
  151. $commandClass = $this->commands[$method];
  152. $command = new $commandClass();
  153. $command->setArguments($arguments);
  154. if (isset($this->processor)) {
  155. $this->processor->process($command);
  156. }
  157. return $command;
  158. }
  159. /**
  160. * Defines a new commands in the server profile.
  161. *
  162. * @param string $alias Command ID.
  163. * @param string $command FQN of a class implementing Predis\Command\CommandInterface.
  164. */
  165. public function defineCommand($alias, $command)
  166. {
  167. $commandReflection = new \ReflectionClass($command);
  168. if (!$commandReflection->isSubclassOf('Predis\Command\CommandInterface')) {
  169. throw new \InvalidArgumentException("Cannot register '$command' as it is not a valid Redis command");
  170. }
  171. $this->commands[strtolower($alias)] = $command;
  172. }
  173. /**
  174. * {@inheritdoc}
  175. */
  176. public function setProcessor(CommandProcessorInterface $processor = null)
  177. {
  178. $this->processor = $processor;
  179. }
  180. /**
  181. * {@inheritdoc}
  182. */
  183. public function getProcessor()
  184. {
  185. return $this->processor;
  186. }
  187. /**
  188. * Returns the version of server profile as its string representation.
  189. *
  190. * @return string
  191. */
  192. public function __toString()
  193. {
  194. return $this->getVersion();
  195. }
  196. }