Browse Source

Change how the key prefix processor is initialized.

When the argument of 'profile' is an instance of Predis\Profiles\IServerProfile
the value of the 'prefix' option is ignored, which means that it is up to the
user to initialize the appropriate prefix processor.
Daniele Alessandri 13 năm trước cách đây
mục cha
commit
4a8c678561
2 tập tin đã thay đổi với 16 bổ sung18 xóa
  1. 2 10
      lib/Predis/Client.php
  2. 14 8
      lib/Predis/Options/ClientProfile.php

+ 2 - 10
lib/Predis/Client.php

@@ -46,13 +46,8 @@ class Client
     {
         $options = $this->filterOptions($options);
 
-        $profile = $options->profile;
-        if (isset($options->prefix)) {
-            $profile->setProcessor($options->prefix);
-        }
-
         $this->options = $options;
-        $this->profile = $profile;
+        $this->profile = $options->profile;
         $this->connections = $options->connections;
 
         $this->connection = $this->initializeConnection($parameters);
@@ -77,12 +72,9 @@ class Client
         if ($options instanceof IClientOptions) {
             return $options;
         }
-        if ($options instanceof IServerProfile) {
+        if ($options instanceof IServerProfile || is_string($options)) {
             return new ClientOptions(array('profile' => $options));
         }
-        if (is_string($options)) {
-            return new ClientOptions(array('profile' => ServerProfile::get($options)));
-        }
 
         throw new \InvalidArgumentException("Invalid type for client options");
     }

+ 14 - 8
lib/Predis/Options/ClientProfile.php

@@ -26,17 +26,18 @@ class ClientProfile extends Option
      */
     public function validate(IClientOptions $options, $value)
     {
-        if ($value instanceof IServerProfile) {
-            return $value;
+        if (is_string($value)) {
+            $value = ServerProfile::get($value);
+            if (isset($options->prefix)) {
+                $value->setProcessor($options->prefix);
+            }
         }
 
-        if (is_string($value)) {
-            return ServerProfile::get($value);
+        if (!$value instanceof IServerProfile) {
+            throw new \InvalidArgumentException('Invalid value for the profile option');
         }
 
-        throw new \InvalidArgumentException(
-            "Invalid value for the profile option"
-        );
+        return $value;
     }
 
     /**
@@ -44,6 +45,11 @@ class ClientProfile extends Option
      */
     public function getDefault(IClientOptions $options)
     {
-        return ServerProfile::getDefault();
+        $profile = ServerProfile::getDefault();
+        if (isset($options->prefix)) {
+            $profile->setProcessor($options->prefix);
+        }
+
+        return $profile;
     }
 }