outPath = root_path() . 'public/upload/generator/'; $this->runtimePath = root_path() . 'public/upload/'; } /** * 删除生成文件 */ public function delOutFiles() { // 删除runtime目录制定文件夹 !is_dir($this->outPath) && mkdirs($this->outPath); del_target_dir($this->outPath, false); } /** * 设置生成状态 * @param $name * @param false $status */ public function setFlag($name, $status = false) { $this->flag = $name; cache($name, (int)$status, 3600); } /** * 获取生成状态标记 * @return mixed|object|App */ public function getFlag() { return cache($this->flag); } /** * 删除标记时间 */ public function delFlag() { cache($this->flag, null); } /** * 生成器相关类 * @return string[] */ public function getClassGenerator() { return [ ControllerGenerator::class, ServiceGenerator::class, ModelGenerator::class, ValidateGenerator::class, MenuSqlGenerator::class, AdminApiRouteGenerator::class, WebIndexGenerator::class, WebEditGenerator::class, WebEditPageGenerator::class, WebApiGenerator::class, WebLangGenerator::class, WebEditLangGenerator::class ]; } /** * 压缩文件 */ public function zipFile() { $fileName = 'niucloud-' . date('YmdHis') . '.zip'; $this->zipName = $fileName; $this->zipPath = $this->outPath . $fileName; $zip = new ZipArchive(); $zip->open($this->zipPath, ZipArchive::CREATE); $this->addFileZip($this->runtimePath, 'generator', $zip); $zip->close(); } /** * 往压缩包写入文件 * @param $basePath * @param $dirName * @param $zip */ public function addFileZip($basePath, $dirName, $zip) { $handler = opendir($basePath . $dirName); while (($filename = readdir($handler)) !== false) { if ($filename != '.' && $filename != '..') { if (is_dir($basePath . $dirName . '/' . $filename)) { // 当前路径是文件夹 $this->addFileZip($basePath, $dirName . '/' . $filename, $zip); } else { // 写入文件到压缩包 $zip->addFile($basePath . $dirName . '/' . $filename, $dirName . '/' . $filename); } } } closedir($handler); } /** * 返回压缩包临时路径 * @return string */ public function getDownloadUrl() { return 'upload/generator/' .$this->zipName; } /** * 生成文件 * @param array $table */ public function generate(array $table) { foreach ($this->getClassGenerator() as $item) { $generator = app()->make($item); $generator->init($table); $generator->generate(); $this->setFlag($this->flag, true); } } /** * 预览文件 * @param array $table * @return array */ public function preview(array $table) { $data = []; foreach ($this->getGenerator() as $item) { $generator = app()->make($item); $generator->init($table); $file_info = $generator->fileInfo(); if(!empty($file_info)) { $data[] = $file_info; } } return $data; } public function getGenerator() { return [ ControllerGenerator::class, ModelGenerator::class, ServiceGenerator::class, ValidateGenerator::class, MenuSqlGenerator::class, AdminApiRouteGenerator::class, WebApiGenerator::class, WebLangGenerator::class, WebEditGenerator::class, WebIndexGenerator::class, WebEditPageGenerator::class, WebEditLangGenerator::class ]; } }