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+ }
0 commit comments