ServerProfile.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Profiles;
  11. use Predis\ClientException;
  12. use Predis\Commands\Processors\ICommandProcessor;
  13. use Predis\Commands\Processors\IProcessingSupport;
  14. abstract class ServerProfile implements IServerProfile, IProcessingSupport
  15. {
  16. private static $_profiles;
  17. private $_registeredCommands;
  18. private $_processor;
  19. public function __construct()
  20. {
  21. $this->_registeredCommands = $this->getSupportedCommands();
  22. }
  23. protected abstract function getSupportedCommands();
  24. public static function getDefault()
  25. {
  26. return self::get('default');
  27. }
  28. public static function getDevelopment()
  29. {
  30. return self::get('dev');
  31. }
  32. private static function getDefaultProfiles()
  33. {
  34. return array(
  35. '1.2' => '\Predis\Profiles\ServerVersion12',
  36. '2.0' => '\Predis\Profiles\ServerVersion20',
  37. '2.2' => '\Predis\Profiles\ServerVersion22',
  38. '2.4' => '\Predis\Profiles\ServerVersion24',
  39. 'default' => '\Predis\Profiles\ServerVersion24',
  40. 'dev' => '\Predis\Profiles\ServerVersionNext',
  41. );
  42. }
  43. public static function define($alias, $profileClass)
  44. {
  45. if (!isset(self::$_profiles)) {
  46. self::$_profiles = self::getDefaultProfiles();
  47. }
  48. $profileReflection = new \ReflectionClass($profileClass);
  49. if (!$profileReflection->isSubclassOf('\Predis\Profiles\IServerProfile')) {
  50. throw new ClientException(
  51. "Cannot register '$profileClass' as it is not a valid profile class"
  52. );
  53. }
  54. self::$_profiles[$alias] = $profileClass;
  55. }
  56. public static function get($version)
  57. {
  58. if (!isset(self::$_profiles)) {
  59. self::$_profiles = self::getDefaultProfiles();
  60. }
  61. if (!isset(self::$_profiles[$version])) {
  62. throw new ClientException("Unknown server profile: $version");
  63. }
  64. $profile = self::$_profiles[$version];
  65. return new $profile();
  66. }
  67. public function supportsCommands(Array $commands)
  68. {
  69. foreach ($commands as $command) {
  70. if ($this->supportsCommand($command) === false) {
  71. return false;
  72. }
  73. }
  74. return true;
  75. }
  76. public function supportsCommand($command)
  77. {
  78. return isset($this->_registeredCommands[strtolower($command)]);
  79. }
  80. public function createCommand($method, $arguments = array())
  81. {
  82. $method = strtolower($method);
  83. if (!isset($this->_registeredCommands[$method])) {
  84. throw new ClientException("'$method' is not a registered Redis command");
  85. }
  86. $commandClass = $this->_registeredCommands[$method];
  87. $command = new $commandClass();
  88. $command->setArguments($arguments);
  89. if (isset($this->_processor)) {
  90. $this->_processor->process($command);
  91. }
  92. return $command;
  93. }
  94. public function defineCommands(Array $commands)
  95. {
  96. foreach ($commands as $alias => $command) {
  97. $this->defineCommand($alias, $command);
  98. }
  99. }
  100. public function defineCommand($alias, $command)
  101. {
  102. $commandReflection = new \ReflectionClass($command);
  103. if (!$commandReflection->isSubclassOf('\Predis\Commands\ICommand')) {
  104. throw new ClientException("Cannot register '$command' as it is not a valid Redis command");
  105. }
  106. $this->_registeredCommands[strtolower($alias)] = $command;
  107. }
  108. public function setProcessor(ICommandProcessor $processor)
  109. {
  110. if (!isset($processor)) {
  111. unset($this->_processor);
  112. return;
  113. }
  114. $this->_processor = $processor;
  115. }
  116. public function getProcessor()
  117. {
  118. return $this->_processor;
  119. }
  120. public function __toString()
  121. {
  122. return $this->getVersion();
  123. }
  124. }