ServerProfile.php 3.8 KB

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