From 60d8d26af1b089e15069e98c9f9e801640414bd1 Mon Sep 17 00:00:00 2001 From: liaofei <136327134@qq.com> Date: Tue, 18 Apr 2023 14:32:57 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=94=9F=E6=88=90=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/v1/setting/SystemCrud.php | 4 +- .../services/system/SystemCrudServices.php | 71 ++++--- crmeb/crmeb/services/FileService.php | 44 +++++ crmeb/crmeb/services/crud/Controller.php | 8 +- crmeb/crmeb/services/crud/Make.php | 183 ++++++++++-------- crmeb/crmeb/services/crud/Model.php | 2 +- crmeb/crmeb/services/crud/Route.php | 7 +- crmeb/crmeb/services/crud/Service.php | 14 +- crmeb/crmeb/services/crud/ViewApi.php | 8 +- crmeb/crmeb/services/crud/ViewPages.php | 2 +- crmeb/crmeb/services/crud/ViewRouter.php | 8 +- 11 files changed, 211 insertions(+), 140 deletions(-) diff --git a/crmeb/app/adminapi/controller/v1/setting/SystemCrud.php b/crmeb/app/adminapi/controller/v1/setting/SystemCrud.php index 2c9747b77..7ad84fc1e 100644 --- a/crmeb/app/adminapi/controller/v1/setting/SystemCrud.php +++ b/crmeb/app/adminapi/controller/v1/setting/SystemCrud.php @@ -198,7 +198,7 @@ class SystemCrud extends AuthController $softDelete = false; foreach ((array)$info->field['tableField'] as $item) { - if ($item['field_type'] === 'addSoftDelete') { + if (isset($item['field_type']) && $item['field_type'] === 'addSoftDelete') { $softDelete = true; break; } @@ -350,7 +350,7 @@ class SystemCrud extends AuthController 'softDelete' => $softDelete, 'fromField' => $info->field['fromField'] ?? [], 'columnField' => $info->field['columnField'] ?? [], - ], $makePath); + ], $makePath, $zipPath); if (!extension_loaded('zip')) { return app('json')->fail(500040); diff --git a/crmeb/app/services/system/SystemCrudServices.php b/crmeb/app/services/system/SystemCrudServices.php index 9afa6e25d..2fc768dba 100644 --- a/crmeb/app/services/system/SystemCrudServices.php +++ b/crmeb/app/services/system/SystemCrudServices.php @@ -27,6 +27,7 @@ use crmeb\services\crud\Validate; use crmeb\services\crud\ViewApi; use crmeb\services\crud\ViewPages; use crmeb\services\crud\ViewRouter; +use crmeb\services\FileService; use Phinx\Db\Adapter\AdapterFactory; use think\facade\Db; use think\helper\Str; @@ -504,99 +505,105 @@ class SystemCrudServices extends BaseServices * @param bool $isMake * @param array $options * @param array $filePath + * @param string $basePath * @return array[] * @author 等风来 * @email 136327134@qq.com * @date 2023/4/7 */ - public function makeFile(string $tableName, string $routeName, bool $isMake = false, array $options = [], array $filePath = []) + public function makeFile(string $tableName, string $routeName, bool $isMake = false, array $options = [], array $filePath = [], string $basePath = '') { $options['fromField'] = is_array($options['fromField']) ? $options['fromField'] : []; $options['columnField'] = is_array($options['columnField']) ? $options['columnField'] : []; //生成模型 $model = app()->make(Model::class); - [$modelContent, $modelPath, $usePath] = $model->setFilePathName($filePath['model'] ?? '')->isMake($isMake)->handle($tableName, $options); + $model->setFilePathName($filePath['model'] ?? '')->setbasePath($basePath)->handle($tableName, $options); //生成dao $dao = app()->make(Dao::class); - [$daoContent, $daoPath, $usePath] = $dao->setFilePathName($filePath['dao'] ?? '')->isMake($isMake)->handle($tableName, [ - 'usePath' => $usePath, + $dao->setFilePathName($filePath['dao'] ?? '')->setbasePath($basePath)->handle($tableName, [ + 'usePath' => $model->getUsePath(), ]); //生成service $service = app()->make(Service::class); - [$serviceContent, $servicePath, $usePath] = $service->setFilePathName($filePath['service'] ?? '')->isMake($isMake)->handle($tableName, [ + $service->setFilePathName($filePath['service'] ?? '')->setbasePath($basePath)->handle($tableName, [ 'field' => $options['fromField'], - 'usePath' => $usePath, + 'usePath' => $dao->getUsePath(), ]); //生成验证器 $validate = app()->make(Validate::class); - [$validateContent, $validatePath] = $validate->setFilePathName($filePath['validate'] ?? '')->isMake($isMake)->handle($tableName); + $validate->setFilePathName($filePath['validate'] ?? '')->setbasePath($basePath)->handle($tableName); //生成控制器 $controller = app()->make(Controller::class); - [$controllerContent, $controllerPath] = $controller->setFilePathName($filePath['controller'] ?? '')->isMake($isMake)->handle($tableName, [ - 'usePath' => $usePath, + $controller->setFilePathName($filePath['controller'] ?? '')->setbasePath($basePath)->handle($tableName, [ + 'usePath' => $service->getUsePath(), 'field' => array_column($options['fromField'], 'field'), ]); //生成路由 $route = app()->make(Route::class); - [$routeContent, $routePath] = $route->setFilePathName($filePath['route'] ?? '')->isMake($isMake)->handle($tableName, [ + $route->setFilePathName($filePath['route'] ?? '')->setbasePath($basePath)->handle($tableName, [ 'menus' => $options['menuName'], 'route' => $routeName ]); //生成前台路由 $viewRouter = app()->make(ViewRouter::class); - [$routerContent, $routerPath] = $viewRouter->setFilePathName($filePath['router'] ?? '')->isMake($isMake)->handle($tableName, [ + $viewRouter->setFilePathName($filePath['router'] ?? '')->setbasePath($basePath)->handle($tableName, [ 'route' => $routeName, 'menuName' => $options['menuName'], ]); //生成前台接口 $viewApi = app()->make(ViewApi::class); - [$apiContent, $apiPath] = $viewApi->setFilePathName($filePath['api'] ?? '')->isMake($isMake)->handle($tableName, [ + $viewApi->setFilePathName($filePath['api'] ?? '')->setbasePath($basePath)->handle($tableName, [ 'route' => $routeName, ]); //生成前台页面 $viewPages = app()->make(ViewPages::class); - [$pagesContent, $pagesPath] = $viewPages->setFilePathName($filePath['pages'] ?? '')->isMake($isMake)->handle($tableName, [ + $viewPages->setFilePathName($filePath['pages'] ?? '')->setbasePath($basePath)->handle($tableName, [ 'field' => $options['columnField'], 'route' => $routeName, - 'pathApiJs' => '@/' . str_replace('\\', '/', str_replace([Make::adminTemplatePath(), '.js'], '', $apiPath)), + 'pathApiJs' => '@/' . str_replace('\\', '/', str_replace([Make::adminTemplatePath(), '.js'], '', $viewApi->getPath())), ]); + //创建文件 + if ($isMake) { + FileService::batchMakeFiles([$model, $dao, $service, $controller, $route, $viewApi, $viewPages, $viewRouter]); + } + return [ 'controller' => [ - 'path' => $this->replace($controllerPath), - 'content' => $controllerContent + 'path' => $this->replace($controller->getPath()), + 'content' => $controller->getContent() ], 'model' => [ - 'path' => $this->replace($modelPath), - 'content' => $modelContent + 'path' => $this->replace($model->getPath()), + 'content' => $model->getContent() ], 'dao' => [ - 'path' => $this->replace($daoPath), - 'content' => $daoContent + 'path' => $this->replace($dao->getPath()), + 'content' => $dao->getContent() ], 'route' => [ - 'path' => $this->replace($routePath), - 'content' => $routeContent + 'path' => $this->replace($route->getPath()), + 'content' => $route->getContent() ], 'service' => [ - 'path' => $this->replace($servicePath), - 'content' => $serviceContent + 'path' => $this->replace($service->getPath()), + 'content' => $service->getContent() ], 'validate' => [ - 'path' => $this->replace($validatePath), - 'content' => $validateContent + 'path' => $this->replace($validate->getPath()), + 'content' => $validate->getContent() ], 'router' => [ - 'path' => $this->replace($routerPath), - 'content' => $routerContent + 'path' => $this->replace($viewRouter->getPath()), + 'content' => $viewRouter->getContent() ], 'api' => [ - 'path' => $this->replace($apiPath), - 'content' => $apiContent + 'path' => $this->replace($viewApi->getPath()), + 'content' => $viewApi->getContent() ], 'pages' => [ - 'path' => $this->replace($pagesPath), - 'content' => $pagesContent + 'path' => $this->replace($viewPages->getPath()), + 'content' => $viewPages->getContent() ], ]; } diff --git a/crmeb/crmeb/services/FileService.php b/crmeb/crmeb/services/FileService.php index 7b0a5d8bc..7f4ffbe68 100644 --- a/crmeb/crmeb/services/FileService.php +++ b/crmeb/crmeb/services/FileService.php @@ -11,6 +11,8 @@ namespace crmeb\services; use crmeb\exceptions\AdminException; +use crmeb\exceptions\CrudException; +use crmeb\services\crud\Make; /** * 文件操作类 @@ -1048,4 +1050,46 @@ class FileService return $zip->extractTo($folder); } + /** + * 批量写入文件 + * @param array $make + * @return bool + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/18 + */ + public static function batchMakeFiles(array $make) + { + + $files = []; + $dirnames = []; + foreach ($make as $item) { + if ($item instanceof Make) { + $files[] = $item = $item->toArray(); + } + try { + $dirnames[] = $dirname = dirname($item['path']); + if (!is_dir($dirname)) { + mkdir($dirname, 0755, true); + } + } catch (\Throwable $e) { + if ($dirnames) { + foreach ($dirnames as $dirname) { + if (strstr($dirname, app()->getRootPath() . 'backup') !== false) { + rmdir($dirname); + } + } + } + throw new \RuntimeException($e->getMessage()); + } + } + $res = true; + foreach ($files as $item) { + var_dump($item['value']); + $res = $res && file_put_contents($item['path'], $item['content'], LOCK_EX); + } + + return $res; + } + } diff --git a/crmeb/crmeb/services/crud/Controller.php b/crmeb/crmeb/services/crud/Controller.php index f305c0b1f..1289f8a92 100644 --- a/crmeb/crmeb/services/crud/Controller.php +++ b/crmeb/crmeb/services/crud/Controller.php @@ -44,7 +44,7 @@ class Controller extends Make } /** - * @return mixed|void + * @return Controller * @author 等风来 * @email 136327134@qq.com * @date 2023/3/13 @@ -99,8 +99,10 @@ class Controller extends Make $contentStr = str_replace($this->var, $this->value, $contentController); $filePath = $this->getFilePathName($path, $this->value['nameCamel']); - - return [$this->makeFile($filePath, $contentStr), $this->filePathName ?: $filePath, $this->value['path']]; + $this->usePath = $this->value['path']; + $this->setPathname($filePath); + $this->setContent($contentStr); + return $this; } /** diff --git a/crmeb/crmeb/services/crud/Make.php b/crmeb/crmeb/services/crud/Make.php index a4aad281d..e9f5a1ebb 100644 --- a/crmeb/crmeb/services/crud/Make.php +++ b/crmeb/crmeb/services/crud/Make.php @@ -58,10 +58,16 @@ abstract class Make protected $content = ''; /** - * 文件存放 - * @var null + * 实际文件存放 + * @var string */ - protected $filePath = null; + protected $pathname = ''; + + /** + * 命名空间路径 + * @var string + */ + protected $usePath = ''; /** * 变量名称 @@ -75,17 +81,6 @@ abstract class Make */ protected $value = []; - /** - * @var bool - */ - protected $isMake = true; - - /** - * 文件存在是否生成 - * @var bool - */ - protected $isExistsMake = true; - /** * 后台前端模板根路径 * @var string @@ -125,6 +120,22 @@ abstract class Make $this->setDefaultValue(); } + /** + * 设置默认路径 + * @param string $basePath + * @return $this + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/18 + */ + public function setbasePath(string $basePath) + { + if ($basePath) { + $this->basePath = $basePath; + } + return $this; + } + /** * @return string * @author 等风来 @@ -176,17 +187,6 @@ abstract class Make return $this->fileBasePath; } - /** - * @return string - * @author 等风来 - * @email 136327134@qq.com - * @date 2023/4/7 - */ - public function getFileBasePath() - { - return $this->fileBasePath; - } - /** * 设置文件保存就路径名称 * @param string $filePathName @@ -216,23 +216,9 @@ abstract class Make return str_pad('', 4 * $num); } - /** - * 是否生成文件 - * @param bool $isMake - * @return $this - * @author 等风来 - * @email 136327134@qq.com - * @date 2023/4/3 - */ - public function isMake(bool $isMake = true) - { - $this->isMake = $isMake; - return $this; - } - /** * 执行创建 - * @return mixed|void + * @return Make * @author 等风来 * @email 136327134@qq.com * @date 2023/3/13 @@ -256,11 +242,11 @@ abstract class Make $contentStr = str_replace($this->var, $this->value, $content); $filePath = $this->getFilePathName($path, $this->value['nameCamel']); - return [ - $this->makeFile($filePath, $contentStr), - $this->filePathName ?: $filePath, - $this->baseDir . '\\' . $this->value['nameCamel'] - ]; + $this->usePath = $this->baseDir . '\\' . $this->value['nameCamel']; + $this->setPathname($filePath); + $this->setContent($contentStr); + + return $this; } /** @@ -434,37 +420,6 @@ abstract class Make return 'app' . ($app ? '\\' . $app : ''); } - - public function make(string $pathname) - { - $pathname = $this->filePathName ?: ($this->filePath ?: $pathname); - - if ($this->isMake) { - - if (is_file($pathname)) { - if ($this->isExistsMake) { - unlink($pathname); - } else { - throw new CrudException(500052, ['filename' => $this->name . ':' . $pathname]); - } - } - - try { - if (!is_dir(dirname($pathname))) { - mkdir(dirname($pathname), 0755, true); - } - } catch (\Throwable $e) { - throw new CrudException(500050, ['pathname' => dirname($pathname)]); - } - - try { - file_put_contents($pathname, $this->content); - } catch (\Throwable $e) { - throw new CrudException(500051, ['pathname' => $pathname]); - } - } - } - /** * 设置内容 * @param string $content @@ -476,26 +431,90 @@ abstract class Make protected function setContent(string $content) { $this->content = str_replace('', '', $content); - return $this->content; } /** - * 执行创建文件 + * @param string $pathname + * @return $this + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/18 + */ + protected function setPathname(string $pathname) + { + $this->pathname = $this->filePathName ?: $pathname; + return $this; + } + + /** + * @param string $key + * @return mixed|null + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/18 + */ + public function getValue(string $key) + { + return $this->value[$key] ?? null; + } + + /** + * 获取命名空间路径 * @return string * @author 等风来 * @email 136327134@qq.com - * @date 2023/3/13 + * @date 2023/4/18 */ - protected function makeFile(string $pathname, string $content) + public function getUsePath() + { + return $this->usePath; + } + + /** + * 获取内容 + * @return string + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/18 + */ + public function getContent() { - $this->content = str_replace('', '', $content); - $this->make($pathname); return $this->content; } + /** + * @return string + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/18 + */ + public function getPath() + { + return $this->pathname; + } + + /** + * @return array + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/18 + */ + public function toArray() + { + return [ + 'path' => $this->pathname, + 'content' => $this->content, + 'value' => $this->value, + 'var' => $this->var, + 'usePath' => $this->usePath, + ]; + } + public function __destruct() { $this->content = ''; + $this->pathname = ''; + $this->usePath = ''; } } diff --git a/crmeb/crmeb/services/crud/Model.php b/crmeb/crmeb/services/crud/Model.php index c61079915..547831b5e 100644 --- a/crmeb/crmeb/services/crud/Model.php +++ b/crmeb/crmeb/services/crud/Model.php @@ -44,7 +44,7 @@ class Model extends Make /** * @param string $name * @param array $options - * @return array|mixed|void + * @return Model * @author 等风来 * @email 136327134@qq.com * @date 2023/4/12 diff --git a/crmeb/crmeb/services/crud/Route.php b/crmeb/crmeb/services/crud/Route.php index f69776722..a9e9d0ae7 100644 --- a/crmeb/crmeb/services/crud/Route.php +++ b/crmeb/crmeb/services/crud/Route.php @@ -38,7 +38,7 @@ class Route extends Make /** * @param string $name * @param array $options - * @return mixed|void + * @return Route */ public function handle(string $name, array $options = []) { @@ -86,7 +86,10 @@ class Route extends Make $filePath = $this->getFilePathName($path, strtolower($name)); - return [$this->makeFile($filePath, $contentStr), $this->filePathName ?: $filePath]; + $this->setPathname($filePath); + $this->setContent($contentStr); + + return $this; } /** diff --git a/crmeb/crmeb/services/crud/Service.php b/crmeb/crmeb/services/crud/Service.php index 68f7602d6..ce6e252e7 100644 --- a/crmeb/crmeb/services/crud/Service.php +++ b/crmeb/crmeb/services/crud/Service.php @@ -10,8 +10,6 @@ // +---------------------------------------------------------------------- namespace crmeb\services\crud; -use crmeb\services\FormBuilder as Form; -use think\facade\Route as Url; use think\helper\Str; /** @@ -39,7 +37,7 @@ class Service extends Make /** * @param string $name * @param array $options - * @return mixed|void + * @return Service * @author 等风来 * @email 136327134@qq.com * @date 2023/3/23 @@ -106,12 +104,10 @@ class Service extends Make $contentStr = str_replace($this->var, $this->value, $content); $filePath = $this->getFilePathName($path, $this->value['nameCamel']); - - return [ - $this->makeFile($filePath, $contentStr), - $this->filePathName ?: $filePath, - $this->baseDir . '\\' . $this->value['nameCamel'] - ]; + $this->usePath = $this->baseDir . '\\' . $this->value['nameCamel']; + $this->setContent($contentStr); + $this->setPathname($filePath); + return $this; } /** diff --git a/crmeb/crmeb/services/crud/ViewApi.php b/crmeb/crmeb/services/crud/ViewApi.php index c5f5f69f9..0554a447f 100644 --- a/crmeb/crmeb/services/crud/ViewApi.php +++ b/crmeb/crmeb/services/crud/ViewApi.php @@ -61,7 +61,7 @@ class ViewApi extends Make /** * @param string $name * @param array $options - * @return array + * @return ViewApi * @author 等风来 * @email 136327134@qq.com * @date 2023/4/4 @@ -100,9 +100,9 @@ class ViewApi extends Make $contentStr = str_replace($this->var, $this->value, $content); $filePath = $this->getFilePathName($path, Str::camel($name)); - $content = $this->makeFile($filePath, $contentStr); - - return [$content, $this->filePathName ?: $filePath]; + $this->setPathname($filePath); + $this->setContent($contentStr); + return $this; } /** diff --git a/crmeb/crmeb/services/crud/ViewPages.php b/crmeb/crmeb/services/crud/ViewPages.php index 08088b3c3..034aa65a2 100644 --- a/crmeb/crmeb/services/crud/ViewPages.php +++ b/crmeb/crmeb/services/crud/ViewPages.php @@ -61,7 +61,7 @@ class ViewPages extends Make * @param string $name * @param string $path * @param array $options - * @return false|mixed|void + * @return ViewPages * @author 等风来 * @email 136327134@qq.com * @date 2023/4/3 diff --git a/crmeb/crmeb/services/crud/ViewRouter.php b/crmeb/crmeb/services/crud/ViewRouter.php index 125206095..9ab189fbe 100644 --- a/crmeb/crmeb/services/crud/ViewRouter.php +++ b/crmeb/crmeb/services/crud/ViewRouter.php @@ -61,7 +61,7 @@ class ViewRouter extends Make * @param string $name * @param string $path * @param array $options - * @return false|mixed|void + * @return ViewRouter * @author 等风来 * @email 136327134@qq.com * @date 2023/4/4 @@ -90,9 +90,9 @@ class ViewRouter extends Make $filePath = $this->getFilePathName($path, Str::camel($name)); - $makeContent = $this->makeFile($filePath, $contentStr); - - return [$makeContent, $filePath]; + $this->setContent($contentStr); + $this->setPathname($filePath); + return $this; } /** From e1bac8bbf9e73fe2c38bcfc0e11aff1ac0c8c752 Mon Sep 17 00:00:00 2001 From: liaofei <136327134@qq.com> Date: Tue, 18 Apr 2023 14:41:43 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=89=93=E5=8D=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crmeb/crmeb/services/FileService.php | 1 - 1 file changed, 1 deletion(-) diff --git a/crmeb/crmeb/services/FileService.php b/crmeb/crmeb/services/FileService.php index 7f4ffbe68..fea638f43 100644 --- a/crmeb/crmeb/services/FileService.php +++ b/crmeb/crmeb/services/FileService.php @@ -1085,7 +1085,6 @@ class FileService } $res = true; foreach ($files as $item) { - var_dump($item['value']); $res = $res && file_put_contents($item['path'], $item['content'], LOCK_EX); }