This commit is contained in:
候学杰 2019-01-07 15:32:24 +08:00
parent 39ad209ab4
commit 83c91ac7c0
3 changed files with 154 additions and 7 deletions

View File

@ -2,7 +2,9 @@
namespace JsonRpc\Providers; namespace JsonRpc\Providers;
use App\Http\Middleware\JsonRpc;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use JsonRpc\Server\JsonRpcDoc;
use JsonRpc\Server\JsonRpcServer; use JsonRpc\Server\JsonRpcServer;
use JsonRpc\Server\JsonRpcTool; use JsonRpc\Server\JsonRpcTool;
use Laravel\Lumen\Application; use Laravel\Lumen\Application;
@ -41,13 +43,18 @@ class LumenServerServiceProvider extends ServiceProvider
if (function_exists('env') && env('APP_DEBUG')) { if (function_exists('env') && env('APP_DEBUG')) {
$tool = function () use ($config) { $tool = function () use ($config) {
$doc = new JsonRpcTool($config); $tool = new JsonRpcTool($config);
return $doc->render(); return $tool->render();
}; };
$this->app->router->get('tool.html', $tool); $this->app->router->get('tool.html', $tool);
$this->app->router->post('tool.html', $tool); $this->app->router->post('tool.html', $tool);
$this->app->router->get('doc.html', function () use ($config) {
$doc = new JsonRpcDoc($config);
return $doc->render();
});
} }
}); });
} }

View File

@ -5,21 +5,55 @@ namespace JsonRpc\Server;
class JsonRpcDoc class JsonRpcDoc
{ {
protected $methods = []; protected $config;
public function __construct($dir) protected $classes;
public function __construct($config)
{ {
$this->methods = include $dir . '/methods.php'; $this->map = include $config['map'];
} }
public function methods() public function methods()
{ {
return $this->methods; $methods = [];
foreach ($this->map as $key => $item) {
$methods[] = [
'method' => $key,
'desc' => $this->desc($item[0], $item[1]),
];
}
return $methods;
} }
public function render() public function render()
{ {
return view('doc', ['doc' => $this]);
/**
* @var $view Factory
*/
$view = view();
dump($this->methods());
exit;
$view->share('methods', $this->methods());
return $view->exists('doc') ?
$view->make('doc') :
$view->file(__DIR__ . '/../views/doc.blade.php');
}
protected function desc($class, $method)
{
if (!isset($this->classes[$class])) {
$reflector = new \ReflectionClass($class);
$this->classes[$class] = $reflector;
} else {
$reflector = $this->classes[$class];
}
return str_replace("/**\n",'',$reflector->getMethod($method)->getDocComment());
} }
} }

106
src/views/doc.blade.php Normal file
View File

@ -0,0 +1,106 @@
<html>
<head>
<title>Json Rpc Doc</title>
<link href="https://cdn.bootcss.com/twitter-bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.bootcss.com/highlight.js/9.13.1/styles/ocean.min.css" rel="stylesheet">
</head>
<body>
<nav class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0">
<a class="navbar-brand col-sm-3 col-md-2 mr-0" href="#">Json Rpc Doc</a>
{{--<input class="form-control form-control-dark w-100" type="text" placeholder="Search" aria-label="Search">--}}
{{--<ul class="navbar-nav px-3">--}}
{{--<li class="nav-item text-nowrap">--}}
{{--<a class="nav-link" href="#">Sign out</a>--}}
{{--</li>--}}
{{--</ul>--}}
</nav>
<div class="container-fluid">
<div class="row">
<nav class="col-md-3 d-none d-md-block bg-light sidebar">
<div class="sidebar-sticky">
<ul class="nav flex-column">
<li class="nav-item">
<a class="nav-link active" href="#">
<span data-feather="home"></span>
Method <span class="sr-only">(current)</span>
</a>
</li>
@foreach( $methods as $method )
<li class="nav-item">
<a class="nav-link" href="#{{$method['method']}}">
<span data-feather="file"></span>
{{$method['method']}}
</a>
</li>
@endforeach
</ul>
</div>
</nav>
<main role="main" class="col-md-8 ml-sm-auto col-lg-9 pt-3 px-4">
<h2>Request</h2>
<form method="POST">
<div class="form-row">
<div class="form-group col-md-12">
<label for="endpoint">Endpoint</label>
{{--<input type="text" class="form-control" id="endpoint" placeholder="Endpoint"--}}
{{-- value="{{$endpoint}}" readonly>--}}
</div>
{{--<div class="form-group col-md-2">--}}
{{--<label for="method">Request Method</label>--}}
{{--<select class="form-control" id="method">--}}
{{--<option>GET</option>--}}
{{--<option>POST</option>--}}
{{--</select>--}}
{{--</div>--}}
</div>
<div class="form-row">
<div class="form-group col-md-12">
<label for="inputAddress">Method</label>
<select class="form-control" id="method" name="method">
{{--@foreach($methods as $k => $v)--}}
{{--<option>{{$k}}</option>--}}
{{--@endforeach--}}
</select>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-12">
<label for="inputAddress">Params</label>
{{--<input type="text" class="form-control" name="params" id="params" placeholder="逗号分隔"--}}
{{--value="{{$params}}">--}}
</div>
</div>
<button type="submit" class="btn btn-primary">Request</button>
</form>
<div class="row col-md-12">
{{--@if( !empty($error) )--}}
{{--<div id='alert' class="alert alert-danger" role="alert">--}}
{{--RpcServerException: {{$error['message']}} with code {{$error['code']}}--}}
{{--</div>--}}
{{--@endif--}}
{{--@if( !empty($result) )--}}
{{--<h5>Result:</h5>--}}
{{--<div class="col-md-12">--}}
{{--<pre><code class="json">{{$result}}</code></pre>--}}
{{--</div>--}}
{{--@endif--}}
</div>
</main>
</div>
</div>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.slim.js"><\/script>')</script>
<script src="https://cdn.bootcss.com/twitter-bootstrap/4.2.1/js/bootstrap.min.js"></script>
<script src="https://cdn.bootcss.com/highlight.js/9.13.1/highlight.min.js"></script>
<script src="https://cdn.bootcss.com/highlight.js/9.13.1/languages/json.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
</body>
</html>