ServerProfile.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace Predis\Profiles;
  3. use Predis\ClientException;
  4. use Predis\Commands\Processors\ICommandProcessor;
  5. use Predis\Commands\Processors\IProcessingSupport;
  6. abstract class ServerProfile implements IServerProfile, IProcessingSupport {
  7. private static $_profiles;
  8. private $_registeredCommands;
  9. private $_processor;
  10. public function __construct() {
  11. $this->_registeredCommands = $this->getSupportedCommands();
  12. }
  13. protected abstract function getSupportedCommands();
  14. public static function getDefault() {
  15. return self::get('default');
  16. }
  17. public static function getDevelopment() {
  18. return self::get('dev');
  19. }
  20. private static function getDefaultProfiles() {
  21. return array(
  22. '1.2' => '\Predis\Profiles\ServerVersion12',
  23. '2.0' => '\Predis\Profiles\ServerVersion20',
  24. '2.2' => '\Predis\Profiles\ServerVersion22',
  25. '2.4' => '\Predis\Profiles\ServerVersion24',
  26. 'default' => '\Predis\Profiles\ServerVersion22',
  27. 'dev' => '\Predis\Profiles\ServerVersionNext',
  28. );
  29. }
  30. public static function define($alias, $profileClass) {
  31. if (!isset(self::$_profiles)) {
  32. self::$_profiles = self::getDefaultProfiles();
  33. }
  34. $profileReflection = new \ReflectionClass($profileClass);
  35. if (!$profileReflection->isSubclassOf('\Predis\Profiles\IServerProfile')) {
  36. throw new ClientException(
  37. "Cannot register '$profileClass' as it is not a valid profile class"
  38. );
  39. }
  40. self::$_profiles[$alias] = $profileClass;
  41. }
  42. public static function get($version) {
  43. if (!isset(self::$_profiles)) {
  44. self::$_profiles = self::getDefaultProfiles();
  45. }
  46. if (!isset(self::$_profiles[$version])) {
  47. throw new ClientException("Unknown server profile: $version");
  48. }
  49. $profile = self::$_profiles[$version];
  50. return new $profile();
  51. }
  52. public function supportsCommands(Array $commands) {
  53. foreach ($commands as $command) {
  54. if ($this->supportsCommand($command) === false) {
  55. return false;
  56. }
  57. }
  58. return true;
  59. }
  60. public function supportsCommand($command) {
  61. $command = strtolower($command);
  62. return isset($this->_registeredCommands[$command]);
  63. }
  64. public function createCommand($method, $arguments = array()) {
  65. $method = strtolower($method);
  66. if (!isset($this->_registeredCommands[$method])) {
  67. throw new ClientException("'$method' is not a registered Redis command");
  68. }
  69. $commandClass = $this->_registeredCommands[$method];
  70. $command = new $commandClass();
  71. $command->setArguments($arguments);
  72. if (isset($this->_processor)) {
  73. $this->_processor->process($command);
  74. }
  75. return $command;
  76. }
  77. public function defineCommands(Array $commands) {
  78. foreach ($commands as $alias => $command) {
  79. $this->defineCommand($alias, $command);
  80. }
  81. }
  82. public function defineCommand($alias, $command) {
  83. $commandReflection = new \ReflectionClass($command);
  84. if (!$commandReflection->isSubclassOf('\Predis\Commands\ICommand')) {
  85. throw new ClientException("Cannot register '$command' as it is not a valid Redis command");
  86. }
  87. $this->_registeredCommands[$alias] = $command;
  88. }
  89. public function setProcessor(ICommandProcessor $processor) {
  90. if (!isset($processor)) {
  91. unset($this->_processor);
  92. return;
  93. }
  94. $this->_processor = $processor;
  95. }
  96. public function getProcessor() {
  97. return $this->_processor;
  98. }
  99. public function __toString() {
  100. return $this->getVersion();
  101. }
  102. }