Skip to content

Commit 832b4ce

Browse files
committed
增加事件的ref引入、完善多语言使用
1 parent e2d5619 commit 832b4ce

File tree

5 files changed

+61
-39
lines changed

5 files changed

+61
-39
lines changed

src/Controller.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public function __construct(App $app)
5050
$defaultAppConfig = ['title'=>$default_app,'path'=>$path,'folder'=>$default_app];
5151
$config['apps'] = [$defaultAppConfig];
5252
}
53+
54+
5355
Config::set(['apidoc'=>$config]);
5456
$this->config = $config;
5557

@@ -74,10 +76,13 @@ public function getConfig(){
7476
if ($this->tp_version === 5){
7577
Lang::setLangCookieVar($params['lang']);
7678
}else{
79+
Lang::setLangSet($params['lang']);
7780
\think\facade\App::loadLangPack($params['lang']);
7881
}
7982

8083
}
84+
$config['title'] = Utils::getLang($config['title']);
85+
$config['desc'] = Utils::getLang($config['desc']);
8186
$config['headers'] = Utils::getArrayLang($config['headers'],"desc");
8287
$config['parameters'] = Utils::getArrayLang($config['parameters'],"desc");
8388
$config['responses'] = Utils::getArrayLang($config['responses'],"desc");
@@ -196,6 +201,7 @@ public function getMdMenus(){
196201
if ($this->tp_version === 5){
197202
Lang::setLangCookieVar($params['lang']);
198203
}else{
204+
Lang::setLangSet($params['lang']);
199205
\think\facade\App::loadLangPack($params['lang']);
200206
}
201207
}
@@ -215,6 +221,7 @@ public function getMdDetail(){
215221
if ($this->tp_version === 5){
216222
Lang::setLangCookieVar($params['lang']);
217223
}else{
224+
Lang::setLangSet($params['lang']);
218225
\think\facade\App::loadLangPack($params['lang']);
219226
}
220227
}

src/annotation/After.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,11 @@ final class After extends Annotation
5252
*/
5353
public $desc;
5454

55+
/**
56+
* 引用
57+
* @var string
58+
*/
59+
public $ref;
60+
5561

5662
}

src/annotation/Before.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,10 @@ final class Before extends Annotation
5252
*/
5353
public $desc;
5454

55+
/**
56+
* 引用
57+
* @var string
58+
*/
59+
public $ref;
5560

5661
}

src/parseApi/ParseAnnotation.php

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ protected function parseApiMethod($refMethod,$routeGroup){
233233
}
234234
// 无标题,且有文本注释
235235
if (empty($methodItem['title']) && !empty($textAnnotations) && count($textAnnotations) > 0) {
236-
$methodItem['title'] = $textAnnotations[0];
236+
$methodItem['title'] = Utils::getLang($textAnnotations[0]);
237237
}
238238
// 添加统一headers请求头参数
239239
if (!empty($config['headers']) && !in_array("NotHeaders", $textAnnotations)) {
@@ -569,43 +569,12 @@ protected function parseAnnotation($refMethod, bool $enableRefService = true,$so
569569
$data['tag'] = $annotation->value;
570570
break;
571571
case $annotation instanceof Before:
572-
if (!empty($annotation->value) && is_array($annotation->value)){
573-
$beforeInfo = Utils::objectToArray($annotation);
574-
$valueList = [];
575-
foreach ($annotation->value as $valueItem){
576-
$valueItemInfo = Utils::objectToArray($valueItem);
577-
if ($valueItem instanceof Before){
578-
$valueItemInfo['type'] = "before";
579-
}else if ($valueItem instanceof After){
580-
$valueItemInfo['type'] = "after";
581-
}
582-
$valueList[] = $valueItemInfo;
583-
}
584-
$beforeInfo['value'] = $valueList;
585-
$before[] = $beforeInfo;
586-
}else{
587-
$before[] = $annotation;
588-
}
589-
572+
$beforeAnnotation = $this->handleEventAnnotation($annotation,'before');
573+
$before = array_merge($before,$beforeAnnotation);
590574
break;
591575
case $annotation instanceof After:
592-
if (!empty($annotation->value) && is_array($annotation->value)){
593-
$afterInfo = Utils::objectToArray($annotation);
594-
$valueList = [];
595-
foreach ($annotation->value as $valueItem){
596-
$valueItemInfo = Utils::objectToArray($valueItem);
597-
if ($valueItem instanceof Before){
598-
$valueItemInfo['type'] = "before";
599-
}else if ($valueItem instanceof After){
600-
$valueItemInfo['type'] = "after";
601-
}
602-
$valueList[] = $valueItemInfo;
603-
}
604-
$afterInfo['value'] = $valueList;
605-
$after[] = $afterInfo;
606-
}else{
607-
$after[] = $annotation;
608-
}
576+
$afterAnnotation = $this->handleEventAnnotation($annotation,'after');
577+
$after =array_merge($after,$afterAnnotation);
609578
break;
610579
}
611580
}
@@ -620,6 +589,37 @@ protected function parseAnnotation($refMethod, bool $enableRefService = true,$so
620589
return $data;
621590
}
622591

592+
public function handleEventAnnotation($annotation,$type){
593+
$config = $this->config;
594+
if (!empty($annotation->ref)){
595+
if (strpos($annotation->ref, '\\') === false && !empty($config['definitions']) ) {
596+
$refPath = $config['definitions'] . '\\' . $annotation->ref;
597+
$data = $this->renderService($refPath);
598+
if (!empty($data[$type])){
599+
return $data[$type];
600+
}
601+
return [];
602+
}
603+
}
604+
if (!empty($annotation->value) && is_array($annotation->value)){
605+
$beforeInfo = Utils::objectToArray($annotation);
606+
$valueList = [];
607+
foreach ($annotation->value as $valueItem){
608+
$valueItemInfo = Utils::objectToArray($valueItem);
609+
if ($valueItem instanceof Before){
610+
$valueItemInfo['type'] = "before";
611+
}else if ($valueItem instanceof After){
612+
$valueItemInfo['type'] = "after";
613+
}
614+
$valueList[] = $valueItemInfo;
615+
}
616+
$beforeInfo['value'] = $valueList;
617+
return [$beforeInfo];
618+
}else{
619+
return [$annotation];
620+
}
621+
}
622+
623623

624624
/**
625625
* 处理请求参数与返回参数

src/parseApi/ParseMarkdown.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use think\facade\App;
77
use hg\apidoc\Utils;
88
use think\facade\Config;
9+
use think\facade\Lang;
910

1011
class ParseMarkdown
1112
{
@@ -39,16 +40,19 @@ protected function handleDocsMenuData(array $menus): array
3940
{
4041
$list = [];
4142
foreach ($menus as $item) {
43+
$item['title'] = Utils::getLang($item['title']);
44+
if(!empty($item['path'])){
45+
$lang = Lang::getLangSet();
46+
$item['path'] = Utils::replaceTemplate($item['path'],['lang'=>$lang]);
47+
}
4248
if (!empty($item['children']) && count($item['children']) > 0) {
4349
$item['children'] = $this->handleDocsMenuData($item['children']);
4450
$item['menu_key'] = Utils::createRandKey("md_group");
45-
$list[] = $item;
4651
} else {
4752
$item['type'] = 'md';
48-
$item['title'] = Utils::getLang($item['title']);
4953
$item['menu_key'] = Utils::createRandKey("md");
50-
$list[] = $item;
5154
}
55+
$list[] = $item;
5256
}
5357
return $list;
5458
}

0 commit comments

Comments
 (0)