|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Freyo\LaravelQueueCMQ\Queue\Driver; |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
Account 对象非线程安全,如果多线程使用,需要每个线程单独初始化Account对象类 |
|
7
|
|
|
*/ |
|
8
|
|
|
class Account |
|
9
|
|
|
{ |
|
10
|
|
|
private $secretId; |
|
11
|
|
|
private $secretKey; |
|
12
|
|
|
private $cmq_client; |
|
13
|
|
|
|
|
14
|
|
|
/* |
|
15
|
|
|
@type host: string |
|
16
|
|
|
@param host: 访问的url,例如:https://cmq-queue-gz.api.qcloud.com |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
@type secretId: string |
|
19
|
|
|
@param secretId: 用户的secretId, 腾讯云官网获取 |
|
20
|
|
|
|
|
21
|
|
|
@type secretKey: string |
|
22
|
|
|
@param secretKey: 用户的secretKey,腾讯云官网获取 |
|
23
|
|
|
|
|
24
|
|
|
@note: Exception |
|
25
|
|
|
:: CMQClientParameterException host格式错误 |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
public function __construct($host, $secretId, $secretKey) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->host = $host; |
|
|
|
|
|
|
31
|
|
|
$this->secretId = $secretId; |
|
32
|
|
|
$this->secretKey = $secretKey; |
|
33
|
|
|
$this->cmq_client = new CMQClient($host, $secretId, $secretKey); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/* |
|
37
|
|
|
* @type sign_method:string |
|
38
|
|
|
* @param sign_method : only support sha1 and sha256 |
|
|
|
|
|
|
39
|
|
|
*/ |
|
40
|
|
|
public function set_sign_method($sign_method = 'sha1') |
|
41
|
|
|
{ |
|
42
|
|
|
$this->cmq_client->set_sign_method($sign_method); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/* 设置访问的url |
|
46
|
|
|
|
|
47
|
|
|
@type host: string |
|
48
|
|
|
@param host: 访问的url,例如:http://cmq-queue-gz.api.tencentyun.com |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
@type secretId: string |
|
51
|
|
|
@param secretId: 用户的secretId,腾讯云官网获取 |
|
52
|
|
|
|
|
53
|
|
|
@type secretKey: string |
|
54
|
|
|
@param secretKey: 用户的secretKey,腾讯云官网获取 |
|
55
|
|
|
|
|
56
|
|
|
@note: Exception |
|
57
|
|
|
:: CMQClientParameterException host格式错误 |
|
58
|
|
|
*/ |
|
59
|
|
|
public function set_client($host, $secretId = null, $secretKey = null) |
|
60
|
|
|
{ |
|
61
|
|
|
if ($secretId == null) { |
|
62
|
|
|
$secretId = $this->secretId; |
|
63
|
|
|
} |
|
64
|
|
|
if ($secretKey == null) { |
|
65
|
|
|
$secretKey = $this->secretKey; |
|
66
|
|
|
} |
|
67
|
|
|
$this->cmq_client = new CMQClient($host, $secretId, $secretKey); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/* 获取queue client |
|
71
|
|
|
|
|
72
|
|
|
@rtype: CMQClient object |
|
73
|
|
|
@return: 返回使用的CMQClient object |
|
74
|
|
|
*/ |
|
75
|
|
|
public function get_client() |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->cmq_client; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/* 获取Account的一个Queue对象 |
|
81
|
|
|
|
|
82
|
|
|
@type queue_name: string |
|
83
|
|
|
@param queue_name: 队列名 |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
@rtype: Queue object |
|
86
|
|
|
@return: 返回该Account的一个Queue对象 |
|
87
|
|
|
*/ |
|
88
|
|
|
public function get_queue($queue_name) |
|
89
|
|
|
{ |
|
90
|
|
|
return new Queue($queue_name, $this->cmq_client); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/* 列出Account的队列 |
|
94
|
|
|
|
|
95
|
|
|
@type searchWord: string |
|
96
|
|
|
@param searchWord: 队列名的前缀 |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
@type limit: int |
|
99
|
|
|
@param limit: list_queue最多返回的队列数 |
|
100
|
|
|
|
|
101
|
|
|
@type offset: string |
|
102
|
|
|
@param offset: list_queue的起始位置,上次list_queue返回的next_offset |
|
103
|
|
|
|
|
104
|
|
|
@rtype: tuple |
|
105
|
|
|
@return: QueueURL的列表和下次list queue的起始位置; 如果所有queue都list出来,next_offset为"". |
|
106
|
|
|
*/ |
|
107
|
|
|
public function list_queue($searchWord = '', $limit = -1, $offset = '') |
|
108
|
|
|
{ |
|
109
|
|
|
$params = []; |
|
110
|
|
|
if ($searchWord != '') { |
|
111
|
|
|
$params['searchWord'] = $searchWord; |
|
112
|
|
|
} |
|
113
|
|
|
if ($limit != -1) { |
|
114
|
|
|
$params['limit'] = $limit; |
|
115
|
|
|
} |
|
116
|
|
|
if ($offset != '') { |
|
117
|
|
|
$params['offset'] = $offset; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$ret_pkg = $this->cmq_client->list_queue($params); |
|
121
|
|
|
|
|
122
|
|
|
if ($offset == '') { |
|
123
|
|
|
$next_offset = count($ret_pkg['queueList']); |
|
124
|
|
|
} else { |
|
125
|
|
|
$next_offset = (int) $offset + count($ret_pkg['queueList']); |
|
126
|
|
|
} |
|
127
|
|
|
if ($next_offset >= $ret_pkg['totalCount']) { |
|
128
|
|
|
$next_offset = ''; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return ['totalCount' => $ret_pkg['totalCount'], |
|
132
|
|
|
'queueList' => $ret_pkg['queueList'], 'next_offset' => $next_offset, ]; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/* 列出Account的主题 |
|
136
|
|
|
|
|
137
|
|
|
@type searchWord: string |
|
138
|
|
|
@param searchWord: 主题关键字 |
|
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
@type limit: int |
|
141
|
|
|
@param limit: 最多返回的主题数目 |
|
142
|
|
|
|
|
143
|
|
|
@type offset: string |
|
144
|
|
|
@param offset: list_topic的起始位置,上次list_topic返回的next_offset |
|
145
|
|
|
|
|
146
|
|
|
@rtype: tuple |
|
147
|
|
|
@return: TopicURL的列表和下次list topic的起始位置; 如果所有topic都list出来,next_offset为"". |
|
148
|
|
|
*/ |
|
149
|
|
|
public function list_topic($searchWord = '', $limit = -1, $offset = '') |
|
150
|
|
|
{ |
|
151
|
|
|
$params = []; |
|
152
|
|
|
if ($searchWord != '') { |
|
153
|
|
|
$params['searchWord'] = $searchWord; |
|
154
|
|
|
} |
|
155
|
|
|
if ($limit != -1) { |
|
156
|
|
|
$params['limit'] = $limit; |
|
157
|
|
|
} |
|
158
|
|
|
if ($offset != '') { |
|
159
|
|
|
$params['offset'] = $offset; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
$resp = $this->cmq_client->list_topic($params); |
|
163
|
|
|
|
|
164
|
|
|
if ($offset == '') { |
|
165
|
|
|
$next_offset = count($resp['topicList']); |
|
166
|
|
|
} else { |
|
167
|
|
|
$next_offset = (int) $offset + count($resp['topicList']); |
|
168
|
|
|
} |
|
169
|
|
|
if ($next_offset >= $resp['totalCount']) { |
|
170
|
|
|
$next_offset = ''; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
return ['totalCoult' => $resp['totalCount'], |
|
174
|
|
|
'topicList' => $resp['topicList'], |
|
175
|
|
|
'next_offset' => $next_offset, ]; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/* 获取Account的一个Topic对象 |
|
179
|
|
|
|
|
180
|
|
|
@type topic_name: string |
|
181
|
|
|
@param queue_name: |
|
182
|
|
|
|
|
183
|
|
|
@rtype: Topic object |
|
184
|
|
|
@return: 返回该Account的一个Topic对象 |
|
185
|
|
|
*/ |
|
|
|
|
|
|
186
|
|
|
public function get_topic($topic_name) |
|
187
|
|
|
{ |
|
188
|
|
|
return new Topic($topic_name, $this->cmq_client); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/* 获取Account的一个Subscription对象 |
|
192
|
|
|
|
|
193
|
|
|
@type topic_name: string |
|
194
|
|
|
@param queue_name: |
|
195
|
|
|
|
|
196
|
|
|
@type subscription_name :string |
|
197
|
|
|
@param subscription_name: |
|
198
|
|
|
|
|
199
|
|
|
@rtype: Subscription object |
|
200
|
|
|
@return: 返回该Account的一个Subscription对象 |
|
201
|
|
|
*/ |
|
|
|
|
|
|
202
|
|
|
public function get_subscription($topic_name, $subscription_name) |
|
203
|
|
|
{ |
|
204
|
|
|
return new Subscription($topic_name, $subscription_name, $this->cmq_client); |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|