GenerateService.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\common\service\generator;
  15. use app\common\service\generator\core\ControllerGenerator;
  16. use app\common\service\generator\core\ListsGenerator;
  17. use app\common\service\generator\core\LogicGenerator;
  18. use app\common\service\generator\core\ModelGenerator;
  19. use app\common\service\generator\core\SqlGenerator;
  20. use app\common\service\generator\core\ValidateGenerator;
  21. use app\common\service\generator\core\VueApiGenerator;
  22. use app\common\service\generator\core\VueEditGenerator;
  23. use app\common\service\generator\core\VueIndexGenerator;
  24. /**
  25. * 生成器
  26. * Class GenerateService
  27. * @package app\common\service\generator
  28. */
  29. class GenerateService
  30. {
  31. // 标记
  32. protected $flag;
  33. // 生成文件路径
  34. protected $generatePath;
  35. // runtime目录
  36. protected $runtimePath;
  37. // 压缩包名称
  38. protected $zipTempName;
  39. // 压缩包临时路径
  40. protected $zipTempPath;
  41. public function __construct()
  42. {
  43. $this->generatePath = root_path() . 'runtime/generate/';
  44. $this->runtimePath = root_path() . 'runtime/';
  45. }
  46. /**
  47. * @notes 删除生成文件夹内容
  48. * @author 段誉
  49. * @date 2022/6/23 18:52
  50. */
  51. public function delGenerateDirContent()
  52. {
  53. // 删除runtime目录制定文件夹
  54. !is_dir($this->generatePath) && mkdir($this->generatePath, 0755, true);
  55. del_target_dir($this->generatePath, false);
  56. }
  57. /**
  58. * @notes 设置生成状态
  59. * @param $name
  60. * @param false $status
  61. * @author 段誉
  62. * @date 2022/6/23 18:53
  63. */
  64. public function setGenerateFlag($name, $status = false)
  65. {
  66. $this->flag = $name;
  67. cache($name, (int)$status, 3600);
  68. }
  69. /**
  70. * @notes 获取生成状态标记
  71. * @return mixed|object|\think\App
  72. * @author 段誉
  73. * @date 2022/6/23 18:53
  74. */
  75. public function getGenerateFlag()
  76. {
  77. return cache($this->flag);
  78. }
  79. /**
  80. * @notes 删除标记时间
  81. * @author 段誉
  82. * @date 2022/6/23 18:53
  83. */
  84. public function delGenerateFlag()
  85. {
  86. cache($this->flag, null);
  87. }
  88. /**
  89. * @notes 生成器相关类
  90. * @return string[]
  91. * @author 段誉
  92. * @date 2022/6/23 17:17
  93. */
  94. public function getGeneratorClass()
  95. {
  96. return [
  97. ControllerGenerator::class,
  98. ListsGenerator::class,
  99. ModelGenerator::class,
  100. ValidateGenerator::class,
  101. LogicGenerator::class,
  102. VueApiGenerator::class,
  103. VueIndexGenerator::class,
  104. VueEditGenerator::class,
  105. SqlGenerator::class,
  106. ];
  107. }
  108. /**
  109. * @notes 生成文件
  110. * @param array $tableData
  111. * @author 段誉
  112. * @date 2022/6/23 18:52
  113. */
  114. public function generate(array $tableData)
  115. {
  116. foreach ($this->getGeneratorClass() as $item) {
  117. $generator = app()->make($item);
  118. $generator->initGenerateData($tableData);
  119. $generator->generate();
  120. // 是否为压缩包下载
  121. if ($generator->isGenerateTypeZip()) {
  122. $this->setGenerateFlag($this->flag, true);
  123. }
  124. // 是否构建菜单
  125. if ($item == 'app\common\service\generator\core\SqlGenerator') {
  126. $generator->isBuildMenu() && $generator->buildMenuHandle();
  127. }
  128. }
  129. }
  130. /**
  131. * @notes 预览文件
  132. * @param array $tableData
  133. * @return array
  134. * @author 段誉
  135. * @date 2022/6/23 18:52
  136. */
  137. public function preview(array $tableData)
  138. {
  139. $data = [];
  140. foreach ($this->getGeneratorClass() as $item) {
  141. $generator = app()->make($item);
  142. $generator->initGenerateData($tableData);
  143. $data[] = $generator->fileInfo();
  144. }
  145. return $data;
  146. }
  147. /**
  148. * @notes 压缩文件
  149. * @author 段誉
  150. * @date 2022/6/23 19:02
  151. */
  152. public function zipFile()
  153. {
  154. $fileName = 'curd-' . date('YmdHis') . '.zip';
  155. $this->zipTempName = $fileName;
  156. $this->zipTempPath = $this->generatePath . $fileName;
  157. $zip = new \ZipArchive();
  158. $zip->open($this->zipTempPath, \ZipArchive::CREATE);
  159. $this->addFileZip($this->runtimePath, 'generate', $zip);
  160. $zip->close();
  161. }
  162. /**
  163. * @notes 往压缩包写入文件
  164. * @param $basePath
  165. * @param $dirName
  166. * @param $zip
  167. * @author 段誉
  168. * @date 2022/6/23 19:02
  169. */
  170. public function addFileZip($basePath, $dirName, $zip)
  171. {
  172. $handler = opendir($basePath . $dirName);
  173. while (($filename = readdir($handler)) !== false) {
  174. if ($filename != '.' && $filename != '..') {
  175. if (is_dir($basePath . $dirName . '/' . $filename)) {
  176. // 当前路径是文件夹
  177. $this->addFileZip($basePath, $dirName . '/' . $filename, $zip);
  178. } else {
  179. // 写入文件到压缩包
  180. $zip->addFile($basePath . $dirName . '/' . $filename, $dirName . '/' . $filename);
  181. }
  182. }
  183. }
  184. closedir($handler);
  185. }
  186. /**
  187. * @notes 返回压缩包临时路径
  188. * @return mixed
  189. * @author 段誉
  190. * @date 2022/6/24 9:41
  191. */
  192. public function getDownloadUrl()
  193. {
  194. $vars = ['file' => $this->zipTempName];
  195. cache('curd_file_name' . $this->zipTempName, $this->zipTempName, 3600);
  196. return (string)url("adminapi/tools.generator/download", $vars, false, true);
  197. }
  198. }