Sfoglia il codice sorgente

Simplify implementation of Predis\Connection\Parameters.

Casting values supplied by users should be done by the consumer since
we cannot cover any possible use case anyway. It is still possible to
subclass Predis\Connection\Parameters and override the filter() method
to convert certain values if deemed necessary.
Daniele Alessandri 10 anni fa
parent
commit
6fe56985fc

+ 5 - 0
CHANGELOG.md

@@ -41,6 +41,11 @@ v1.0.0 (201x-xx-xx)
   at a level lower than the client abstraction (our standard and composable text
   protocol processors still handle them and can be used as an example).
 
+- Simplified the implementation of connection parameters by removing method used
+  to cast to int / bool / float certain parameters supplied by users. Casting
+  values, if deemed necessary, should be done by the consumer or you can just
+  subclass `Predis\Connection\Parameters` and override the `filter()` method.
+
 - Changed a couple of options for our transaction abstraction:
 
     - `exceptions`: overrides the value of the client option with the same name.

+ 1 - 60
src/Connection/Parameters.php

@@ -29,14 +29,6 @@ class Parameters implements ParametersInterface
         'timeout' => 5.0,
     );
 
-    private static $casters = array(
-        'port' => 'self::castInteger',
-        'async_connect' => 'self::castBoolean',
-        'persistent' => 'self::castBoolean',
-        'timeout' => 'self::castFloat',
-        'read_write_timeout' => 'self::castFloat',
-    );
-
     /**
      * @param array $parameters Named array of connection parameters.
      */
@@ -55,16 +47,6 @@ class Parameters implements ParametersInterface
         return self::$defaults;
     }
 
-    /**
-     * Returns cast functions for user-supplied parameter values.
-     *
-     * @return array
-     */
-    protected function getValueCasters()
-    {
-        return self::$casters;
-    }
-
     /**
      * Creates a new instance by supplying the initial parameters either in the
      * form of an URI string or a named array.
@@ -114,52 +96,11 @@ class Parameters implements ParametersInterface
      * @param  array $parameters Connection parameters.
      * @return array
      */
-    private function filter(array $parameters)
+    protected function filter(array $parameters)
     {
-        if ($parameters) {
-            $casters = array_intersect_key($this->getValueCasters(), $parameters);
-
-            foreach ($casters as $parameter => $caster) {
-                $parameters[$parameter] = call_user_func($caster, $parameters[$parameter]);
-            }
-        }
-
         return $parameters ?: array();
     }
 
-    /**
-     * Validates value as boolean.
-     *
-     * @param  mixed   $value Input value.
-     * @return boolean
-     */
-    private static function castBoolean($value)
-    {
-        return (bool) $value;
-    }
-
-    /**
-     * Validates value as float.
-     *
-     * @param  mixed $value Input value.
-     * @return float
-     */
-    private static function castFloat($value)
-    {
-        return (float) $value;
-    }
-
-    /**
-     * Validates value as integer.
-     *
-     * @param  mixed $value Input value.
-     * @return int
-     */
-    private static function castInteger($value)
-    {
-        return (int) $value;
-    }
-
     /**
      * {@inheritdoc}
      */

+ 3 - 3
src/Connection/PhpiredisSocketConnection.php

@@ -204,7 +204,7 @@ class PhpiredisSocketConnection extends AbstractConnection
         }
 
         if (isset($parameters->read_write_timeout)) {
-            $rwtimeout = $parameters->read_write_timeout;
+            $rwtimeout = (float) $parameters->read_write_timeout;
             $timeoutSec = floor($rwtimeout);
             $timeoutUsec = ($rwtimeout - $timeoutSec) * 1000000;
 
@@ -264,7 +264,7 @@ class PhpiredisSocketConnection extends AbstractConnection
 
         socket_set_nonblock($socket);
 
-        if (@socket_connect($socket, $host, $parameters->port) === false) {
+        if (@socket_connect($socket, $host, (int) $parameters->port) === false) {
             $error = socket_last_error();
 
             if ($error != SOCKET_EINPROGRESS && $error != SOCKET_EALREADY) {
@@ -277,7 +277,7 @@ class PhpiredisSocketConnection extends AbstractConnection
         $null = null;
         $selectable = array($socket);
 
-        $timeout = $parameters->timeout;
+        $timeout = (float) $parameters->timeout;
         $timeoutSecs = floor($timeout);
         $timeoutUSecs = ($timeout - $timeoutSecs) * 1000000;
 

+ 6 - 6
src/Connection/StreamConnection.php

@@ -69,23 +69,23 @@ class StreamConnection extends AbstractConnection
         $uri = "tcp://{$parameters->host}:{$parameters->port}";
         $flags = STREAM_CLIENT_CONNECT;
 
-        if (isset($parameters->async_connect) && $parameters->async_connect) {
+        if (isset($parameters->async_connect) && (bool) $parameters->async_connect) {
             $flags |= STREAM_CLIENT_ASYNC_CONNECT;
         }
 
-        if (isset($parameters->persistent) && $parameters->persistent) {
+        if (isset($parameters->persistent) && (bool) $parameters->persistent) {
             $flags |= STREAM_CLIENT_PERSISTENT;
             $uri .= strpos($path = $parameters->path, '/') === 0 ? $path : "/$path";
         }
 
-        $resource = @stream_socket_client($uri, $errno, $errstr, $parameters->timeout, $flags);
+        $resource = @stream_socket_client($uri, $errno, $errstr, (float) $parameters->timeout, $flags);
 
         if (!$resource) {
             $this->onConnectionError(trim($errstr), $errno);
         }
 
         if (isset($parameters->read_write_timeout)) {
-            $rwtimeout = $parameters->read_write_timeout;
+            $rwtimeout = (float) $parameters->read_write_timeout;
             $rwtimeout = $rwtimeout > 0 ? $rwtimeout : -1;
             $timeoutSeconds  = floor($rwtimeout);
             $timeoutUSeconds = ($rwtimeout - $timeoutSeconds) * 1000000;
@@ -111,11 +111,11 @@ class StreamConnection extends AbstractConnection
         $uri = "unix://{$parameters->path}";
         $flags = STREAM_CLIENT_CONNECT;
 
-        if ($parameters->persistent) {
+        if ((bool) $parameters->persistent) {
             $flags |= STREAM_CLIENT_PERSISTENT;
         }
 
-        $resource = @stream_socket_client($uri, $errno, $errstr, $parameters->timeout, $flags);
+        $resource = @stream_socket_client($uri, $errno, $errstr, (float) $parameters->timeout, $flags);
 
         if (!$resource) {
             $this->onConnectionError(trim($errstr), $errno);