ServerProfile.php 3.5 KB

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