123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- <?php
- namespace Predis\Profiles;
- use Predis\ClientException;
- use Predis\Commands\Processors\ICommandProcessor;
- use Predis\Commands\Processors\IProcessingSupport;
- abstract class ServerProfile implements IServerProfile, IProcessingSupport {
- private static $_profiles;
- private $_registeredCommands;
- private $_processor;
- public function __construct() {
- $this->_registeredCommands = $this->getSupportedCommands();
- }
- protected abstract function getSupportedCommands();
- public static function getDefault() {
- return self::get('default');
- }
- public static function getDevelopment() {
- return self::get('dev');
- }
- private static function getDefaultProfiles() {
- return array(
- '1.2' => '\Predis\Profiles\ServerVersion12',
- '2.0' => '\Predis\Profiles\ServerVersion20',
- '2.2' => '\Predis\Profiles\ServerVersion22',
- 'default' => '\Predis\Profiles\ServerVersion22',
- 'dev' => '\Predis\Profiles\ServerVersionNext',
- );
- }
- public static function define($alias, $profileClass) {
- if (!isset(self::$_profiles)) {
- self::$_profiles = self::getDefaultProfiles();
- }
- $profileReflection = new \ReflectionClass($profileClass);
- if (!$profileReflection->isSubclassOf('\Predis\Profiles\IServerProfile')) {
- throw new ClientException(
- "Cannot register '$profileClass' as it is not a valid profile class"
- );
- }
- self::$_profiles[$alias] = $profileClass;
- }
- public static function get($version) {
- if (!isset(self::$_profiles)) {
- self::$_profiles = self::getDefaultProfiles();
- }
- if (!isset(self::$_profiles[$version])) {
- throw new ClientException("Unknown server profile: $version");
- }
- $profile = self::$_profiles[$version];
- return new $profile();
- }
- public function supportsCommands(Array $commands) {
- foreach ($commands as $command) {
- if ($this->supportsCommand($command) === false) {
- return false;
- }
- }
- return true;
- }
- public function supportsCommand($command) {
- return isset($this->_registeredCommands[$command]);
- }
- public function createCommand($method, $arguments = array()) {
- if (!isset($this->_registeredCommands[$method])) {
- throw new ClientException("'$method' is not a registered Redis command");
- }
- $commandClass = $this->_registeredCommands[$method];
- $command = new $commandClass();
- $command->setArguments($arguments);
- if (isset($this->_processor)) {
- $this->_processor->process($command);
- }
- return $command;
- }
- public function defineCommands(Array $commands) {
- foreach ($commands as $alias => $command) {
- $this->defineCommand($alias, $command);
- }
- }
- public function defineCommand($alias, $command) {
- $commandReflection = new \ReflectionClass($command);
- if (!$commandReflection->isSubclassOf('\Predis\Commands\ICommand')) {
- throw new ClientException("Cannot register '$command' as it is not a valid Redis command");
- }
- $this->_registeredCommands[$alias] = $command;
- }
- public function setProcessor(ICommandProcessor $processor) {
- if (!isset($processor)) {
- unset($this->_processor);
- return;
- }
- $this->_processor = $processor;
- }
- public function getProcessor() {
- return $this->_processor;
- }
- public function __toString() {
- return $this->getVersion();
- }
- }
|