1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ABM\Kilns\Module; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Request; |
6
|
|
|
use GuzzleHttp\Psr7\Response; |
7
|
|
|
|
8
|
|
|
abstract class Core |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* $serverHost |
12
|
|
|
* 接口域名. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $serverHost = ''; |
17
|
|
|
/** |
18
|
|
|
* $secretKey |
19
|
|
|
* secretKey. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $secretKey = ''; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* __construct. |
27
|
|
|
* |
28
|
|
|
* @param array $config [description] |
29
|
|
|
*/ |
30
|
|
|
public function __construct($config = []) |
31
|
|
|
{ |
32
|
|
|
if (!empty($config)) { |
33
|
|
|
$this->setConfig($config); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* setConfig |
39
|
|
|
* 设置配置. |
40
|
|
|
* |
41
|
|
|
* @param array $config 模块配置 |
42
|
|
|
*/ |
43
|
|
|
public function setConfig($config) |
44
|
|
|
{ |
45
|
|
|
if (!is_array($config) || !count($config)) { |
46
|
|
|
return false; |
47
|
|
|
} |
48
|
|
|
foreach ($config as $key => $val) { |
49
|
|
|
switch ($key) { |
50
|
|
|
case 'Subscription-Key': |
51
|
|
|
$this->setConfigSecretKey($val); |
52
|
|
|
break; |
53
|
|
|
case 'Content-Type': |
54
|
|
|
$this->setConfigContentType($val); |
55
|
|
|
break; |
56
|
|
|
case 'Request-Method': |
57
|
|
|
$this->setConfigRequestMethod($val); |
58
|
|
|
break; |
59
|
|
|
default: |
60
|
|
|
break; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return true; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* setConfigSecretKey |
69
|
|
|
* 设置secretKey. |
70
|
|
|
* |
71
|
|
|
* @param string $secretKey |
72
|
|
|
*/ |
73
|
|
|
public function setConfigSecretKey($secretKey) |
74
|
|
|
{ |
75
|
|
|
$this->secretKey = $secretKey; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* setConfigRequestMethod |
82
|
|
|
* 设置请求方法. |
83
|
|
|
* |
84
|
|
|
* @param string $method |
|
|
|
|
85
|
|
|
*/ |
86
|
|
|
public function setConfigContentType($contentType) |
87
|
|
|
{ |
88
|
|
|
$this->contentType = strtoupper($contentType); |
|
|
|
|
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* setConfigSecretKey |
95
|
|
|
* 设置secretKey. |
96
|
|
|
* |
97
|
|
|
* @param string $secretKey |
|
|
|
|
98
|
|
|
*/ |
99
|
|
|
public function setConfigRequestMethod($requestMethod) |
100
|
|
|
{ |
101
|
|
|
$this->requestMethod = $requestMethod; |
|
|
|
|
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* getLastRequest |
108
|
|
|
* 获取上次请求的url. |
109
|
|
|
* |
110
|
|
|
* @return |
111
|
|
|
*/ |
112
|
|
|
public function getLastRequest() |
113
|
|
|
{ |
114
|
|
|
$response = new Response(); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* getLastResponse |
119
|
|
|
* 获取请求的原始返回. |
120
|
|
|
* |
121
|
|
|
* @return |
122
|
|
|
*/ |
123
|
|
|
public function getLastResponse($response) |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
$response = new Response(); |
126
|
|
|
|
127
|
|
|
return $response; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* generateUrl |
132
|
|
|
* 生成请求的URL,不发起请求 |
133
|
|
|
* |
134
|
|
|
* @param string $name 接口方法名 |
135
|
|
|
* @param array $params 请求参数 |
136
|
|
|
* @param string $body 请求Body |
137
|
|
|
* |
138
|
|
|
* @return |
139
|
|
|
*/ |
140
|
|
|
public function generateUrl($name, $params, $body) |
141
|
|
|
{ |
142
|
|
|
$action = ucfirst($name); |
|
|
|
|
143
|
|
|
|
144
|
|
|
return $this->generateUrlBody($params, $this->secretKey, $this->contentType, $this->requestMethod, $this->serverHost.$name, $body); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* generateUrlBody |
149
|
|
|
* 生成请求的URLBody. |
150
|
|
|
* |
151
|
|
|
* @param array $paramArray 请求参数 |
152
|
|
|
* @param string $secretKey 订阅密钥 |
153
|
|
|
* @param string $ContentType 请求Body的类型 |
|
|
|
|
154
|
|
|
* @param string $requestMethod 请求方式,GET/POST |
155
|
|
|
* @param string $url 接口URL |
156
|
|
|
* @param string $body 请求Body |
157
|
|
|
* |
158
|
|
|
* @return |
159
|
|
|
*/ |
160
|
|
|
public static function generateUrlBody($paramArray, $secretKey, $contentType, $requestMethod, $url, $body) |
161
|
|
|
{ |
162
|
|
|
$header = []; |
163
|
|
|
$urlBopy = []; |
|
|
|
|
164
|
|
|
if ($contentType) { |
165
|
|
|
$header[] = 'Content-Type:'.strtolower($contentType); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$header[] = 'Ocp-Apim-Subscription-Key:'.$secretKey; |
169
|
|
|
switch ($requestMethod) { |
170
|
|
|
case 'GET': |
171
|
|
|
$url .= '&'.http_build_query($body); |
172
|
|
|
break; |
173
|
|
|
default: |
174
|
|
|
$url .= '?'.http_build_query($paramArray); |
175
|
|
|
$urlBody['body'] = $body; |
|
|
|
|
176
|
|
|
break; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$urlBody = array_merge(['url' => $url, 'method' => $requestMethod, 'header' => $header], $urlBody); |
|
|
|
|
180
|
|
|
|
181
|
|
|
return $urlBody; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* __call |
186
|
|
|
* 通过__call转发请求 |
187
|
|
|
* |
188
|
|
|
* @param string $name 方法名 |
189
|
|
|
* @param array $arguments 参数 |
190
|
|
|
* |
191
|
|
|
* @return |
192
|
|
|
*/ |
193
|
|
|
public function call($name, $arguments) |
194
|
|
|
{ |
195
|
|
|
require_once Kilns_ROOT_PATH.'/Module/Core.php'; |
196
|
|
|
$response = $this->dispatchRequest($name, $arguments); |
197
|
|
|
|
198
|
|
|
return $this->dealResponse($response); |
|
|
|
|
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* _dispatchRequest |
203
|
|
|
* 发起接口请求 |
204
|
|
|
* |
205
|
|
|
* @param string $name 接口名 |
206
|
|
|
* @param array $arguments 接口参数 |
207
|
|
|
* |
208
|
|
|
* @return |
209
|
|
|
*/ |
210
|
|
|
protected function dispatchRequest($name, $arguments) |
211
|
|
|
{ |
212
|
|
|
$action = ucfirst($name); |
213
|
|
|
$params = []; |
214
|
|
|
if (is_array($arguments) && !empty($arguments)) { |
215
|
|
|
$params[0] = (array) $arguments[0]; |
216
|
|
|
} |
217
|
|
|
if (is_array($arguments) && !empty($arguments)) { |
218
|
|
|
$params[1] = $arguments[1]; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
$response = $this->send($params, $this->_secretKey, $this->_contentType, $this->_requestMethod, $this->_serverHost.$action); |
|
|
|
|
222
|
|
|
|
223
|
|
|
return $response; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* send |
228
|
|
|
* 发起请求 |
229
|
|
|
* |
230
|
|
|
* @param array $paramArray 请求参数 |
231
|
|
|
* @param string $secretKey 订阅密钥 |
232
|
|
|
* @param string $ContentType 请求Body的类型 |
|
|
|
|
233
|
|
|
* @param string $requestMethod 请求方式,GET/POST |
234
|
|
|
* @param string $url 接口URL |
|
|
|
|
235
|
|
|
* @param string $body 请求Body |
|
|
|
|
236
|
|
|
* |
237
|
|
|
* @return |
238
|
|
|
*/ |
239
|
|
|
public static function send($paramArray, $secretKey, $contentType, $requestMethod, $requestHost) |
240
|
|
|
{ |
241
|
|
|
$param = $paramArray[0]; |
242
|
|
|
$body = $paramArray[1]; |
243
|
|
|
if ($contentType) { |
244
|
|
|
$header[] = 'Content-Type:'.strtolower($contentType); |
|
|
|
|
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
$header[] = 'Ocp-Apim-Subscription-Key:'.$secretKey; |
|
|
|
|
248
|
|
|
|
249
|
|
|
$url = $requestHost; |
250
|
|
|
|
251
|
|
|
switch ($requestMethod) { |
252
|
|
|
case 'POST': |
253
|
|
|
$url .= '?'.http_build_query($param); |
254
|
|
|
break; |
255
|
|
|
default: |
256
|
|
|
$url .= '&'.http_build_query($body); |
257
|
|
|
break; |
258
|
|
|
} |
259
|
|
|
$request = new Request($requestMethod, $url, $header, $body); |
260
|
|
|
|
261
|
|
|
return $request; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* _dealResponse |
266
|
|
|
* 处理返回. |
267
|
|
|
* |
268
|
|
|
* @param array $response |
269
|
|
|
* |
270
|
|
|
* @return |
271
|
|
|
*/ |
272
|
|
|
protected function dealResponse($response) |
273
|
|
|
{ |
274
|
|
|
$phrase = $response->getReasonPhrase(); |
|
|
|
|
275
|
|
|
if ($phrase !== 'OK') { |
276
|
|
|
echo $response->getStatusCode(); |
|
|
|
|
277
|
|
|
echo $response->getBody(); |
|
|
|
|
278
|
|
|
|
279
|
|
|
return false; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
if (count($response)) { |
283
|
|
|
return $response; |
284
|
|
|
} else { |
285
|
|
|
return true; |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.