Эх сурвалжийг харах

Implemented a factory method for creating server profiles instances.

Daniele Alessandri 15 жил өмнө
parent
commit
7fa935f827
2 өөрчлөгдсөн 23 нэмэгдсэн , 4 устгасан
  1. 22 3
      lib/Predis.php
  2. 1 1
      test/PredisShared.php

+ 22 - 3
lib/Predis.php

@@ -770,7 +770,7 @@ class ConnectionCluster implements IConnection, \IteratorAggregate {
 /* ------------------------------------------------------------------------- */
 
 abstract class RedisServerProfile {
-    const DEFAULT_SERVER_PROFILE = '\Predis\RedisServer_v1_2';
+    private static $_serverProfiles;
     private $_registeredCommands;
 
     public function __construct() {
@@ -782,8 +782,27 @@ abstract class RedisServerProfile {
     protected abstract function getSupportedCommands();
 
     public static function getDefault() {
-        $defaultProfile = self::DEFAULT_SERVER_PROFILE;
-        return new $defaultProfile();
+        return self::get('default');
+    }
+
+    private static function predisServerProfiles() {
+        return array(
+            '1.0'     => '\Predis\RedisServer_v1_0',
+            '1.2'     => '\Predis\RedisServer_v1_2',
+            'default' => '\Predis\RedisServer_v1_2',
+            'dev'     => '\Predis\RedisServer_vNext',
+        );
+    }
+
+    public static function get($version) {
+        if (!isset(self::$_serverProfiles)) {
+            self::$_serverProfiles = self::predisServerProfiles();
+        }
+        if (!isset(self::$_serverProfiles[$version])) {
+            throw new ClientException("Unknown server profile: $version");
+        }
+        $profile = self::$_serverProfiles[$version];
+        return new $profile();
     }
 
     public function compareWith($version, $operator = null) {

+ 1 - 1
test/PredisShared.php

@@ -26,7 +26,7 @@ class RC {
     private static $_connection;
 
     private static function createConnection() {
-        $serverProfile = new Predis\RedisServer_vNext();
+        $serverProfile = Predis\RedisServerProfile::get('dev');
         $connection = new Predis\Client(array('host' => RC::SERVER_HOST, 'port' => RC::SERVER_PORT), $serverProfile);
         $connection->connect();
         $connection->selectDatabase(RC::DEFAULT_DATABASE);