From 40d2d457f5675081fcfc3acc9257f2ad528fc53d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=80=99=E5=AD=A6=E6=9D=B0?= Date: Sat, 5 Jan 2019 12:47:51 +0800 Subject: [PATCH] lumen service provider --- src/Providers/LumenServerServiceProvider.php | 15 +++++++++-- src/Server/JsonRpcMethod.php | 2 ++ src/Server/JsonRpcServer.php | 27 ++++++++++++++++---- src/Server/JsonRpcTool.php | 16 ++++++++++++ 4 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 src/Server/JsonRpcTool.php diff --git a/src/Providers/LumenServerServiceProvider.php b/src/Providers/LumenServerServiceProvider.php index dfb2091..a07629f 100644 --- a/src/Providers/LumenServerServiceProvider.php +++ b/src/Providers/LumenServerServiceProvider.php @@ -1,4 +1,5 @@ 'rpc', ], function () { + $this->app->configure('rpc'); + $config = config('rpc.server'); - $callback = function () use($config) { + $callback = function () use ($config) { $server = new JsonRpcServer($config); return $server->handler(); }; @@ -35,10 +38,18 @@ class LumenServerServiceProvider extends ServiceProvider $this->app->router->post('json-rpc-v2.json', $callback); $this->app->router->get('json-rpc-v2.json', $callback); -// $this->app->router->get('doc.html', function () { + if (function_exists('env') && env('APP_DEBUG')) { + $this->app->router->get('tools.html', function () { + $doc = new JsonRpcTool(); + return $doc->render(); + }); + +// $this->app->router->get('doc.html', function () { // $doc = new JsonRpcDoc(base_path('app/Rpc/')); // return $doc->render(); // }); + + } }); } diff --git a/src/Server/JsonRpcMethod.php b/src/Server/JsonRpcMethod.php index 57509e0..a460436 100644 --- a/src/Server/JsonRpcMethod.php +++ b/src/Server/JsonRpcMethod.php @@ -2,6 +2,8 @@ namespace JsonRpc\Server; +use Illuminate\Http\JsonResponse; + class JsonRpcMethod { protected $id; diff --git a/src/Server/JsonRpcServer.php b/src/Server/JsonRpcServer.php index 3986815..fd7d184 100644 --- a/src/Server/JsonRpcServer.php +++ b/src/Server/JsonRpcServer.php @@ -29,18 +29,30 @@ class JsonRpcServer public function __construct($config) { + $this->config = $config; $this->request = function_exists('app') ? app('request') : Request::capture(); + + $this->map = require_once $config['map']; } public function handler() { - if ($this->request->getContentType() != 'json') { - return $this->error(self::Rpc_Error_Invalid_Request); - } +// if ($this->request->getContentType() != 'json') { +// return $this->error(self::Rpc_Error_Invalid_Request); +// } try { - list($method, $params, $id) = $this->parseJson($this->request->getContent()); - list($class, $function) = $this->parseMethod($method); + + if ($this->request->method() == Request::METHOD_GET) { + $method = $this->request->input('method'); + $id = $this->request->input('id'); + $params = \GuzzleHttp\json_decode($this->request->input('params'),true); + } else { + list($method, $params, $id) = $this->parseJson($this->request->getContent()); + } + + list($class, $function) = $this->parseMethodWithMap($method); +// dump($class,$function);exit; if (!class_exists($class) || !method_exists($class, $function)) { return $this->error(self::Rpc_Error_NOT_FOUND); @@ -68,6 +80,11 @@ class JsonRpcServer return [$method, $params, $id]; } + protected function parseMethodWithMap($method) + { + return isset($this->map[$method]) ? $this->map[$method] : ['', '']; + } + protected function parseMethod($method) { $method = explode('.', $method); diff --git a/src/Server/JsonRpcTool.php b/src/Server/JsonRpcTool.php new file mode 100644 index 0000000..a7c7825 --- /dev/null +++ b/src/Server/JsonRpcTool.php @@ -0,0 +1,16 @@ +