Jelajahi Sumber

Add the Predis\IReplyObject interface (used to mark returned objects that must skip reply parsing).

Daniele Alessandri 14 tahun lalu
induk
melakukan
2012963ed0

+ 8 - 0
lib/Predis/IRedisServerError.php

@@ -0,0 +1,8 @@
+<?php
+
+namespace Predis;
+
+interface IRedisServerError extends IReplyObject {
+    public function getMessage();
+    public function getErrorType();
+}

+ 6 - 0
lib/Predis/IReplyObject.php

@@ -0,0 +1,6 @@
+<?php
+
+namespace Predis;
+
+interface IReplyObject {
+}

+ 2 - 1
lib/Predis/Network/ConnectionBase.php

@@ -4,6 +4,7 @@ namespace Predis\Network;
 
 use \InvalidArgumentException;
 use Predis\Helpers;
+use Predis\IReplyObject;
 use Predis\IConnectionParameters;
 use Predis\ClientException;
 use Predis\ProtocolException;
@@ -74,7 +75,7 @@ abstract class ConnectionBase implements IConnectionSingle {
 
     public function readResponse(ICommand $command) {
         $reply = $this->read();
-        if (isset($reply->skipParse)) {
+        if ($reply instanceof IReplyObject) {
             return $reply;
         }
         return $command->parseResponse($reply);

+ 7 - 16
lib/Predis/ResponseError.php

@@ -2,32 +2,23 @@
 
 namespace Predis;
 
-class ResponseError {
-    public $skipParse = true;
+class ResponseError implements IRedisServerError {
     private $_message;
-    private $_type;
 
     public function __construct($message) {
         $this->_message = $message;
-        $this->_type = substr($message, 0, strpos($message, ' '));
     }
 
-    public function __get($property) {
-        switch ($property) {
-            case 'error':
-                return true;
-            case 'message':
-                return $this->_message;
-            case 'type':
-                return $this->_type;
-        }
+    public function getMessage() {
+        return $this->_message;
     }
 
-    public function __isset($property) {
-        return $property === 'error';
+    public function getErrorType() {
+        list($errorType, ) = explode(' ', $this->getMessage(), 2);
+        return $errorType;
     }
 
     public function __toString() {
-        return $this->_message;
+        return $this->getMessage();
     }
 }

+ 1 - 3
lib/Predis/ResponseQueued.php

@@ -2,9 +2,7 @@
 
 namespace Predis;
 
-class ResponseQueued {
-    public $skipParse = true;
-
+class ResponseQueued implements IReplyObject {
     public function __toString() {
         return 'QUEUED';
     }

+ 4 - 10
lib/Predis/ServerException.php

@@ -2,19 +2,13 @@
 
 namespace Predis;
 
-class ServerException extends PredisException {
-    private $_errorType;
-
-    public function __construct($message) {
-        parent::__construct($message);
-        $this->_errorType = substr($message, 0, strpos($message, ' '));
+class ServerException extends PredisException implements IRedisServerError {
+    public function getErrorType() {
+        list($errorType, ) = explode(' ', $this->getMessage(), 2);
+        return $errorType;
     }
 
     public function toResponseError() {
         return new ResponseError($this->getMessage());
     }
-
-    public function getErrorType() {
-        return $this->_errorType;
-    }
 }

+ 6 - 4
test/ClientFeaturesTest.php

@@ -157,7 +157,7 @@ class ClientFeaturesTestSuite extends PHPUnit_Framework_TestCase {
 
     function testResponseQueued() {
         $response = new \Predis\ResponseQueued();
-        $this->assertTrue($response->queued);
+        $this->assertInstanceOf('\Predis\IReplyObject', $response);
         $this->assertEquals(\Predis\Protocols\Text\TextProtocol::QUEUED, (string)$response);
     }
 
@@ -168,8 +168,10 @@ class ClientFeaturesTestSuite extends PHPUnit_Framework_TestCase {
         $errorMessage = 'ERROR MESSAGE';
         $response = new \Predis\ResponseError($errorMessage);
 
-        $this->assertTrue($response->error);
-        $this->assertEquals($errorMessage, $response->message);
+        $this->assertInstanceOf('\Predis\IReplyObject', $response);
+        $this->assertInstanceOf('\Predis\IRedisServerError', $response);
+
+        $this->assertEquals($errorMessage, $response->getMessage());
         $this->assertEquals($errorMessage, (string)$response);
     }
 
@@ -309,7 +311,7 @@ class ClientFeaturesTestSuite extends PHPUnit_Framework_TestCase {
         $connection->writeBytes($rawCmdUnexpected);
         $errorReply = $reader->read($connection);
         $this->assertInstanceOf('\Predis\ResponseError', $errorReply);
-        $this->assertEquals(RC::EXCEPTION_WRONG_TYPE, $errorReply->message);
+        $this->assertEquals(RC::EXCEPTION_WRONG_TYPE, $errorReply->getMessage());
 
         $reader->setHandler(
             \Predis\Protocols\Text\TextProtocol::PREFIX_ERROR,