lumen service provider
This commit is contained in:
parent
e98954ec60
commit
40d2d457f5
|
@ -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);
|
||||||
|
|
||||||
|
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 () {
|
// $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();
|
||||||
// });
|
// });
|
||||||
|
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
namespace JsonRpc\Server;
|
namespace JsonRpc\Server;
|
||||||
|
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
|
||||||
class JsonRpcMethod
|
class JsonRpcMethod
|
||||||
{
|
{
|
||||||
protected $id;
|
protected $id;
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
||||||
|
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($method, $params, $id) = $this->parseJson($this->request->getContent());
|
||||||
list($class, $function) = $this->parseMethod($method);
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
16
src/Server/JsonRpcTool.php
Normal file
16
src/Server/JsonRpcTool.php
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace JsonRpc\Server;
|
||||||
|
|
||||||
|
class JsonRpcTool
|
||||||
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function render()
|
||||||
|
{
|
||||||
|
return file_get_contents('abc.html');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user