88namespace app \util ;
99
1010
11+ use app \model \AdminMenu ;
12+ use think \facade \Db ;
13+
1114class AutoBuild {
1215
1316 private $ config = [
@@ -22,16 +25,34 @@ class AutoBuild {
2225 'fid ' => 0 // 父级ID
2326 ];
2427
25- private $ basePath = '' ;
26-
28+ /**
29+ * 自动构建
30+ * @param array $config
31+ * @throws \think\db\exception\DataNotFoundException
32+ * @throws \think\db\exception\DbException
33+ * @throws \think\db\exception\ModelNotFoundException
34+ * @author zhaoxiang <zhaoxiang051405@gmail.com>
35+ */
2736 public function run ($ config = []) {
2837 $ this ->config = array_merge ($ this ->config , $ config );
2938
30- if ($ this ->config ['module ' ] == 1 ) {
39+ if ($ this ->config ['model ' ] == 1 ) {
40+ $ this ->buildModel ();
3141
42+ if ($ this ->config ['table ' ] == 1 ) {
43+ $ this ->createTable ();
44+ }
3245 }
3346 if ($ this ->config ['control ' ] && $ this ->config ['name ' ]) {
3447 $ this ->buildControl ();
48+
49+ if ($ this ->config ['menu ' ] && $ this ->config ['module ' ] == 1 ) {
50+ $ this ->buildMenu ();
51+ }
52+
53+ if ($ this ->config ['route ' ] && $ this ->config ['module ' ] == 1 ) {
54+ $ this ->buildRoute ();
55+ }
3556 }
3657 }
3758
@@ -46,23 +67,137 @@ private function unCamelize($camelCaps, $separator = '_'): string {
4667 return strtolower (preg_replace ('/([a-z])([A-Z])/ ' , "$1 " . $ separator . "$2 " , $ camelCaps ));
4768 }
4869
70+ /**
71+ * 构建控制器
72+ * @author zhaoxiang <zhaoxiang051405@gmail.com>
73+ */
4974 private function buildControl () {
75+ $ tplPath = root_path () . 'install ' . DIRECTORY_SEPARATOR ;
76+ if ($ this ->config ['module ' ] == 1 ) {
77+ $ module = 'admin ' ;
78+ } else {
79+ $ module = 'api ' ;
80+ }
5081
82+ $ controlStr = str_replace (
83+ ['{$MODULE} ' , '{$NAME} ' ],
84+ [$ module , $ this ->config ['name ' ]],
85+ file_get_contents ($ tplPath . 'control.tpl ' )
86+ );
87+ file_put_contents (
88+ base_path () . 'controller ' . DIRECTORY_SEPARATOR . $ module . DIRECTORY_SEPARATOR . $ this ->config ['name ' ] . '.php ' ,
89+ $ controlStr
90+ );
5191 }
5292
93+ /**
94+ * 构建模型
95+ * @author zhaoxiang <zhaoxiang051405@gmail.com>
96+ */
5397 private function buildModel () {
54-
98+ $ modelStr = '<?php ' . PHP_EOL ;
99+ $ modelStr .= '/** ' . PHP_EOL ;
100+ $ modelStr .= ' * 由ApiAdmin自动构建 ' . PHP_EOL ;
101+ $ modelStr .= ' * @author apiadmin <apiadmin.org> ' . PHP_EOL ;
102+ $ modelStr .= ' */ ' . PHP_EOL ;
103+ $ modelStr .= 'namespace app\model; ' . PHP_EOL ;
104+ $ modelStr .= 'class ' . $ this ->config ['modelName ' ] . ' extends Base { ' . PHP_EOL ;
105+ $ modelStr .= '} ' . PHP_EOL ;
106+
107+ file_put_contents (
108+ base_path () . 'model ' . DIRECTORY_SEPARATOR . $ this ->config ['modelName ' ] . '.php ' ,
109+ $ modelStr
110+ );
55111 }
56112
113+ /**
114+ * 构建表
115+ * @author zhaoxiang <zhaoxiang051405@gmail.com>
116+ */
57117 private function createTable () {
58-
118+ $ tableName = $ this ->unCamelize ($ this ->config ['modelName ' ]);
119+ $ cmd = "CREATE TABLE ` {$ tableName }` (`id` int NOT NULL AUTO_INCREMENT,PRIMARY KEY (`id`)) COMMENT='由ApiAdmin自动构建'; " ;
120+ Db::execute ($ cmd );
59121 }
60122
123+ /**
124+ * 构建菜单
125+ * @author zhaoxiang <zhaoxiang051405@gmail.com>
126+ */
61127 private function buildMenu () {
62-
128+ $ menus = [
129+ [
130+ 'title ' => '新增 ' ,
131+ 'fid ' => $ this ->config ['fid ' ],
132+ 'url ' => "admin/ {$ this ->config ['name ' ]}/add " ,
133+ 'auth ' => 1 ,
134+ 'sort ' => 0 ,
135+ 'show ' => 1 ,
136+ 'icon ' => '' ,
137+ 'level ' => 3 ,
138+ 'component ' => '' ,
139+ 'router ' => '' ,
140+ 'log ' => 1 ,
141+ 'permission ' => 1 ,
142+ 'method ' => 2
143+ ],
144+ [
145+ 'title ' => '编辑 ' ,
146+ 'fid ' => $ this ->config ['fid ' ],
147+ 'url ' => "admin/ {$ this ->config ['name ' ]}/edit " ,
148+ 'auth ' => 1 ,
149+ 'sort ' => 0 ,
150+ 'show ' => 1 ,
151+ 'icon ' => '' ,
152+ 'level ' => 3 ,
153+ 'component ' => '' ,
154+ 'router ' => '' ,
155+ 'log ' => 1 ,
156+ 'permission ' => 1 ,
157+ 'method ' => 2
158+ ],
159+ [
160+ 'title ' => '删除 ' ,
161+ 'fid ' => $ this ->config ['fid ' ],
162+ 'url ' => "admin/ {$ this ->config ['name ' ]}/del " ,
163+ 'auth ' => 1 ,
164+ 'sort ' => 0 ,
165+ 'show ' => 1 ,
166+ 'icon ' => '' ,
167+ 'level ' => 3 ,
168+ 'component ' => '' ,
169+ 'router ' => '' ,
170+ 'log ' => 1 ,
171+ 'permission ' => 1 ,
172+ 'method ' => 1
173+ ],
174+ [
175+ 'title ' => '列表 ' ,
176+ 'fid ' => $ this ->config ['fid ' ],
177+ 'url ' => "admin/ {$ this ->config ['name ' ]}/index " ,
178+ 'auth ' => 1 ,
179+ 'sort ' => 0 ,
180+ 'show ' => 1 ,
181+ 'icon ' => '' ,
182+ 'level ' => 3 ,
183+ 'component ' => '' ,
184+ 'router ' => '' ,
185+ 'log ' => 1 ,
186+ 'permission ' => 1 ,
187+ 'method ' => 1
188+ ]
189+ ];
190+ (new AdminMenu ())->insertAll ($ menus );
63191 }
64192
193+ /**
194+ * 构建路由
195+ * @throws \think\db\exception\DataNotFoundException
196+ * @throws \think\db\exception\DbException
197+ * @throws \think\db\exception\ModelNotFoundException
198+ * @author zhaoxiang <zhaoxiang051405@gmail.com>
199+ */
65200 private function buildRoute () {
66-
201+ RouterTool:: buildAdminRouter ();
67202 }
68203}
0 commit comments