Przeglądaj źródła

Initialize client options in a lazy way.

Daniele Alessandri 14 lat temu
rodzic
commit
893618c985
2 zmienionych plików z 27 dodań i 12 usunięć
  1. 4 3
      lib/Predis/Client.php
  2. 23 9
      lib/Predis/ClientOptions.php

+ 4 - 3
lib/Predis/Client.php

@@ -15,10 +15,11 @@ class Client {
     public function __construct($parameters = null, $options = null) {
         $options = $this->filterOptions($options);
         $this->_options = $options;
-        $this->_profile = $options->profile;
-        if ($prefixer = $options->prefix) {
-            $this->_profile->setProcessor($prefixer);
+        $profile = $options->profile;
+        if (isset($options->prefix)) {
+            $profile->setProcessor($options->prefix);
         }
+        $this->_profile = $profile;
         $this->_connectionFactory = $options->connections;
         $this->_connection = $this->initializeConnection($parameters);
     }

+ 23 - 9
lib/Predis/ClientOptions.php

@@ -9,11 +9,14 @@ use Predis\Options\ClientCluster;
 use Predis\Options\ClientConnectionFactory;
 
 class ClientOptions {
-    private $_handlers, $_options;
     private static $_sharedOptions;
+    private $_options = array();
+    private $_handlers;
+    private $_defined;
 
     public function __construct(Array $options = array()) {
-        $this->initialize($options);
+        $this->_handlers = $this->initialize($options);
+        $this->_defined = array_keys($options);
     }
 
     private static function getSharedOptions() {
@@ -40,13 +43,20 @@ class ClientOptions {
     }
 
     private function initialize($options) {
-        $this->_handlers = self::getSharedOptions();
+        $handlers = self::getSharedOptions();
         foreach ($options as $option => $value) {
-            if (isset($this->_handlers[$option])) {
-                $handler = $this->_handlers[$option];
-                $this->_options[$option] = $handler($value);
+            if (isset($handlers[$option])) {
+                $handler = $handlers[$option];
+                $handlers[$option] = function() use($handler, $value) {
+                    return $handler->validate($value);
+                };
             }
         }
+        return $handlers;
+    }
+
+    public function __isset($option) {
+        return in_array($option, $this->_defined);
     }
 
     public function __get($option) {
@@ -54,11 +64,15 @@ class ClientOptions {
             return $this->_options[$option];
         }
         if (isset($this->_handlers[$option])) {
-            $opts = self::getSharedOptions();
-            $value = $opts[$option]->getDefault();
+            $handler = $this->_handlers[$option];
+            if ($handler instanceof IOption) {
+                $value = $handler->getDefault();
+            }
+            else {
+                $value = $handler();
+            }
             $this->_options[$option] = $value;
             return $value;
         }
-        return null;
     }
 }