Bläddra i källkod

Push initialization commands already created instances of Predis\IConnectionSingle.

Daniele Alessandri 14 år sedan
förälder
incheckning
5124fd1d43
1 ändrade filer med 16 tillägg och 12 borttagningar
  1. 16 12
      lib/Predis.php

+ 16 - 12
lib/Predis.php

@@ -94,33 +94,37 @@ class Client {
             $this->setConnection($cluster);
             $this->setConnection($cluster);
         }
         }
         else {
         else {
-            $this->setConnection($parameters instanceof IConnection
-                ? $parameters
-                : $this->createConnection($parameters)
-            );
+            $this->setConnection($this->createConnection($parameters));
         }
         }
     }
     }
 
 
     private function createConnection($parameters) {
     private function createConnection($parameters) {
+        $params = null;
+        $connection = null;
         if ($parameters instanceof IConnectionSingle) {
         if ($parameters instanceof IConnectionSingle) {
-            return $parameters;
+            $connection = $parameters;
+            $params = $connection->getParameters();
+        }
+        else {
+            $params = $parameters instanceof ConnectionParameters 
+                        ? $parameters 
+                        : new ConnectionParameters($parameters);
+            $connection = ConnectionFactory::create($params, $this->_responseReader);
         }
         }
-        $params     = $parameters instanceof ConnectionParameters 
-                          ? $parameters 
-                          : new ConnectionParameters($parameters);
-        $connection = ConnectionFactory::create($params, $this->_responseReader);
+        return $this->pushInitCommands($connection, $params);
+    }
 
 
-        if ($params->password !== null) {
+    private function pushInitCommands(IConnectionSingle $connection, ConnectionParameters $params) {
+        if (isset($params->password)) {
             $connection->pushInitCommand($this->createCommand(
             $connection->pushInitCommand($this->createCommand(
                 'auth', array($params->password)
                 'auth', array($params->password)
             ));
             ));
         }
         }
-        if ($params->database !== null) {
+        if (isset($params->database)) {
             $connection->pushInitCommand($this->createCommand(
             $connection->pushInitCommand($this->createCommand(
                 'select', array($params->database)
                 'select', array($params->database)
             ));
             ));
         }
         }
-
         return $connection;
         return $connection;
     }
     }