ServerProfile.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. 'default' => '\Predis\Profiles\ServerVersion22',
  26. 'dev' => '\Predis\Profiles\ServerVersionNext',
  27. );
  28. }
  29. public static function define($alias, $profileClass) {
  30. if (!isset(self::$_profiles)) {
  31. self::$_profiles = self::getDefaultProfiles();
  32. }
  33. $profileReflection = new \ReflectionClass($profileClass);
  34. if (!$profileReflection->isSubclassOf('\Predis\Profiles\IServerProfile')) {
  35. throw new ClientException(
  36. "Cannot register '$profileClass' as it is not a valid profile class"
  37. );
  38. }
  39. self::$_profiles[$alias] = $profileClass;
  40. }
  41. public static function get($version) {
  42. if (!isset(self::$_profiles)) {
  43. self::$_profiles = self::getDefaultProfiles();
  44. }
  45. if (!isset(self::$_profiles[$version])) {
  46. throw new ClientException("Unknown server profile: $version");
  47. }
  48. $profile = self::$_profiles[$version];
  49. return new $profile();
  50. }
  51. public function supportsCommands(Array $commands) {
  52. foreach ($commands as $command) {
  53. if ($this->supportsCommand($command) === false) {
  54. return false;
  55. }
  56. }
  57. return true;
  58. }
  59. public function supportsCommand($command) {
  60. return isset($this->_registeredCommands[$command]);
  61. }
  62. public function createCommand($method, $arguments = array()) {
  63. if (!isset($this->_registeredCommands[$method])) {
  64. throw new ClientException("'$method' is not a registered Redis command");
  65. }
  66. $commandClass = $this->_registeredCommands[$method];
  67. $command = new $commandClass();
  68. $command->setArguments($arguments);
  69. if (isset($this->_processor)) {
  70. $this->_processor->process($command);
  71. }
  72. return $command;
  73. }
  74. public function defineCommands(Array $commands) {
  75. foreach ($commands as $alias => $command) {
  76. $this->defineCommand($alias, $command);
  77. }
  78. }
  79. public function defineCommand($alias, $command) {
  80. $commandReflection = new \ReflectionClass($command);
  81. if (!$commandReflection->isSubclassOf('\Predis\Commands\ICommand')) {
  82. throw new ClientException("Cannot register '$command' as it is not a valid Redis command");
  83. }
  84. $this->_registeredCommands[$alias] = $command;
  85. }
  86. public function setProcessor(ICommandProcessor $processor) {
  87. if (!isset($processor)) {
  88. unset($this->_processor);
  89. return;
  90. }
  91. $this->_processor = $processor;
  92. }
  93. public function getProcessor() {
  94. return $this->_processor;
  95. }
  96. public function __toString() {
  97. return $this->getVersion();
  98. }
  99. }