Przeglądaj źródła

Implement new various changes.

Daniele Alessandri 14 lat temu
rodzic
commit
b11ab53bb1

+ 14 - 23
lib/Predis/Client.php

@@ -38,7 +38,7 @@ class Client {
     }
 
     private function initializeConnection($parameters = array()) {
-        if (!isset($parameters)) {
+        if ($parameters === null) {
             return $this->createConnection(array());
         }
         if ($parameters instanceof IConnection) {
@@ -57,21 +57,11 @@ class Client {
     }
 
     private function createConnection($parameters) {
-        if (is_array($parameters) || is_string($parameters)) {
-            $parameters = new ConnectionParameters($parameters);
-        }
-        else if (!$parameters instanceof ConnectionParameters) {
-            $type = is_object($parameters) ? get_class($parameters) : gettype($parameters);
-            throw new \InvalidArgumentException(
-                "Cannot create a connection using an argument of type $type"
-            );
-        }
-
-        $connection = self::newConnectionInternal($parameters);
+        $connection = self::newConnection($parameters);
         $this->pushInitCommands($connection);
 
         $callback = $this->_options->on_connection_initialized;
-        if (isset($callback)) {
+        if ($callback !== null) {
             $callback($this, $connection);
         }
 
@@ -117,9 +107,7 @@ class Client {
     }
 
     public function connect() {
-        if (!$this->_connection->isConnected()) {
-            $this->_connection->connect();
-        }
+        $this->_connection->connect();
     }
 
     public function disconnect() {
@@ -135,10 +123,10 @@ class Client {
     }
 
     public function getConnection($id = null) {
-        $connection = $this->_connection;
-        if (!isset($id)) {
-            return $connection;
+        if ($id === null) {
+            return $this->_connection;
         }
+        $connection = $this->_connection;
         $isCluster = Utils::isCluster($connection);
         return $isCluster ? $connection->getConnectionById($id) : $connection;
     }
@@ -265,10 +253,13 @@ class Client {
     }
 
     public static function newConnectionByScheme($scheme, $parameters = array()) {
-        $connection = self::getConnectionClass($scheme);
-        if (!$parameters instanceof ConnectionParameters) {
-            $parameters = new ConnectionParameters($parameters);
+        if ($parameters instanceof ConnectionParameters) {
+            $parameters = $parameters->toArray();
+        }
+        if (is_array($parameters)) {
+            $parameters['scheme'] = $scheme;
+            return self::newConnection($parameters);
         }
-        return self::newConnection($parameters);
+        throw new \InvalidArgumentException("Invalid type for connection parameters");
     }
 }

+ 3 - 3
lib/Predis/ClientOptions.php

@@ -6,8 +6,8 @@ class ClientOptions {
     private $_handlers, $_options;
     private static $_sharedOptions;
 
-    public function __construct($options = null) {
-        $this->initialize($options ?: array());
+    public function __construct(Array $options = array()) {
+        $this->initialize($options);
     }
 
     private static function getSharedOptions() {
@@ -19,7 +19,7 @@ class ClientOptions {
             'key_distribution' => new Options\ClientKeyDistribution(),
             'on_connection_initialized' => new Options\CustomOption(array(
                 'validate' => function($value) {
-                    if (isset($value) && is_callable($value)) {
+                    if (is_callable($value)) {
                         return $value;
                     }
                 },

+ 10 - 2
lib/Predis/ConnectionParameters.php

@@ -89,7 +89,7 @@ class ConnectionParameters {
             $uri = str_ireplace('unix:///', 'unix://localhost/', $uri);
         }
         $parsed = @parse_url($uri);
-        if ($parsed == false || !isset($parsed['host'])) {
+        if ($parsed === false || !isset($parsed['host'])) {
             throw new \InvalidArgumentException("Invalid URI: $uri");
         }
         if (array_key_exists('query', $parsed)) {
@@ -100,7 +100,7 @@ class ConnectionParameters {
         return $this->filter($parsed);
     }
 
-    protected function filter($parameters) {
+    protected function filter(Array $parameters) {
         $handlers = self::getSharedOptions();
         foreach ($parameters as $parameter => $value) {
             if (isset($handlers[$parameter])) {
@@ -148,4 +148,12 @@ class ConnectionParameters {
         }
         return count($query) > 0 ? ($str . '/?' . implode('&', $query)) : $str;
     }
+
+    public function toArray() {
+        $parameters = array();
+        foreach ($this->_parameters as $k => $v) {
+            $parameters[$k] = $v;
+        }
+        return $parameters;
+    }
 }