瀏覽代碼

Change signature of the connection parameters class constructor.

Only named arrays are explicitly accepted now, but the old behaviour
of creating a connection parameters instance out of an URI string or
a named array is still available using the "create()" static method.

  $array = ['host' => '127.0.0.1', 'timeout' => 1];
  $uri = 'tcp://127.0.0.1?timeout=1';

  $parameters = new ConnectionParameters($array);      // Arrays only
  $parameters = ConnectionParameters::create($array);  // Arrays OK
  $parameters = ConnectionParameters::create($uri);    // Strings OK

The purpose of the change is to have a more concise constructor with a
well defined signature.
Daniele Alessandri 11 年之前
父節點
當前提交
b7ac7595c2

+ 4 - 0
CHANGELOG.md

@@ -55,6 +55,10 @@ v0.9.0 (201x-xx-xx)
   to create `AUTH` and `SELECT` commands when connection parameters contain both
   `password` and `database`. Raw commands will be used instead.
 
+- `ConnectionParameters::__construct()` only accepts named arrays, but instances
+  can be created using URIs or arrays using the `ConnectionParameters::create()`
+  static method.
+
 - Most classes and interfaces in the `Predis\Protocol` namespace have been moved
   or renamed while rationalizing the whole API of external protocol processors.
 

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

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

+ 20 - 8
lib/Predis/Connection/ConnectionParameters.php

@@ -38,15 +38,11 @@ class ConnectionParameters implements ConnectionParametersInterface
     );
 
     /**
-     * @param string|array Connection parameters in the form of an URI string or a named array.
+     * @param array Named array of connection parameters.
      */
-    public function __construct($parameters = null)
+    public function __construct(array $parameters = array())
     {
-        if (is_string($parameters)) {
-            $parameters = self::parse($parameters);
-        }
-
-        $this->parameters = $this->filter($parameters ?: array()) + $this->getDefaults();
+        $this->parameters = $this->filter($parameters) + $this->getDefaults();
     }
 
     /**
@@ -69,6 +65,22 @@ class ConnectionParameters implements ConnectionParametersInterface
         return self::$casters;
     }
 
+    /**
+     * Creates a new instance by supplying the initial parameters either in the
+     * form of an URI string or a named array.
+     *
+     * @param array|string $parameters Set of connection parameters.
+     * @return ConnectionParameters
+     */
+    public static function create($parameters)
+    {
+        if (is_string($parameters)) {
+            $parameters = self::parse($parameters);
+        }
+
+        return new self($parameters ?: array());
+    }
+
     /**
      * Parses an URI string returning an array of connection parameters.
      *
@@ -116,7 +128,7 @@ class ConnectionParameters implements ConnectionParametersInterface
             }
         }
 
-        return $parameters;
+        return $parameters ?: array();
     }
 
     /**

+ 31 - 18
tests/Predis/Connection/ConnectionParametersTest.php

@@ -44,13 +44,8 @@ class ParametersTest extends PredisTestCase
         $this->assertFalse(isset($parameters->unknown));
     }
 
-    /**
-     * @group disconnected
-     */
-    public function testUserDefinedParameters()
+    public function sharedTestsWithArrayParameters(ConnectionParameters $parameters)
     {
-        $parameters = new ConnectionParameters(array('port' => 7000, 'custom' => 'foobar'));
-
         $this->assertTrue(isset($parameters->scheme));
         $this->assertSame('tcp', $parameters->scheme);
 
@@ -67,7 +62,33 @@ class ParametersTest extends PredisTestCase
     /**
      * @group disconnected
      */
-    public function testConstructWithUriString()
+    public function testConstructWithArrayParameters()
+    {
+        $parameters = new ConnectionParameters(array(
+            'port' => 7000,
+            'custom' => 'foobar'
+        ));
+
+        $this->sharedTestsWithArrayParameters($parameters);
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testCreateWithArrayParameters()
+    {
+        $parameters = new ConnectionParameters(array(
+            'port' => 7000,
+            'custom' => 'foobar'
+        ));
+
+        $this->sharedTestsWithArrayParameters($parameters);
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testCreateWithUriString()
     {
         $defaults = $this->getDefaultParametersArray();
 
@@ -77,19 +98,11 @@ class ParametersTest extends PredisTestCase
             'custom' => 'foobar',
         );
 
-        $parameters = new ConnectionParameters($this->getParametersString($overrides));
-
-        $this->assertEquals($defaults['scheme'], $parameters->scheme);
-        $this->assertEquals($defaults['host'], $parameters->host);
-        $this->assertEquals($overrides['port'], $parameters->port);
+        $uriString = $this->getParametersString($overrides);
+        $parameters = ConnectionParameters::create($uriString);
 
+        $this->sharedTestsWithArrayParameters($parameters);
         $this->assertEquals($overrides['database'], $parameters->database);
-
-        $this->assertTrue(isset($parameters->custom));
-        $this->assertEquals($overrides['custom'], $parameters->custom);
-
-        $this->assertFalse(isset($parameters->unknown));
-        $this->assertNull($parameters->unknown);
     }
 
     /**

+ 1 - 1
tests/Predis/Connection/MasterSlaveReplicationTest.php

@@ -576,7 +576,7 @@ class MasterSlaveReplicationTest extends PredisTestCase
         $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
 
         if ($parameters) {
-            $parameters = new ConnectionParameters($parameters);
+            $parameters = ConnectionParameters::create($parameters);
             $hash = "{$parameters->host}:{$parameters->port}";
 
             $connection->expects($this->any())

+ 1 - 1
tests/Predis/Connection/PredisClusterTest.php

@@ -388,7 +388,7 @@ class PredisClusterTest extends PredisTestCase
         $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
 
         if ($parameters) {
-            $parameters = new ConnectionParameters($parameters);
+            $parameters = ConnectionParameters::create($parameters);
             $hash = "{$parameters->host}:{$parameters->port}";
 
             $connection->expects($this->any())

+ 1 - 1
tests/Predis/Connection/RedisClusterTest.php

@@ -615,7 +615,7 @@ class RedisClusterTest extends PredisTestCase
         $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
 
         if ($parameters) {
-            $parameters = new ConnectionParameters($parameters);
+            $parameters = ConnectionParameters::create($parameters);
             $hash = "{$parameters->host}:{$parameters->port}";
 
             $connection->expects($this->any())