ServerProfile.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 define($alias, $profileClass) {
  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. self::$_profiles[$alias] = $profileClass;
  37. }
  38. public static function get($version) {
  39. if (!isset(self::$_profiles)) {
  40. self::$_profiles = self::getDefaultProfiles();
  41. }
  42. if (!isset(self::$_profiles[$version])) {
  43. throw new ClientException("Unknown server profile: $version");
  44. }
  45. $profile = self::$_profiles[$version];
  46. return new $profile();
  47. }
  48. public function supportsCommands(Array $commands) {
  49. foreach ($commands as $command) {
  50. if ($this->supportsCommand($command) === false) {
  51. return false;
  52. }
  53. }
  54. return true;
  55. }
  56. public function supportsCommand($command) {
  57. return isset($this->_registeredCommands[$command]);
  58. }
  59. public function createCommand($method, $arguments = array()) {
  60. if (!isset($this->_registeredCommands[$method])) {
  61. throw new ClientException("'$method' is not a registered Redis command");
  62. }
  63. $commandClass = $this->_registeredCommands[$method];
  64. $command = new $commandClass();
  65. $command->setArgumentsArray($arguments);
  66. return $command;
  67. }
  68. public function defineCommands(Array $commands) {
  69. foreach ($commands as $alias => $command) {
  70. $this->defineCommand($alias, $command);
  71. }
  72. }
  73. public function defineCommand($alias, $command) {
  74. $commandReflection = new \ReflectionClass($command);
  75. if (!$commandReflection->isSubclassOf('\Predis\Commands\ICommand')) {
  76. throw new ClientException("Cannot register '$command' as it is not a valid Redis command");
  77. }
  78. $this->_registeredCommands[$alias] = $command;
  79. }
  80. public function __toString() {
  81. return $this->getVersion();
  82. }
  83. }