ServerProfile.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. return isset($this->_registeredCommands[$command]);
  62. }
  63. public function createCommand($method, $arguments = array()) {
  64. if (!isset($this->_registeredCommands[$method])) {
  65. throw new ClientException("'$method' is not a registered Redis command");
  66. }
  67. $commandClass = $this->_registeredCommands[$method];
  68. $command = new $commandClass();
  69. $command->setArguments($arguments);
  70. if (isset($this->_processor)) {
  71. $this->_processor->process($command);
  72. }
  73. return $command;
  74. }
  75. public function defineCommands(Array $commands) {
  76. foreach ($commands as $alias => $command) {
  77. $this->defineCommand($alias, $command);
  78. }
  79. }
  80. public function defineCommand($alias, $command) {
  81. $commandReflection = new \ReflectionClass($command);
  82. if (!$commandReflection->isSubclassOf('\Predis\Commands\ICommand')) {
  83. throw new ClientException("Cannot register '$command' as it is not a valid Redis command");
  84. }
  85. $this->_registeredCommands[$alias] = $command;
  86. }
  87. public function setProcessor(ICommandProcessor $processor) {
  88. if (!isset($processor)) {
  89. unset($this->_processor);
  90. return;
  91. }
  92. $this->_processor = $processor;
  93. }
  94. public function getProcessor() {
  95. return $this->_processor;
  96. }
  97. public function __toString() {
  98. return $this->getVersion();
  99. }
  100. }