Browse Source

Rework how connection parameters are handled.

These changes do not affect the actual functionalities of the client,
but make the code more explicit and less error-prone.
Daniele Alessandri 11 năm trước cách đây
mục cha
commit
3c6389f4dd

+ 27 - 11
lib/Predis/Client.php

@@ -19,6 +19,7 @@ use Predis\Configuration\OptionsInterface;
 use Predis\Connection\AggregatedConnectionInterface;
 use Predis\Connection\ConnectionInterface;
 use Predis\Connection\ConnectionFactoryInterface;
+use Predis\Connection\ConnectionParametersInterface;
 use Predis\Monitor\MonitorContext;
 use Predis\Pipeline\PipelineContext;
 use Predis\Profile\ServerProfile;
@@ -48,8 +49,8 @@ class Client implements ClientInterface
      */
     public function __construct($parameters = null, $options = null)
     {
-        $this->options    = $this->createOptions($options);
-        $this->connection = $this->createConnection($parameters);
+        $this->options    = $this->createOptions($options ?: array());
+        $this->connection = $this->createConnection($parameters ?: array());
         $this->profile    = $this->options->profile;
     }
 
@@ -63,10 +64,6 @@ class Client implements ClientInterface
      */
     protected function createOptions($options)
     {
-        if (!isset($options)) {
-            return new Options();
-        }
-
         if (is_array($options)) {
             return new Options($options);
         }
@@ -83,6 +80,14 @@ class Client implements ClientInterface
      * (string, array) or returns the passed argument if it is an instance of a
      * class implementing Predis\Connection\ConnectionInterface.
      *
+     * Accepted types for connection parameters are:
+     *
+     *  - Instance of Predis\Connection\ConnectionInterface.
+     *  - Instance of Predis\Connection\ConnectionParametersInterface.
+     *  - Array
+     *  - String
+     *  - Callable
+     *
      * @param mixed $parameters Connection parameters or connection instance.
      * @return ConnectionInterface
      */
@@ -92,12 +97,23 @@ class Client implements ClientInterface
             return $parameters;
         }
 
-        if (is_array($parameters) && isset($parameters[0])) {
+        if ($parameters instanceof ConnectionParametersInterface || is_string($parameters)) {
+            return $this->options->connections->create($parameters);
+        }
+
+        if (is_array($parameters)) {
             $options = $this->options;
-            $replication = isset($options->replication) && $options->replication;
-            $connection = $options->{$replication ? 'replication' : 'cluster'};
 
-            return $options->connections->createAggregated($connection, $parameters);
+            if (isset($parameters[0])) {
+                $replication = isset($options->replication) && $options->replication;
+                $connection = $options->{$replication ? 'replication' : 'cluster'};
+
+                $options->connections->createAggregated($connection, $parameters);
+
+                return $connection;
+            }
+
+            return $options->connections->create($parameters);
         }
 
         if (is_callable($parameters)) {
@@ -112,7 +128,7 @@ class Client implements ClientInterface
             return $connection;
         }
 
-        return $this->options->connections->create($parameters);
+        throw new InvalidArgumentException('Invalid type for connection parameters');
     }
 
     /**

+ 1 - 1
lib/Predis/Connection/ConnectionFactory.php

@@ -97,7 +97,7 @@ class ConnectionFactory implements ConnectionFactoryInterface
     public function create($parameters)
     {
         if (!$parameters instanceof ConnectionParametersInterface) {
-            $parameters = new ConnectionParameters($parameters ?: array());
+            $parameters = new ConnectionParameters($parameters);
         }
 
         $scheme = $parameters->scheme;

+ 3 - 3
lib/Predis/Connection/ConnectionParameters.php

@@ -32,13 +32,13 @@ class ConnectionParameters implements ConnectionParametersInterface
     /**
      * @param string|array Connection parameters in the form of an URI string or a named array.
      */
-    public function __construct($parameters = array())
+    public function __construct($parameters = null)
     {
-        if (!is_array($parameters)) {
+        if (is_string($parameters)) {
             $parameters = self::parseURI($parameters);
         }
 
-        $this->parameters = $this->filter($parameters) + $this->getDefaults();
+        $this->parameters = $this->filter($parameters ?: array()) + $this->getDefaults();
     }
 
     /**