Generate.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的多应用管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\service\admin\generator;
  12. use think\App;
  13. use ZipArchive;
  14. use app\service\admin\generator\core\ControllerGenerator;
  15. use app\service\admin\generator\core\ServiceGenerator;
  16. use app\service\admin\generator\core\AdminApiRouteGenerator;
  17. use app\service\admin\generator\core\ModelGenerator;
  18. use app\service\admin\generator\core\ValidateGenerator;
  19. use app\service\admin\generator\core\WebIndexGenerator;
  20. use app\service\admin\generator\core\WebEditGenerator;
  21. use app\service\admin\generator\core\WebEditPageGenerator;
  22. use app\service\admin\generator\core\WebApiGenerator;
  23. use app\service\admin\generator\core\WebLangGenerator;
  24. use app\service\admin\generator\core\WebEditLangGenerator;
  25. use app\service\admin\generator\core\MenuSqlGenerator;
  26. /**
  27. * 代码生成器
  28. * Class GenerateService
  29. * @package app\service\generator
  30. */
  31. class Generate
  32. {
  33. // 生成文件路径
  34. protected $outPath;
  35. // runtime目录
  36. protected $runtimePath;
  37. // 压缩包名称
  38. protected $zipName;
  39. // 压缩包临时路径
  40. protected $zipPath;
  41. // 代码生成标记
  42. protected $flag;
  43. public function __construct()
  44. {
  45. $this->outPath = root_path() . 'public/upload/generator/';
  46. $this->runtimePath = root_path() . 'public/upload/';
  47. }
  48. /**
  49. * 删除生成文件
  50. */
  51. public function delOutFiles()
  52. {
  53. // 删除runtime目录制定文件夹
  54. !is_dir($this->outPath) && mkdirs($this->outPath);
  55. del_target_dir($this->outPath, false);
  56. }
  57. /**
  58. * 设置生成状态
  59. * @param $name
  60. * @param false $status
  61. */
  62. public function setFlag($name, $status = false)
  63. {
  64. $this->flag = $name;
  65. cache($name, (int)$status, 3600);
  66. }
  67. /**
  68. * 获取生成状态标记
  69. * @return mixed|object|App
  70. */
  71. public function getFlag()
  72. {
  73. return cache($this->flag);
  74. }
  75. /**
  76. * 删除标记时间
  77. */
  78. public function delFlag()
  79. {
  80. cache($this->flag, null);
  81. }
  82. /**
  83. * 生成器相关类
  84. * @return string[]
  85. */
  86. public function getClassGenerator()
  87. {
  88. return [
  89. ControllerGenerator::class,
  90. ServiceGenerator::class,
  91. ModelGenerator::class,
  92. ValidateGenerator::class,
  93. MenuSqlGenerator::class,
  94. AdminApiRouteGenerator::class,
  95. WebIndexGenerator::class,
  96. WebEditGenerator::class,
  97. WebEditPageGenerator::class,
  98. WebApiGenerator::class,
  99. WebLangGenerator::class,
  100. WebEditLangGenerator::class
  101. ];
  102. }
  103. /**
  104. * 压缩文件
  105. */
  106. public function zipFile()
  107. {
  108. $fileName = 'niucloud-' . date('YmdHis') . '.zip';
  109. $this->zipName = $fileName;
  110. $this->zipPath = $this->outPath . $fileName;
  111. $zip = new ZipArchive();
  112. $zip->open($this->zipPath, ZipArchive::CREATE);
  113. $this->addFileZip($this->runtimePath, 'generator', $zip);
  114. $zip->close();
  115. }
  116. /**
  117. * 往压缩包写入文件
  118. * @param $basePath
  119. * @param $dirName
  120. * @param $zip
  121. */
  122. public function addFileZip($basePath, $dirName, $zip)
  123. {
  124. $handler = opendir($basePath . $dirName);
  125. while (($filename = readdir($handler)) !== false) {
  126. if ($filename != '.' && $filename != '..') {
  127. if (is_dir($basePath . $dirName . '/' . $filename)) {
  128. // 当前路径是文件夹
  129. $this->addFileZip($basePath, $dirName . '/' . $filename, $zip);
  130. } else {
  131. // 写入文件到压缩包
  132. $zip->addFile($basePath . $dirName . '/' . $filename, $dirName . '/' . $filename);
  133. }
  134. }
  135. }
  136. closedir($handler);
  137. }
  138. /**
  139. * 返回压缩包临时路径
  140. * @return string
  141. */
  142. public function getDownloadUrl()
  143. {
  144. return 'upload/generator/' .$this->zipName;
  145. }
  146. /**
  147. * 生成文件
  148. * @param array $table
  149. */
  150. public function generate(array $table)
  151. {
  152. foreach ($this->getClassGenerator() as $item) {
  153. $generator = app()->make($item);
  154. $generator->init($table);
  155. $generator->generate();
  156. $this->setFlag($this->flag, true);
  157. }
  158. }
  159. /**
  160. * 预览文件
  161. * @param array $table
  162. * @return array
  163. */
  164. public function preview(array $table)
  165. {
  166. $data = [];
  167. foreach ($this->getGenerator() as $item) {
  168. $generator = app()->make($item);
  169. $generator->init($table);
  170. $file_info = $generator->fileInfo();
  171. if(!empty($file_info))
  172. {
  173. $data[] = $file_info;
  174. }
  175. }
  176. return $data;
  177. }
  178. public function getGenerator()
  179. {
  180. return [
  181. ControllerGenerator::class,
  182. ModelGenerator::class,
  183. ServiceGenerator::class,
  184. ValidateGenerator::class,
  185. MenuSqlGenerator::class,
  186. AdminApiRouteGenerator::class,
  187. WebApiGenerator::class,
  188. WebLangGenerator::class,
  189. WebEditGenerator::class,
  190. WebIndexGenerator::class,
  191. WebEditPageGenerator::class,
  192. WebEditLangGenerator::class
  193. ];
  194. }
  195. }