Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit 4e12f58

Browse files
author
xigu.lx
committed
init
1 parent 0298155 commit 4e12f58

34 files changed

+2014
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
vendor/
2+
.DS_Store
3+
composer.lock
4+
.idea/

MQ/AsyncCallback.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
namespace MQ;
3+
4+
use MQ\Exception\MQException;
5+
6+
class AsyncCallback
7+
{
8+
protected $succeedCallback;
9+
protected $failedCallback;
10+
11+
public function __construct(callable $succeedCallback, callable $failedCallback)
12+
{
13+
$this->succeedCallback = $succeedCallback;
14+
$this->failedCallback = $failedCallback;
15+
}
16+
17+
public function onSucceed($result)
18+
{
19+
return call_user_func($this->succeedCallback, $result);
20+
}
21+
22+
public function onFailed(MQException $e)
23+
{
24+
return call_user_func($this->failedCallback, $e);
25+
}
26+
}
27+
28+
?>

MQ/Common/XMLParser.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
namespace MQ\Common;
3+
4+
class XMLParser
5+
{
6+
/**
7+
* Most of the error responses are in same format.
8+
*/
9+
static function parseNormalError(\XMLReader $xmlReader) {
10+
$result = array('Code' => NULL, 'Message' => NULL, 'RequestId' => NULL, 'HostId' => NULL);
11+
while ($xmlReader->Read())
12+
{
13+
if ($xmlReader->nodeType == \XMLReader::ELEMENT)
14+
{
15+
switch ($xmlReader->name) {
16+
case 'Code':
17+
$xmlReader->read();
18+
if ($xmlReader->nodeType == \XMLReader::TEXT)
19+
{
20+
$result['Code'] = $xmlReader->value;
21+
}
22+
break;
23+
case 'Message':
24+
$xmlReader->read();
25+
if ($xmlReader->nodeType == \XMLReader::TEXT)
26+
{
27+
$result['Message'] = $xmlReader->value;
28+
}
29+
break;
30+
case 'RequestId':
31+
$xmlReader->read();
32+
if ($xmlReader->nodeType == \XMLReader::TEXT)
33+
{
34+
$result['RequestId'] = $xmlReader->value;
35+
}
36+
break;
37+
case 'HostId':
38+
$xmlReader->read();
39+
if ($xmlReader->nodeType == \XMLReader::TEXT)
40+
{
41+
$result['HostId'] = $xmlReader->value;
42+
}
43+
break;
44+
}
45+
}
46+
}
47+
return $result;
48+
}
49+
}
50+
51+
?>

MQ/Config.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
namespace MQ;
3+
4+
class Config
5+
{
6+
private $proxy; // http://username:password@192.168.16.1:10
7+
private $connectTimeout;
8+
private $requestTimeout;
9+
private $expectContinue;
10+
11+
public function __construct()
12+
{
13+
$this->proxy = NULL;
14+
$this->requestTimeout = 35; // 35 seconds
15+
$this->connectTimeout = 3; // 3 seconds
16+
$this->expectContinue = false;
17+
}
18+
19+
20+
public function getProxy()
21+
{
22+
return $this->proxy;
23+
}
24+
25+
public function setProxy($proxy)
26+
{
27+
$this->proxy = $proxy;
28+
}
29+
30+
public function getRequestTimeout()
31+
{
32+
return $this->requestTimeout;
33+
}
34+
35+
public function setRequestTimeout($requestTimeout)
36+
{
37+
$this->requestTimeout = $requestTimeout;
38+
}
39+
40+
public function setConnectTimeout($connectTimeout)
41+
{
42+
$this->connectTimeout = $connectTimeout;
43+
}
44+
45+
public function getConnectTimeout()
46+
{
47+
return $this->connectTimeout;
48+
}
49+
50+
public function getExpectContinue()
51+
{
52+
return $this->expectContinue;
53+
}
54+
55+
public function setExpectContinue($expectContinue)
56+
{
57+
$this->expectContinue = $expectContinue;
58+
}
59+
}
60+
61+
?>

MQ/Constants.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
namespace MQ;
3+
4+
class Constants
5+
{
6+
const GMT_DATE_FORMAT = "D, d M Y H:i:s \\G\\M\\T";
7+
8+
const VERSION_HEADER = "x-mq-version";
9+
const HEADER_PREFIX = "x-mq-";
10+
const XML_NAMESPACE = "http://mq.aliyuncs.com/doc/v1/";
11+
12+
const VERSION_VALUE = "2015-06-06";
13+
const AUTHORIZATION = "Authorization";
14+
const AUTH_PREFIX = "MQ";
15+
16+
const CONTENT_LENGTH = "Content-Length";
17+
const CONTENT_TYPE = "Content-Type";
18+
const SECURITY_TOKEN = "security-token";
19+
20+
// XML Tag
21+
const ERROR = "Error";
22+
const ERRORS = "Errors";
23+
const MESSAGE_BODY = "MessageBody";
24+
const MESSAGE_TAG = "MessageTag";
25+
const MESSAGE_ID = "MessageId";
26+
const MESSAGE_BODY_MD5 = "MessageBodyMD5";
27+
const PUBLISH_TIME = "PublishTime";
28+
const NEXT_CONSUME_TIME = "NextConsumeTime";
29+
const FIRST_CONSUME_TIME = "FirstConsumeTime";
30+
const RECEIPT_HANDLE = "ReceiptHandle";
31+
const RECEIPT_HANDLES = "ReceiptHandles";
32+
const CONSUMED_TIMES = "ConsumedTimes";
33+
const ERROR_CODE = "ErrorCode";
34+
const ERROR_MESSAGE = "ErrorMessage";
35+
36+
// some ErrorCodes
37+
const INVALID_ARGUMENT = "InvalidArgument";
38+
const MALFORMED_XML = "MalformedXML";
39+
const MESSAGE_NOT_EXIST = "MessageNotExist";
40+
const RECEIPT_HANDLE_ERROR = "ReceiptHandleError";
41+
const ACK_FAIL = "AckFail";
42+
43+
const TOPIC_NOT_EXIST = "TopicNotExist";
44+
}
45+
46+
?>

MQ/Exception/AckMessageException.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
namespace MQ\Exception;
3+
4+
use MQ\Constants;
5+
use MQ\Model\AckMessageErrorItem;
6+
7+
/**
8+
* Ack message could fail for some receipt handles,
9+
* and AckMessageException will be thrown.
10+
* All failed receiptHandles are saved in "$ackMessageErrorItems"
11+
*/
12+
class AckMessageException extends MQException
13+
{
14+
protected $ackMessageErrorItems;
15+
16+
public function __construct($code, $message, $previousException = NULL, $requestId = NULL, $hostId = NULL)
17+
{
18+
parent::__construct($code, $message, $previousException, Constants::ACK_FAIL, $requestId, $hostId);
19+
20+
$this->ackMessageErrorItems = array();
21+
}
22+
23+
public function addAckMessageErrorItem(AckMessageErrorItem $item)
24+
{
25+
$this->ackMessageErrorItems[] = $item;
26+
}
27+
28+
public function getAckMessageErrorItems()
29+
{
30+
return $this->ackMessageErrorItems;
31+
}
32+
}
33+
34+
?>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
namespace MQ\Exception;
3+
4+
class InvalidArgumentException extends MQException
5+
{
6+
}
7+
8+
?>

MQ/Exception/MQException.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
namespace MQ\Exception;
3+
4+
class MQException extends \RuntimeException
5+
{
6+
private $onsErrorCode;
7+
private $requestId;
8+
private $hostId;
9+
10+
public function __construct($code, $message, $previousException = NULL, $onsErrorCode = NULL, $requestId = NULL, $hostId = NULL)
11+
{
12+
parent::__construct($message, $code, $previousException);
13+
14+
if ($onsErrorCode == NULL)
15+
{
16+
if ($code >= 500)
17+
{
18+
$onsErrorCode = "ServerError";
19+
}
20+
else
21+
{
22+
$onsErrorCode = "ClientError";
23+
}
24+
}
25+
$this->onsErrorCode = $onsErrorCode;
26+
27+
$this->requestId = $requestId;
28+
$this->hostId = $hostId;
29+
}
30+
31+
public function __toString()
32+
{
33+
$str = "Code: " . $this->getCode() . " Message: " . $this->getMessage();
34+
if ($this->onsErrorCode != NULL)
35+
{
36+
$str .= " ErrorCode: " . $this->onsErrorCode;
37+
}
38+
if ($this->requestId != NULL)
39+
{
40+
$str .= " RequestId: " . $this->requestId;
41+
}
42+
if ($this->hostId != NULL)
43+
{
44+
$str .= " HostId: " . $this->hostId;
45+
}
46+
return $str;
47+
}
48+
49+
public function getOnsErrorCode()
50+
{
51+
return $this->onsErrorCode;
52+
}
53+
54+
public function getRequestId()
55+
{
56+
return $this->requestId;
57+
}
58+
59+
public function getHostId()
60+
{
61+
return $this->hostId;
62+
}
63+
}
64+
65+
?>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
namespace MQ\Exception;
3+
4+
class MalformedXMLException extends MQException
5+
{
6+
}
7+
8+
?>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
namespace MQ\Exception;
3+
4+
class MessageNotExistException extends MQException
5+
{
6+
}
7+
8+
?>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
namespace MQ\Exception;
3+
4+
class ReceiptHandleErrorException extends MQException
5+
{
6+
}
7+
8+
?>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
namespace MQ\Exception;
3+
4+
class TopicNotExistException extends MQException
5+
{
6+
}
7+
8+
?>

0 commit comments

Comments
 (0)