Эх сурвалжийг харах

Deprecate the whole Predis\Helpers class.

Daniele Alessandri 12 жил өмнө
parent
commit
116eaba75e
31 өөрчлөгдсөн 132 нэмэгдсэн , 60 устгасан
  1. 3 0
      CHANGELOG.md
  2. 30 0
      lib/Predis/Command/AbstractCommand.php
  3. 1 3
      lib/Predis/Command/HashDelete.php
  4. 1 3
      lib/Predis/Command/HashGetMultiple.php
  5. 1 3
      lib/Predis/Command/KeyDelete.php
  6. 1 3
      lib/Predis/Command/ListPushTail.php
  7. 1 3
      lib/Predis/Command/PubSubSubscribe.php
  8. 0 2
      lib/Predis/Command/PubSubSubscribeByPattern.php
  9. 1 3
      lib/Predis/Command/PubSubUnsubscribe.php
  10. 0 2
      lib/Predis/Command/ServerObject.php
  11. 1 3
      lib/Predis/Command/SetAdd.php
  12. 1 3
      lib/Predis/Command/SetIntersection.php
  13. 1 3
      lib/Predis/Command/SetRemove.php
  14. 1 3
      lib/Predis/Command/StringGetMultiple.php
  15. 0 2
      lib/Predis/Command/ZSetAdd.php
  16. 1 3
      lib/Predis/Command/ZSetRemove.php
  17. 19 0
      lib/Predis/CommunicationException.php
  18. 3 3
      lib/Predis/Connection/AbstractConnection.php
  19. 4 0
      lib/Predis/Helpers.php
  20. 0 1
      lib/Predis/Pipeline/PipelineContext.php
  21. 2 2
      lib/Predis/Protocol/Text/ResponseBulkHandler.php
  22. 2 2
      lib/Predis/Protocol/Text/ResponseIntegerHandler.php
  23. 2 2
      lib/Predis/Protocol/Text/ResponseMultiBulkHandler.php
  24. 2 2
      lib/Predis/Protocol/Text/ResponseMultiBulkStreamHandler.php
  25. 2 2
      lib/Predis/Protocol/Text/TextProtocol.php
  26. 2 2
      lib/Predis/Protocol/Text/TextResponseReader.php
  27. 0 1
      lib/Predis/PubSub/AbstractPubSubContext.php
  28. 2 2
      lib/Predis/PubSub/PubSubContext.php
  29. 1 2
      lib/Predis/Transaction/MultiExecContext.php
  30. 31 0
      tests/Predis/Command/CommandTest.php
  31. 16 0
      tests/Predis/CommunicationExceptionTest.php

+ 3 - 0
CHANGELOG.md

@@ -23,6 +23,9 @@ v0.8.3 (2013-xx-xx)
   instead of `FALSE` to indicate that all of the arguments of a Lua script must
   be used to populate `ARGV[]`. This does not represent a breaking change.
 
+- The `Predis\Helpers` class has been deprecated and it will be removed in
+  future releases.
+
 
 v0.8.2 (2013-02-03)
 ===============================================================================

+ 30 - 0
lib/Predis/Command/AbstractCommand.php

@@ -129,4 +129,34 @@ abstract class AbstractCommand implements CommandInterface
             $this->getId()
         );
     }
+
+    /**
+     * Normalizes the arguments array passed to a Redis command.
+     *
+     * @param array $arguments Arguments for a command.
+     * @return array
+     */
+    public static function normalizeArguments(Array $arguments)
+    {
+        if (count($arguments) === 1 && is_array($arguments[0])) {
+            return $arguments[0];
+        }
+
+        return $arguments;
+    }
+
+    /**
+     * Normalizes the arguments array passed to a variadic Redis command.
+     *
+     * @param array $arguments Arguments for a command.
+     * @return array
+     */
+    public static function normalizeVariadic(Array $arguments)
+    {
+        if (count($arguments) === 2 && is_array($arguments[1])) {
+            return array_merge(array($arguments[0]), $arguments[1]);
+        }
+
+        return $arguments;
+    }
 }

+ 1 - 3
lib/Predis/Command/HashDelete.php

@@ -11,8 +11,6 @@
 
 namespace Predis\Command;
 
-use Predis\Helpers;
-
 /**
  * @link http://redis.io/commands/hdel
  * @author Daniele Alessandri <suppakilla@gmail.com>
@@ -32,6 +30,6 @@ class HashDelete extends PrefixableCommand
      */
     protected function filterArguments(Array $arguments)
     {
-        return Helpers::filterVariadicValues($arguments);
+        return self::normalizeVariadic($arguments);
     }
 }

+ 1 - 3
lib/Predis/Command/HashGetMultiple.php

@@ -11,8 +11,6 @@
 
 namespace Predis\Command;
 
-use Predis\Helpers;
-
 /**
  * @link http://redis.io/commands/hmget
  * @author Daniele Alessandri <suppakilla@gmail.com>
@@ -32,6 +30,6 @@ class HashGetMultiple extends PrefixableCommand
      */
     protected function filterArguments(Array $arguments)
     {
-        return Helpers::filterVariadicValues($arguments);
+        return self::normalizeVariadic($arguments);
     }
 }

+ 1 - 3
lib/Predis/Command/KeyDelete.php

@@ -11,8 +11,6 @@
 
 namespace Predis\Command;
 
-use Predis\Helpers;
-
 /**
  * @link http://redis.io/commands/del
  * @author Daniele Alessandri <suppakilla@gmail.com>
@@ -32,7 +30,7 @@ class KeyDelete extends AbstractCommand implements PrefixableCommandInterface
      */
     protected function filterArguments(Array $arguments)
     {
-        return Helpers::filterArrayArguments($arguments);
+        return self::normalizeArguments($arguments);
     }
 
     /**

+ 1 - 3
lib/Predis/Command/ListPushTail.php

@@ -11,8 +11,6 @@
 
 namespace Predis\Command;
 
-use Predis\Helpers;
-
 /**
  * @link http://redis.io/commands/rpush
  * @author Daniele Alessandri <suppakilla@gmail.com>
@@ -32,6 +30,6 @@ class ListPushTail extends PrefixableCommand
      */
     protected function filterArguments(Array $arguments)
     {
-        return Helpers::filterVariadicValues($arguments);
+        return self::normalizeVariadic($arguments);
     }
 }

+ 1 - 3
lib/Predis/Command/PubSubSubscribe.php

@@ -11,8 +11,6 @@
 
 namespace Predis\Command;
 
-use Predis\Helpers;
-
 /**
  * @link http://redis.io/commands/subscribe
  * @author Daniele Alessandri <suppakilla@gmail.com>
@@ -32,7 +30,7 @@ class PubSubSubscribe extends AbstractCommand implements PrefixableCommandInterf
      */
     protected function filterArguments(Array $arguments)
     {
-        return Helpers::filterArrayArguments($arguments);
+        return self::normalizeArguments($arguments);
     }
 
     /**

+ 0 - 2
lib/Predis/Command/PubSubSubscribeByPattern.php

@@ -11,8 +11,6 @@
 
 namespace Predis\Command;
 
-use Predis\Helpers;
-
 /**
  * @link http://redis.io/commands/psubscribe
  * @author Daniele Alessandri <suppakilla@gmail.com>

+ 1 - 3
lib/Predis/Command/PubSubUnsubscribe.php

@@ -11,8 +11,6 @@
 
 namespace Predis\Command;
 
-use Predis\Helpers;
-
 /**
  * @link http://redis.io/commands/unsubscribe
  * @author Daniele Alessandri <suppakilla@gmail.com>
@@ -32,7 +30,7 @@ class PubSubUnsubscribe extends AbstractCommand implements PrefixableCommandInte
      */
     protected function filterArguments(Array $arguments)
     {
-        return Helpers::filterArrayArguments($arguments);
+        return self::normalizeArguments($arguments);
     }
 
     /**

+ 0 - 2
lib/Predis/Command/ServerObject.php

@@ -11,8 +11,6 @@
 
 namespace Predis\Command;
 
-use Predis\Helpers;
-
 /**
  * @link http://redis.io/commands/object
  * @author Daniele Alessandri <suppakilla@gmail.com>

+ 1 - 3
lib/Predis/Command/SetAdd.php

@@ -11,8 +11,6 @@
 
 namespace Predis\Command;
 
-use Predis\Helpers;
-
 /**
  * @link http://redis.io/commands/sadd
  * @author Daniele Alessandri <suppakilla@gmail.com>
@@ -32,6 +30,6 @@ class SetAdd extends PrefixableCommand
      */
     protected function filterArguments(Array $arguments)
     {
-        return Helpers::filterVariadicValues($arguments);
+        return self::normalizeVariadic($arguments);
     }
 }

+ 1 - 3
lib/Predis/Command/SetIntersection.php

@@ -11,8 +11,6 @@
 
 namespace Predis\Command;
 
-use Predis\Helpers;
-
 /**
  * @link http://redis.io/commands/sinter
  * @author Daniele Alessandri <suppakilla@gmail.com>
@@ -32,7 +30,7 @@ class SetIntersection extends AbstractCommand implements PrefixableCommandInterf
      */
     protected function filterArguments(Array $arguments)
     {
-        return Helpers::filterArrayArguments($arguments);
+        return self::normalizeArguments($arguments);
     }
 
     /**

+ 1 - 3
lib/Predis/Command/SetRemove.php

@@ -11,8 +11,6 @@
 
 namespace Predis\Command;
 
-use Predis\Helpers;
-
 /**
  * @link http://redis.io/commands/srem
  * @author Daniele Alessandri <suppakilla@gmail.com>
@@ -32,6 +30,6 @@ class SetRemove extends PrefixableCommand
      */
     protected function filterArguments(Array $arguments)
     {
-        return Helpers::filterVariadicValues($arguments);
+        return self::normalizeVariadic($arguments);
     }
 }

+ 1 - 3
lib/Predis/Command/StringGetMultiple.php

@@ -11,8 +11,6 @@
 
 namespace Predis\Command;
 
-use Predis\Helpers;
-
 /**
  * @link http://redis.io/commands/mget
  * @author Daniele Alessandri <suppakilla@gmail.com>
@@ -32,7 +30,7 @@ class StringGetMultiple extends AbstractCommand implements PrefixableCommandInte
      */
     protected function filterArguments(Array $arguments)
     {
-        return Helpers::filterArrayArguments($arguments);
+        return self::normalizeArguments($arguments);
     }
 
     /**

+ 0 - 2
lib/Predis/Command/ZSetAdd.php

@@ -11,8 +11,6 @@
 
 namespace Predis\Command;
 
-use Predis\Helpers;
-
 /**
  * @link http://redis.io/commands/zadd
  * @author Daniele Alessandri <suppakilla@gmail.com>

+ 1 - 3
lib/Predis/Command/ZSetRemove.php

@@ -11,8 +11,6 @@
 
 namespace Predis\Command;
 
-use Predis\Helpers;
-
 /**
  * @link http://redis.io/commands/zrem
  * @author Daniele Alessandri <suppakilla@gmail.com>
@@ -32,6 +30,6 @@ class ZSetRemove extends PrefixableCommand
      */
     protected function filterArguments(Array $arguments)
     {
-        return Helpers::filterVariadicValues($arguments);
+        return self::normalizeVariadic($arguments);
     }
 }

+ 19 - 0
lib/Predis/CommunicationException.php

@@ -54,4 +54,23 @@ abstract class CommunicationException extends PredisException
     {
         return true;
     }
+
+    /**
+     * Offers a generic and reusable method to handle exceptions generated by
+     * a connection object.
+     *
+     * @param CommunicationException $exception Exception.
+     */
+    public static function handle(CommunicationException $exception)
+    {
+        if ($exception->shouldResetConnection()) {
+            $connection = $exception->getConnection();
+
+            if ($connection->isConnected()) {
+                $connection->disconnect();
+            }
+        }
+
+        throw $exception;
+    }
 }

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

@@ -12,7 +12,7 @@
 namespace Predis\Connection;
 
 use Predis\ClientException;
-use Predis\Helpers;
+use Predis\CommunicationException;
 use Predis\NotSupportedException;
 use Predis\Command\CommandInterface;
 use Predis\Protocol\ProtocolException;
@@ -136,7 +136,7 @@ abstract class AbstractConnection implements SingleConnectionInterface
      */
     protected function onConnectionError($message, $code = null)
     {
-        Helpers::onCommunicationException(new ConnectionException($this, $message, $code));
+        CommunicationException::handle(new ConnectionException($this, $message, $code));
     }
 
     /**
@@ -146,7 +146,7 @@ abstract class AbstractConnection implements SingleConnectionInterface
      */
     protected function onProtocolError($message)
     {
-        Helpers::onCommunicationException(new ProtocolException($this, $message));
+        CommunicationException::handle(new ProtocolException($this, $message));
     }
 
     /**

+ 4 - 0
lib/Predis/Helpers.php

@@ -19,6 +19,7 @@ use Predis\Connection\ConnectionInterface;
  * Defines a few helper methods.
  *
  * @author Daniele Alessandri <suppakilla@gmail.com>
+ * @deprecated Deprecated since v0.8.3.
  */
 class Helpers
 {
@@ -26,6 +27,7 @@ class Helpers
      * Offers a generic and reusable method to handle exceptions generated by
      * a connection object.
      *
+     * @deprecated Deprecated since v0.8.3 - moved in Predis\CommunicationException::handle()
      * @param CommunicationException $exception Exception.
      */
     public static function onCommunicationException(CommunicationException $exception)
@@ -44,6 +46,7 @@ class Helpers
     /**
      * Normalizes the arguments array passed to a Redis command.
      *
+     * @deprecated Deprecated since v0.8.3 - moved in Predis\Command\AbstractCommand::normalizeArguments()
      * @param array $arguments Arguments for a command.
      * @return array
      */
@@ -59,6 +62,7 @@ class Helpers
     /**
      * Normalizes the arguments array passed to a variadic Redis command.
      *
+     * @deprecated Deprecated since v0.8.3 - moved in Predis\Command\AbstractCommand::normalizeVariadic()
      * @param array $arguments Arguments for a command.
      * @return array
      */

+ 0 - 1
lib/Predis/Pipeline/PipelineContext.php

@@ -16,7 +16,6 @@ use Predis\BasicClientInterface;
 use Predis\ClientException;
 use Predis\ClientInterface;
 use Predis\ExecutableContextInterface;
-use Predis\Helpers;
 use Predis\Command\CommandInterface;
 
 /**

+ 2 - 2
lib/Predis/Protocol/Text/ResponseBulkHandler.php

@@ -11,7 +11,7 @@
 
 namespace Predis\Protocol\Text;
 
-use Predis\Helpers;
+use Predis\CommunicationException;
 use Predis\Connection\ComposableConnectionInterface;
 use Predis\Protocol\ProtocolException;
 use Predis\Protocol\ResponseHandlerInterface;
@@ -37,7 +37,7 @@ class ResponseBulkHandler implements ResponseHandlerInterface
         $length = (int) $lengthString;
 
         if ("$length" !== $lengthString) {
-            Helpers::onCommunicationException(new ProtocolException(
+            CommunicationException::handle(new ProtocolException(
                 $connection, "Cannot parse '$lengthString' as bulk length"
             ));
         }

+ 2 - 2
lib/Predis/Protocol/Text/ResponseIntegerHandler.php

@@ -11,7 +11,7 @@
 
 namespace Predis\Protocol\Text;
 
-use Predis\Helpers;
+use Predis\CommunicationException;
 use Predis\Connection\ComposableConnectionInterface;
 use Predis\Protocol\ProtocolException;
 use Predis\Protocol\ResponseHandlerInterface;
@@ -39,7 +39,7 @@ class ResponseIntegerHandler implements ResponseHandlerInterface
         }
 
         if ($number !== 'nil') {
-            Helpers::onCommunicationException(new ProtocolException(
+            CommunicationException::handle(new ProtocolException(
                 $connection, "Cannot parse '$number' as numeric response"
             ));
         }

+ 2 - 2
lib/Predis/Protocol/Text/ResponseMultiBulkHandler.php

@@ -11,7 +11,7 @@
 
 namespace Predis\Protocol\Text;
 
-use Predis\Helpers;
+use Predis\CommunicationException;
 use Predis\Connection\ComposableConnectionInterface;
 use Predis\Protocol\ProtocolException;
 use Predis\Protocol\ResponseHandlerInterface;
@@ -37,7 +37,7 @@ class ResponseMultiBulkHandler implements ResponseHandlerInterface
         $length = (int) $lengthString;
 
         if ("$length" !== $lengthString) {
-            Helpers::onCommunicationException(new ProtocolException(
+            CommunicationException::handle(new ProtocolException(
                 $connection, "Cannot parse '$lengthString' as multi-bulk length"
             ));
         }

+ 2 - 2
lib/Predis/Protocol/Text/ResponseMultiBulkStreamHandler.php

@@ -11,7 +11,7 @@
 
 namespace Predis\Protocol\Text;
 
-use Predis\Helpers;
+use Predis\CommunicationException;
 use Predis\Connection\ComposableConnectionInterface;
 use Predis\Iterator\MultiBulkResponseSimple;
 use Predis\Protocol\ProtocolException;
@@ -38,7 +38,7 @@ class ResponseMultiBulkStreamHandler implements ResponseHandlerInterface
         $length = (int) $lengthString;
 
         if ("$length" != $lengthString) {
-            Helpers::onCommunicationException(new ProtocolException(
+            CommunicationException::handle(new ProtocolException(
                 $connection, "Cannot parse '$lengthString' as multi-bulk length"
             ));
         }

+ 2 - 2
lib/Predis/Protocol/Text/TextProtocol.php

@@ -11,7 +11,7 @@
 
 namespace Predis\Protocol\Text;
 
-use Predis\Helpers;
+use Predis\CommunicationException;
 use Predis\ResponseError;
 use Predis\ResponseQueued;
 use Predis\ServerException;
@@ -117,7 +117,7 @@ class TextProtocol implements ProtocolInterface
                 return new ResponseError($payload);
 
             default:
-                Helpers::onCommunicationException(new ProtocolException(
+                CommunicationException::handle(new ProtocolException(
                     $connection, "Unknown prefix: '$prefix'"
                 ));
         }

+ 2 - 2
lib/Predis/Protocol/Text/TextResponseReader.php

@@ -11,7 +11,7 @@
 
 namespace Predis\Protocol\Text;
 
-use Predis\Helpers;
+use Predis\CommunicationException;
 use Predis\Connection\ComposableConnectionInterface;
 use Predis\Protocol\ProtocolException;
 use Predis\Protocol\ResponseHandlerInterface;
@@ -108,6 +108,6 @@ class TextResponseReader implements ResponseReaderInterface
      */
     private function protocolError(ComposableConnectionInterface $connection, $message)
     {
-        Helpers::onCommunicationException(new ProtocolException($connection, $message));
+        CommunicationException::handle(new ProtocolException($connection, $message));
     }
 }

+ 0 - 1
lib/Predis/PubSub/AbstractPubSubContext.php

@@ -13,7 +13,6 @@ namespace Predis\PubSub;
 
 use Predis\ClientException;
 use Predis\ClientInterface;
-use Predis\Helpers;
 use Predis\NotSupportedException;
 
 /**

+ 2 - 2
lib/Predis/PubSub/PubSubContext.php

@@ -13,7 +13,7 @@ namespace Predis\PubSub;
 
 use Predis\ClientException;
 use Predis\ClientInterface;
-use Predis\Helpers;
+use Predis\Command\AbstractCommand as Command;
 use Predis\NotSupportedException;
 use Predis\Connection\AggregatedConnectionInterface;
 
@@ -77,7 +77,7 @@ class PubSubContext extends AbstractPubSubContext
      */
     protected function writeCommand($method, $arguments)
     {
-        $arguments = Helpers::filterArrayArguments($arguments);
+        $arguments = Command::normalizeArguments($arguments);
         $command = $this->client->createCommand($method, $arguments);
         $this->client->getConnection()->writeCommand($command);
     }

+ 1 - 2
lib/Predis/Transaction/MultiExecContext.php

@@ -17,7 +17,6 @@ use Predis\ClientException;
 use Predis\ClientInterface;
 use Predis\CommunicationException;
 use Predis\ExecutableContextInterface;
-use Predis\Helpers;
 use Predis\NotSupportedException;
 use Predis\ResponseErrorInterface;
 use Predis\ResponseQueued;
@@ -443,7 +442,7 @@ class MultiExecContext implements BasicClientInterface, ExecutableContextInterfa
         // Since a MULTI/EXEC block cannot be initialized when using aggregated
         // connections, we can safely assume that Predis\Client::getConnection()
         // will always return an instance of Predis\Connection\SingleConnectionInterface.
-        Helpers::onCommunicationException(new ProtocolException(
+        CommunicationException::handle(new ProtocolException(
             $this->client->getConnection(), $message
         ));
     }

+ 31 - 0
tests/Predis/Command/CommandTest.php

@@ -140,4 +140,35 @@ class CommandTest extends StandardTestCase
 
         $this->assertEquals($expected, (string) $command);
     }
+
+    /**
+     * @group disconnected
+     */
+    public function testNormalizeArguments()
+    {
+        $arguments = array('arg1', 'arg2', 'arg3', 'arg4');
+
+        $this->assertSame($arguments, AbstractCommand::normalizeArguments($arguments));
+        $this->assertSame($arguments, AbstractCommand::normalizeArguments(array($arguments)));
+
+        $arguments = array(array(), array());
+        $this->assertSame($arguments, AbstractCommand::normalizeArguments($arguments));
+
+        $arguments = array(new \stdClass());
+        $this->assertSame($arguments, AbstractCommand::normalizeArguments($arguments));
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testNormalizeVariadic()
+    {
+        $arguments = array('key', 'value1', 'value2', 'value3');
+
+        $this->assertSame($arguments, AbstractCommand::normalizeVariadic($arguments));
+        $this->assertSame($arguments, AbstractCommand::normalizeVariadic(array('key', array('value1', 'value2', 'value3'))));
+
+        $arguments = array(new \stdClass());
+        $this->assertSame($arguments, AbstractCommand::normalizeVariadic($arguments));
+    }
 }

+ 16 - 0
tests/Predis/CommunicationExceptionTest.php

@@ -56,6 +56,22 @@ class CommunicationExceptionTest extends StandardTestCase
         $this->assertTrue($exception->shouldResetConnection());
     }
 
+    /**
+     * @group disconnected
+     * @expectedException Predis\CommunicationException
+     * @expectedExceptionMessage Communication error
+     */
+    public function testCommunicationExceptionHandling()
+    {
+        $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
+        $connection->expects($this->once())->method('isConnected')->will($this->returnValue(true));
+        $connection->expects($this->once())->method('disconnect');
+
+        $exception = $this->getException($connection, 'Communication error');
+
+        CommunicationException::handle($exception);
+    }
+
     // ******************************************************************** //
     // ---- HELPER METHODS ------------------------------------------------ //
     // ******************************************************************** //