Skip to content

Commit 8e7a515

Browse files
committed
增加代码模板
1 parent 33a9f1f commit 8e7a515

File tree

6 files changed

+124
-2
lines changed

6 files changed

+124
-2
lines changed

src/Controller.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace hg\apidoc;
55

66
use hg\apidoc\parses\ParseApiDetail;
7+
use hg\apidoc\parses\ParseCodeTemplate;
78
use hg\apidoc\parses\ParseApiMenus;
89
use hg\apidoc\parses\ParseMarkdown;
910
use hg\apidoc\utils\Cache;
@@ -255,4 +256,18 @@ public function createAllCache(){
255256
return Helper::showJson(0,"",true);
256257
}
257258

259+
public function renderCodeTemplate(){
260+
$this->init(true);
261+
$config = $this->config;
262+
$params = $this->requestParams;
263+
264+
$code = (new ParseCodeTemplate($config))->renderCode($params);
265+
266+
return Helper::showJson(0,"",[
267+
'code'=>$code
268+
]);
269+
270+
}
271+
272+
258273
}

src/parses/ParseApiDetail.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ protected function handleParamAndReturned($params,$annotation,$type="param",$ena
689689
$param['children'][] = [
690690
'children' => $children['params'],
691691
'name' => !empty($annotation->childrenField)?$annotation->childrenField:"children",
692-
'type' => 'array',
692+
'type' => 'array<object>',
693693
'desc' => Lang::getLang($annotation->childrenDesc),
694694
];
695695
}

src/parses/ParseApiMenus.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ protected function scanDir($dir) {
177177
return $list;
178178
}
179179

180-
protected function parseController($class)
180+
public function parseController($class)
181181
{
182182

183183
$data = [];
@@ -194,6 +194,7 @@ protected function parseController($class)
194194
$controllersNameArr = explode("\\", $class);
195195
$controllersName = $controllersNameArr[count($controllersNameArr) - 1];
196196
$data['controller'] = $controllersName;
197+
$data['path'] = $class;
197198
$data['group'] = !empty($group->value) ? $group->value : null;
198199
$data['sort'] = !empty($sort->value) ? $sort->value : null;
199200
if (!empty($data['group']) && !in_array($data['group'], $this->groups)) {

src/parses/ParseCodeTemplate.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace hg\apidoc\parses;
5+
6+
use Doctrine\Common\Annotations\AnnotationReader;
7+
use hg\apidoc\exception\ErrorException;
8+
use hg\apidoc\generator\ParseTemplate;
9+
use hg\apidoc\utils\DirAndFile;
10+
use hg\apidoc\utils\Helper;
11+
use hg\apidoc\utils\Lang;
12+
13+
class ParseCodeTemplate
14+
{
15+
16+
protected $config = [];
17+
18+
protected $currentApp = [];
19+
20+
21+
public function __construct($config)
22+
{
23+
24+
$this->config = $config;
25+
}
26+
27+
public function renderCode($params)
28+
{
29+
$appKey = $params['appKey'];
30+
$currentAppConfig = Helper::getCurrentAppConfig($appKey);
31+
$currentApp = $currentAppConfig['appConfig'];
32+
$this->currentApp = $currentApp;
33+
34+
$codeTemplate = $params['template'];
35+
36+
//验证参数
37+
38+
//验证模板文件是否存在
39+
40+
//解析接口数据
41+
$tplData = [];
42+
if ($codeTemplate['select_mode'] == 'controller'){
43+
$parseApiMenusService = new ParseApiMenus($this->config);
44+
$controllers = $params['selected'];
45+
if (!empty($controllers) && count($controllers) > 0) {
46+
$controllerList = [];
47+
foreach ($controllers as $class) {
48+
$classData = $parseApiMenusService->parseController($class);
49+
if ($classData !== false) {
50+
$controllerList[] = $classData;
51+
}
52+
}
53+
if (empty($codeTemplate['multiple'])){
54+
$tplData = $controllerList[0];
55+
}else{
56+
$tplData = $controllerList;
57+
}
58+
}
59+
}else{
60+
// api
61+
$apis = $params['selected'];
62+
if (!empty($apis) && count($apis) > 0) {
63+
$parseApiDetailService = new ParseApiDetail($this->config);
64+
$apiList = [];
65+
foreach ($apis as $key) {
66+
$apiKey = urldecode($key);
67+
$pathArr = explode("@", $apiKey);
68+
$classPath = $pathArr[0];
69+
$method = $pathArr[1];
70+
71+
$apiDetail = $parseApiDetailService->renderApiDetail($appKey,$classPath,$method);
72+
if ($apiDetail !== false) {
73+
$apiList[] = $apiDetail;
74+
}
75+
}
76+
if (empty($codeTemplate['multiple'])){
77+
$tplData = $apiList[0];
78+
}else{
79+
$tplData = $apiList;
80+
}
81+
}
82+
}
83+
84+
85+
// 读取模板
86+
$templatePath =DirAndFile::formatPath( APIDOC_ROOT_PATH . $codeTemplate['template'],"/");
87+
if (is_readable($templatePath) == false) {
88+
throw new ErrorException("template not found", [
89+
'template' => $template
90+
]);
91+
}
92+
$tplParams = [
93+
'form'=> $params['form'],
94+
'data'=>$tplData
95+
];
96+
$html = (new ParseTemplate())->compile($templatePath,$tplParams);
97+
98+
99+
100+
return $html;
101+
}
102+
103+
104+
}

src/providers/CommonService.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ trait CommonService
1818
['rule'=>'generator','route'=>'createGenerator','method'=>'POST'],
1919
['rule'=>'cancelAllCache','route'=>'cancelAllCache','method'=>'POST'],
2020
['rule'=>'createAllCache','route'=>'createAllCache','method'=>'POST'],
21+
['rule'=>'renderCodeTemplate','route'=>'renderCodeTemplate','method'=>'POST'],
2122
];
2223

2324

src/utils/ConfigProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public static function getFeConfig(){
4949
'params'=>!empty($config['params'])?$config['params']:[],
5050
'responses'=>!empty($config['responses'])?$config['responses']:[],
5151
'generator'=>!empty($config['generator'])?$config['generator']:[],
52+
'code_template'=>!empty($config['code_template'])?$config['code_template']:[],
5253
];
5354
if (!empty($feConfig['apps']) && count($feConfig['apps'])){
5455
// 清除apps配置中的password

0 commit comments

Comments
 (0)