lumen service provider

This commit is contained in:
候学杰 2019-01-05 12:47:51 +08:00
parent e98954ec60
commit 40d2d457f5
4 changed files with 53 additions and 7 deletions

View File

@ -1,4 +1,5 @@
<?php <?php
namespace JsonRpc\Providers; namespace JsonRpc\Providers;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
@ -25,9 +26,11 @@ class LumenServerServiceProvider extends ServiceProvider
// 'middleware' => 'rpc', // 'middleware' => 'rpc',
], function () { ], function () {
$this->app->configure('rpc');
$config = config('rpc.server'); $config = config('rpc.server');
$callback = function () use($config) { $callback = function () use ($config) {
$server = new JsonRpcServer($config); $server = new JsonRpcServer($config);
return $server->handler(); return $server->handler();
}; };
@ -35,10 +38,18 @@ class LumenServerServiceProvider extends ServiceProvider
$this->app->router->post('json-rpc-v2.json', $callback); $this->app->router->post('json-rpc-v2.json', $callback);
$this->app->router->get('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/')); // $doc = new JsonRpcDoc(base_path('app/Rpc/'));
// return $doc->render(); // return $doc->render();
// }); // });
}
}); });
} }

View File

@ -2,6 +2,8 @@
namespace JsonRpc\Server; namespace JsonRpc\Server;
use Illuminate\Http\JsonResponse;
class JsonRpcMethod class JsonRpcMethod
{ {
protected $id; protected $id;

View File

@ -29,18 +29,30 @@ class JsonRpcServer
public function __construct($config) public function __construct($config)
{ {
$this->config = $config;
$this->request = function_exists('app') ? app('request') : Request::capture(); $this->request = function_exists('app') ? app('request') : Request::capture();
$this->map = require_once $config['map'];
} }
public function handler() public function handler()
{ {
if ($this->request->getContentType() != 'json') { // if ($this->request->getContentType() != 'json') {
return $this->error(self::Rpc_Error_Invalid_Request); // return $this->error(self::Rpc_Error_Invalid_Request);
} // }
try { 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)) { if (!class_exists($class) || !method_exists($class, $function)) {
return $this->error(self::Rpc_Error_NOT_FOUND); return $this->error(self::Rpc_Error_NOT_FOUND);
@ -68,6 +80,11 @@ class JsonRpcServer
return [$method, $params, $id]; return [$method, $params, $id];
} }
protected function parseMethodWithMap($method)
{
return isset($this->map[$method]) ? $this->map[$method] : ['', ''];
}
protected function parseMethod($method) protected function parseMethod($method)
{ {
$method = explode('.', $method); $method = explode('.', $method);

View File

@ -0,0 +1,16 @@
<?php
namespace JsonRpc\Server;
class JsonRpcTool
{
public function __construct()
{
}
public function render()
{
return file_get_contents('abc.html');
}
}