Procházet zdrojové kódy

Remove some overkill stuff from Predis\ConnectionParameters.

It was basically some crazy and almost useless stuff I'd say.
Daniele Alessandri před 13 roky
rodič
revize
a3b4c2244a

+ 2 - 2
lib/Predis/Connection/PhpiredisConnection.php

@@ -73,10 +73,10 @@ class PhpiredisConnection extends AbstractConnection
      */
     protected function checkParameters(ConnectionParametersInterface $parameters)
     {
-        if ($parameters->isSetByUser('iterable_multibulk')) {
+        if ($parameters->iterable_multibulk === true) {
             $this->onInvalidOption('iterable_multibulk', $parameters);
         }
-        if ($parameters->isSetByUser('persistent')) {
+        if ($parameters->persistent === true) {
             $this->onInvalidOption('persistent', $parameters);
         }
 

+ 5 - 5
lib/Predis/Connection/StreamConnection.php

@@ -46,8 +46,8 @@ class StreamConnection extends AbstractConnection
      */
     protected function initializeProtocol(ConnectionParametersInterface $parameters)
     {
-        $this->throwErrors = $parameters->throw_errors;
-        $this->mbiterable = $parameters->iterable_multibulk;
+        $this->throwErrors = (bool) $parameters->throw_errors;
+        $this->mbiterable = (bool) $parameters->iterable_multibulk;
     }
 
     /**
@@ -72,10 +72,10 @@ class StreamConnection extends AbstractConnection
         $uri = "tcp://{$parameters->host}:{$parameters->port}/";
 
         $flags = STREAM_CLIENT_CONNECT;
-        if ($parameters->async_connect) {
+        if (isset($parameters->async_connect) && $parameters->async_connect === true) {
             $flags |= STREAM_CLIENT_ASYNC_CONNECT;
         }
-        if ($parameters->persistent) {
+        if (isset($parameters->persistent) && $parameters->persistent === true) {
             $flags |= STREAM_CLIENT_PERSISTENT;
         }
 
@@ -109,7 +109,7 @@ class StreamConnection extends AbstractConnection
         $uri = "unix://{$parameters->path}";
 
         $flags = STREAM_CLIENT_CONNECT;
-        if ($parameters->persistent) {
+        if ($parameters->persistent === true) {
             $flags |= STREAM_CLIENT_PERSISTENT;
         }
 

+ 34 - 154
lib/Predis/ConnectionParameters.php

@@ -20,108 +20,59 @@ use Predis\Option\OptionInterface;
  */
 class ConnectionParameters implements ConnectionParametersInterface
 {
-    private static $defaultParameters;
-    private static $validators;
-
     private $parameters;
-    private $userDefined;
+
+    private static $defaults = array(
+        'scheme' => 'tcp',
+        'host' => '127.0.0.1',
+        'port' => 6379,
+        'timeout' => 5.0,
+        'iterable_multibulk' => false,
+        'throw_errors' => true,
+    );
 
     /**
      * @param string|array Connection parameters in the form of an URI string or a named array.
      */
     public function __construct($parameters = array())
     {
-        self::ensureDefaults();
-
         if (!is_array($parameters)) {
             $parameters = $this->parseURI($parameters);
         }
 
-        $this->userDefined = array_keys($parameters);
-        $this->parameters = $this->filter($parameters) + self::$defaultParameters;
-    }
-
-    /**
-     * Ensures that the default values and validators are initialized.
-     */
-    private static function ensureDefaults()
-    {
-        if (!isset(self::$defaultParameters)) {
-            self::$defaultParameters = array(
-                'scheme' => 'tcp',
-                'host' => '127.0.0.1',
-                'port' => 6379,
-                'database' => null,
-                'password' => null,
-                'async_connect' => false,
-                'persistent' => false,
-                'timeout' => 5.0,
-                'read_write_timeout' => null,
-                'alias' => null,
-                'weight' => null,
-                'path' => null,
-                'iterable_multibulk' => false,
-                'throw_errors' => true,
-            );
-        }
-
-        if (!isset(self::$validators)) {
-            $bool = function($value) { return (bool) $value; };
-            $float = function($value) { return (float) $value; };
-            $int = function($value) { return (int) $value; };
-
-            self::$validators = array(
-                'port' => $int,
-                'async_connect' => $bool,
-                'persistent' => $bool,
-                'timeout' => $float,
-                'read_write_timeout' => $float,
-                'iterable_multibulk' => $bool,
-                'throw_errors' => $bool,
-            );
-        }
+        $this->parameters = $this->filter($parameters) + $this->getDefaults();
     }
 
     /**
-     * Defines a default value and a validator for the specified parameter.
+     * Returns some default parameters with their values.
      *
-     * @param string $parameter Name of the parameter.
-     * @param mixed $default Default value or an instance of OptionInterface.
-     * @param mixed $callable A validator callback.
+     * @return array
      */
-    public static function define($parameter, $default, $callable = null)
+    protected function getDefaults()
     {
-        self::ensureDefaults();
-        self::$defaultParameters[$parameter] = $default;
-
-        if ($default instanceof OptionInterface) {
-            self::$validators[$parameter] = $default;
-            return;
-        }
-
-        if (!isset($callable)) {
-            unset(self::$validators[$parameter]);
-            return;
-        }
-
-        if (!is_callable($callable)) {
-            throw new \InvalidArgumentException(
-                "The validator for $parameter must be a callable object"
-            );
-        }
-
-        self::$validators[$parameter] = $callable;
+        return self::$defaults;
     }
 
     /**
-     * Undefines the default value and validator for the specified parameter.
+     * Returns validators functions for the values of certain parameters.
      *
-     * @param string $parameter Name of the parameter.
+     * @return array
      */
-    public static function undefine($parameter)
+    protected function getValidators()
     {
-        self::ensureDefaults();
-        unset(self::$defaultParameters[$parameter], self::$validators[$parameter]);
+        $bool = function($value) { return (bool) $value; };
+        $float = function($value) { return (float) $value; };
+        $int = function($value) { return (int) $value; };
+
+        return array(
+            'port' => $int,
+            'async_connect' => $bool,
+            'persistent' => $bool,
+            'timeout' => $float,
+            'read_write_timeout' => $float,
+            'iterable_multibulk' => $bool,
+            'throw_errors' => $bool,
+        );
     }
 
     /**
@@ -161,7 +112,7 @@ class ConnectionParameters implements ConnectionParametersInterface
     private function filter(Array $parameters)
     {
         if (count($parameters) > 0) {
-            $validators = array_intersect_key(self::$validators, $parameters);
+            $validators = array_intersect_key($this->getValidators(), $parameters);
             foreach ($validators as $parameter => $validator) {
                 $parameters[$parameter] = $validator($parameters[$parameter]);
             }
@@ -175,13 +126,9 @@ class ConnectionParameters implements ConnectionParametersInterface
      */
     public function __get($parameter)
     {
-        $value = $this->parameters[$parameter];
-
-        if ($value instanceof OptionInterface) {
-            $this->parameters[$parameter] = ($value = $value->getDefault());
+        if (isset($this->{$parameter})) {
+            return $this->parameters[$parameter];
         }
-
-        return $value;
     }
 
     /**
@@ -192,39 +139,6 @@ class ConnectionParameters implements ConnectionParametersInterface
         return isset($this->parameters[$parameter]);
     }
 
-    /**
-     * Checks if the specified parameter has been set by the user.
-     *
-     * @param string $parameter Name of the parameter.
-     * @return Boolean
-     */
-    public function isSetByUser($parameter)
-    {
-        return in_array($parameter, $this->userDefined);
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    protected function getBaseURI()
-    {
-        if ($this->scheme === 'unix') {
-            return "{$this->scheme}://{$this->path}";
-        }
-
-        return "{$this->scheme}://{$this->host}:{$this->port}";
-    }
-
-    /**
-     * Returns the URI parts that must be omitted when calling __toString().
-     *
-     * @return array
-     */
-    protected function getDisallowedURIParts()
-    {
-        return array('scheme', 'host', 'port', 'password', 'path');
-    }
-
     /**
      * {@inheritdoc}
      */
@@ -233,45 +147,11 @@ class ConnectionParameters implements ConnectionParametersInterface
         return $this->parameters;
     }
 
-    /**
-     * Returns a string representation of the parameters.
-     *
-     * @return string
-     */
-    public function __toString()
-    {
-        $query = array();
-        $parameters = $this->toArray();
-        $reject = $this->getDisallowedURIParts();
-
-        foreach ($this->userDefined as $param) {
-            if (in_array($param, $reject) || !isset($parameters[$param])) {
-                continue;
-            }
-            $value = $parameters[$param];
-            $query[] = "$param=" . ($value === false ? '0' : $value);
-        }
-
-        if (count($query) === 0) {
-            return $this->getBaseURI();
-        }
-
-        return $this->getBaseURI() . '/?' . implode('&', $query);
-    }
-
     /**
      * {@inheritdoc}
      */
     public function __sleep()
     {
-        return array('parameters', 'userDefined');
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function __wakeup()
-    {
-        self::ensureDefaults();
+        return array('parameters');
     }
 }

+ 9 - 3
tests/Predis/ConnectionFactoryTest.php

@@ -71,7 +71,9 @@ class ConnectionFactoryTest extends StandardTestCase
 
         $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $connection);
         $this->assertEquals('tcp', $parameters->scheme);
-        $this->assertFalse($parameters->isSetByUser('custom'));
+
+        $this->assertFalse(isset($parameters->custom));
+        $this->assertNull($parameters->custom);
     }
 
     /**
@@ -85,7 +87,9 @@ class ConnectionFactoryTest extends StandardTestCase
 
         $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $connection);
         $this->assertEquals('tcp', $parameters->scheme);
-        $this->assertTrue($parameters->isSetByUser('custom'));
+
+        $this->assertTrue(isset($parameters->custom));
+        $this->assertSame('foobar', $parameters->custom);
     }
 
     /**
@@ -99,7 +103,9 @@ class ConnectionFactoryTest extends StandardTestCase
 
         $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $connection);
         $this->assertEquals('tcp', $parameters->scheme);
-        $this->assertTrue($parameters->isSetByUser('custom'));
+
+        $this->assertTrue(isset($parameters->custom));
+        $this->assertSame('foobar', $parameters->custom);
     }
 
     /**

+ 7 - 48
tests/Predis/ConnectionParametersTest.php

@@ -32,15 +32,7 @@ class ConnectionParametersTest extends StandardTestCase
         $this->assertEquals($defaults['port'], $parameters->port);
         $this->assertEquals($defaults['throw_errors'], $parameters->throw_errors);
         $this->assertEquals($defaults['iterable_multibulk'], $parameters->iterable_multibulk);
-        $this->assertEquals($defaults['async_connect'], $parameters->async_connect);
-        $this->assertEquals($defaults['persistent'], $parameters->persistent);
         $this->assertEquals($defaults['timeout'], $parameters->timeout);
-        $this->assertEquals($defaults['read_write_timeout'], $parameters->read_write_timeout);
-        $this->assertEquals($defaults['database'], $parameters->database);
-        $this->assertEquals($defaults['password'], $parameters->password);
-        $this->assertEquals($defaults['alias'], $parameters->alias);
-        $this->assertEquals($defaults['weight'], $parameters->weight);
-        $this->assertEquals($defaults['path'], $parameters->path);
     }
 
     /**
@@ -57,21 +49,21 @@ class ConnectionParametersTest extends StandardTestCase
     /**
      * @group disconnected
      */
-    public function testIsSetByUser()
+    public function testUserDefinedParameters()
     {
         $parameters = new ConnectionParameters(array('port' => 7000, 'custom' => 'foobar'));
 
         $this->assertTrue(isset($parameters->scheme));
-        $this->assertFalse($parameters->isSetByUser('scheme'));
+        $this->assertSame('tcp', $parameters->scheme);
 
         $this->assertTrue(isset($parameters->port));
-        $this->assertTrue($parameters->isSetByUser('port'));
+        $this->assertSame(7000, $parameters->port);
 
         $this->assertTrue(isset($parameters->custom));
-        $this->assertTrue($parameters->isSetByUser('custom'));
+        $this->assertSame('foobar', $parameters->custom);
 
         $this->assertFalse(isset($parameters->unknown));
-        $this->assertFalse($parameters->isSetByUser('unknown'));
+        $this->assertNull($parameters->unknown);
     }
 
     /**
@@ -98,11 +90,10 @@ class ConnectionParametersTest extends StandardTestCase
         $this->assertEquals($overrides['throw_errors'], $parameters->throw_errors);
 
         $this->assertTrue(isset($parameters->custom));
-        $this->assertTrue($parameters->isSetByUser('custom'));
         $this->assertEquals($overrides['custom'], $parameters->custom);
 
         $this->assertFalse(isset($parameters->unknown));
-        $this->assertFalse($parameters->isSetByUser('unknown'));
+        $this->assertNull($parameters->unknown);
     }
 
     /**
@@ -116,29 +107,6 @@ class ConnectionParametersTest extends StandardTestCase
         $this->assertEquals($this->getParametersArray($additional), $parameters->toArray());
     }
 
-    /**
-     * @group disconnected
-     */
-    public function testToString()
-    {
-        $uri = 'tcp://localhost:7000/?database=15&custom=foobar&throw_errors=0';
-        $parameters = new ConnectionParameters($uri);
-
-        $this->assertEquals($uri, (string) $parameters);
-    }
-
-    /**
-     * @group disconnected
-     * @todo Does it actually make sense?
-     */
-    public function testToStringOmitPassword()
-    {
-        $uri = 'tcp://localhost:7000/?database=15&custom=foobar&throw_errors=0';
-        $parameters = new ConnectionParameters($uri . '&password=foobar');
-
-        $this->assertEquals($uri, (string) $parameters);
-    }
-
     /**
      * @group disconnected
      */
@@ -151,11 +119,10 @@ class ConnectionParametersTest extends StandardTestCase
         $this->assertEquals($parameters->port, $unserialized->port);
 
         $this->assertTrue(isset($unserialized->custom));
-        $this->assertTrue($unserialized->isSetByUser('custom'));
         $this->assertEquals($parameters->custom, $unserialized->custom);
 
         $this->assertFalse(isset($unserialized->unknown));
-        $this->assertFalse($unserialized->isSetByUser('unknown'));
+        $this->assertNull($unserialized->unknown);
     }
 
     // ******************************************************************** //
@@ -173,15 +140,7 @@ class ConnectionParametersTest extends StandardTestCase
             'scheme' => 'tcp',
             'host' => '127.0.0.1',
             'port' => 6379,
-            'database' => null,
-            'password' => null,
-            'async_connect' => false,
-            'persistent' => false,
             'timeout' => 5.0,
-            'read_write_timeout' => null,
-            'alias' => null,
-            'weight' => null,
-            'path' => null,
             'iterable_multibulk' => false,
             'throw_errors' => true,
         );