ComposerUpdate

This commit is contained in:
Anyon 2019-12-16 11:43:34 +08:00
parent 6cb1f12827
commit 24a1a67e27
5 changed files with 46 additions and 83 deletions

8
composer.lock generated
View File

@ -909,12 +909,12 @@
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/zoujingli/ThinkLibrary.git", "url": "https://github.com/zoujingli/ThinkLibrary.git",
"reference": "211f7afd9d19f9165989a5ecea78a828996e9c3f" "reference": "ca6df0b64b3d22c210260a218bb31134661c881d"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/211f7afd9d19f9165989a5ecea78a828996e9c3f", "url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/ca6df0b64b3d22c210260a218bb31134661c881d",
"reference": "211f7afd9d19f9165989a5ecea78a828996e9c3f", "reference": "ca6df0b64b3d22c210260a218bb31134661c881d",
"shasum": "", "shasum": "",
"mirrors": [ "mirrors": [
{ {
@ -958,7 +958,7 @@
], ],
"description": "ThinkPHP v6.0 Development Library", "description": "ThinkPHP v6.0 Development Library",
"homepage": "http://framework.thinkadmin.top", "homepage": "http://framework.thinkadmin.top",
"time": "2019-12-16T02:57:39+00:00" "time": "2019-12-16T03:27:06+00:00"
}, },
{ {
"name": "zoujingli/wechat-developer", "name": "zoujingli/wechat-developer",

View File

@ -935,12 +935,12 @@
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/zoujingli/ThinkLibrary.git", "url": "https://github.com/zoujingli/ThinkLibrary.git",
"reference": "211f7afd9d19f9165989a5ecea78a828996e9c3f" "reference": "ca6df0b64b3d22c210260a218bb31134661c881d"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/211f7afd9d19f9165989a5ecea78a828996e9c3f", "url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/ca6df0b64b3d22c210260a218bb31134661c881d",
"reference": "211f7afd9d19f9165989a5ecea78a828996e9c3f", "reference": "ca6df0b64b3d22c210260a218bb31134661c881d",
"shasum": "", "shasum": "",
"mirrors": [ "mirrors": [
{ {
@ -956,7 +956,7 @@
"ext-json": "*", "ext-json": "*",
"topthink/framework": "^6.0" "topthink/framework": "^6.0"
}, },
"time": "2019-12-16T02:57:39+00:00", "time": "2019-12-16T03:27:06+00:00",
"type": "library", "type": "library",
"extra": { "extra": {
"think": { "think": {

2
vendor/services.php vendored
View File

@ -1,5 +1,5 @@
<?php <?php
// This file is automatically generated at:2019-12-16 11:11:22 // This file is automatically generated at:2019-12-16 11:42:37
declare (strict_types = 1); declare (strict_types = 1);
return array ( return array (
0 => 'think\\app\\Service', 0 => 'think\\app\\Service',

View File

@ -25,12 +25,6 @@ use think\admin\Service;
*/ */
class JsonRpcClientService extends Service class JsonRpcClientService extends Service
{ {
/**
* 调式状态
* @var boolean
*/
private $debug;
/** /**
* 服务端地址 * 服务端地址
* @var string * @var string
@ -41,37 +35,20 @@ class JsonRpcClientService extends Service
* 请求ID * 请求ID
* @var integer * @var integer
*/ */
private $requestId; private $requestid;
/**
* 通知状态
* @var boolean
*/
private $notification = false;
/** /**
* 创建连接对象 * 创建连接对象
* @param string $proxy * @param string $proxy
* @param boolean $debug
* @return $this * @return $this
*/ */
public function create($proxy, $debug = false) public function create($proxy)
{ {
$this->proxy = $proxy; $this->proxy = $proxy;
$this->debug = empty($debug) ? false : true; $this->requestid = CodeExtend::uniqidNumber();
$this->requestId = CodeExtend::uniqidNumber();
return $this; return $this;
} }
/**
* 设置对象的通知状态(在此状态下,将执行通知而不是请求)
* @param boolean $notification
*/
public function setRpcNotification($notification)
{
$this->notification = empty($notification) ? false : true;
}
/** /**
* 执行 JsonRCP 请求 * 执行 JsonRCP 请求
* @param string $method * @param string $method
@ -81,53 +58,39 @@ class JsonRpcClientService extends Service
*/ */
public function __call($method, $params) public function __call($method, $params)
{ {
// check // check
if (!is_scalar($method)) { if (!is_scalar($method)) {
throw new \think\Exception('Method name has no scalar value'); throw new \think\Exception('Method name has no scalar value');
} }
// check // check
if (is_array($params)) { if (is_array($params)) {
// no keys
$params = array_values($params); $params = array_values($params);
} else { } else {
throw new \think\Exception('Params must be given as array'); throw new \think\Exception('Params must be given as array');
} }
// sets notification or request task
$currentId = $this->notification ? null : $this->requestId;
// prepares the request
$request = json_encode(['method' => $method, 'params' => $params, 'id' => $currentId], JSON_UNESCAPED_UNICODE);
$this->debug && $this->debug .= '***** Request *****' . "\n" . $request . "\n" . '***** End Of request *****' . "\n\n";
// performs the HTTP POST // performs the HTTP POST
$options = ['http' => ['method' => 'POST', 'header' => 'Content-type: application/json', 'content' => $request]]; $options = [
'http' => [
'method' => 'POST', 'header' => 'Content-type: application/json',
'content' => json_encode(['method' => $method, 'params' => $params, 'id' => $this->requestid], JSON_UNESCAPED_UNICODE),
],
];
if ($fp = fopen($this->proxy, 'r', false, stream_context_create($options))) { if ($fp = fopen($this->proxy, 'r', false, stream_context_create($options))) {
$response = ''; $response = '';
while ($row = fgets($fp)) $response .= trim($row) . "\n"; while ($row = fgets($fp)) $response .= trim($row) . "\n";
$this->debug && $this->debug .= '***** Server response *****' . "\n" . $response . '***** End of server response *****' . "\n"; fclose($fp);
$response = json_decode($response, true); $response = json_decode($response, true);
} else { } else {
throw new \think\Exception("Unable to connect to {$this->proxy}"); throw new \think\Exception("Unable to connect to {$this->proxy}");
} }
// debug output
if ($this->debug) {
echo nl2br($this->debug);
}
// final checks and return // final checks and return
if ($this->notification) { if ($response['id'] != $this->requestid) {
return true; throw new \think\Exception("Incorrect response id (request id: {$this->requestid}, response id: {$response['id']}");
} else { }
// check if (is_null($response['error'])) {
if ($response['id'] != $currentId) {
throw new \think\Exception("Incorrect response id (request id: {$currentId}, response id: {$response['id']}");
}
if (!is_null($response['error'])) {
throw new \think\Exception("Request error: {$response['error']}");
}
return $response['result']; return $response['result'];
} else {
throw new \think\Exception("Request error: {$response['error']}");
} }
} }
} }

View File

@ -28,35 +28,35 @@ class JsonRpcServerService extends Service
/** /**
* 设置监听对象 * 设置监听对象
* @param mixed $object * @param mixed $object
* @return boolean
* @throws \think\Exception * @throws \think\Exception
*/ */
public function handle($object) public function handle($object)
{ {
// Checks if a JSON-RCP request has been received // Checks if a JSON-RCP request has been received
if ($this->app->request->method() !== "POST" || $this->app->request->contentType() != 'application/json') { if ($this->app->request->method() !== "POST" || $this->app->request->contentType() != 'application/json') {
foreach (get_class_methods($object) as $method) echo "<p>method {$method}()</p>"; echo "<h2>" . get_class($object) . "</h2>";
return false; foreach (get_class_methods($object) as $method) {
} if ($method[0] !== '_') echo "<p>method {$method}()</p>";
// Reads the input data
$request = json_decode(file_get_contents('php://input'), true);
if (empty($request['id'])) {
throw new \think\Exception('JsonRpc Request id cannot be empty');
}
// Executes the task on local object
try {
if ($result = call_user_func_array([$object, $request['method']], $request['params'])) {
$response = ['id' => $request['id'], 'result' => $result, 'error' => null];
} else {
$response = ['id' => $request['id'], 'result' => null, 'error' => 'unknown method or incorrect parameters'];
} }
} catch (\Exception $e) { } else {
$response = ['id' => $request['id'], 'result' => null, 'error' => $e->getMessage()]; // Reads the input data
$request = json_decode(file_get_contents('php://input'), true);
if (empty($request['id'])) {
throw new \think\Exception('JsonRpc Request id cannot be empty');
}
// Executes the task on local object
try {
if ($result = @call_user_func_array([$object, $request['method']], $request['params'])) {
$response = ['id' => $request['id'], 'result' => $result, 'error' => null];
} else {
$response = ['id' => $request['id'], 'result' => null, 'error' => 'unknown method or incorrect parameters'];
}
} catch (\Exception $e) {
$response = ['id' => $request['id'], 'result' => null, 'error' => $e->getMessage()];
}
// Output the response
throw new HttpResponseException(json($response)->contentType('text/javascript'));
} }
// Output the response
throw new HttpResponseException(json($response)->contentType('text/javascript'));
} }
} }