123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546 |
- <?php
- /**
- * +----------------------------------------------------------------------
- * | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- * +----------------------------------------------------------------------
- * | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
- * +----------------------------------------------------------------------
- * | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- * +----------------------------------------------------------------------
- * | Author: CRMEB Team <admin@crmeb.com>
- * +----------------------------------------------------------------------
- */
- namespace crmeb\services\crud;
- use think\App;
- use think\helper\Str;
- /**
- * 创建crud基类
- * Class Make
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/3/13
- * @package crmeb\services\crud
- */
- abstract class Make
- {
- /**
- * 名称
- * @var string
- */
- protected $name = '';
- /**
- * 文件类型
- * @var string
- */
- protected $fileMime = 'php';
- /**
- * 文件全部路径
- * @var string
- */
- protected $filePathName = null;
- /**
- * @var string
- */
- protected $fileBasePath;
- /**
- * 文件内容
- * @var string
- */
- protected $content = '';
- /**
- * 实际文件存放
- * @var string
- */
- protected $pathname = '';
- /**
- * 命名空间路径
- * @var string
- */
- protected $usePath = '';
- /**
- * 变量名称
- * @var array
- */
- protected $var = [];
- /**
- * 内容
- * @var array
- */
- protected $value = [];
- /**
- * 参数
- * @var array
- */
- protected $options = [];
- /**
- * 数据库获取器后缀
- * @var string
- */
- protected $attrPrefix = '_label';
- /**
- * 代码生成功能生成前端文件的路径
- * @var string
- */
- protected $adminTemplatePath;
- /**
- * 默认保存路径
- * @var string
- */
- protected $basePath;
- /**
- * 默认文件夹
- * @var string
- */
- protected $baseDir;
- /**
- * @var
- */
- protected $app;
- /**
- * Make constructor.
- * @param App $app
- */
- public function __construct(App $app)
- {
- $this->app = $app;
- $this->adminTemplatePath = self::adminTemplatePath();
- $this->basePath = $this->app->getRootPath();
- $this->baseDir = $this->setBaseDir();
- $this->var = $this->authDrawVar();
- $this->value = $this->drawValueKeys();
- $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 等风来
- * @email 136327134@qq.com
- * @date 2023/5/22
- */
- public function getAttrPrefix()
- {
- return $this->attrPrefix;
- }
- /**
- * @return string
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/11
- */
- public static function adminTemplatePath()
- {
- return config('app.admin_template_path');
- }
- /**
- * 设置默认保存目录
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/4
- */
- protected function setBaseDir(): string
- {
- return 'crud';
- }
- /**
- * 获取保存文件的目录
- * @param string $path
- * @return string
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/4
- */
- protected function getBasePath(string $path = '')
- {
- //替换成本地路径格式
- $path = str_replace('/', DS, $path);
- $pathAttr = explode(DS, $path);
- $basePathAttr = explode(DS, $this->baseDir);
- //替换掉和基础目录相同的
- if (count($pathAttr) > 1) {
- $newsPath = array_merge(array_diff($basePathAttr, $pathAttr))[0] ?? '';
- if ($newsPath !== 'crud') {
- $path = $newsPath;
- } else {
- $this->baseDir = '';
- }
- }
- //多个斜杠的替换成一个
- $this->fileBasePath = str_replace(DS . DS, DS, $this->basePath . ($this->baseDir ? $this->baseDir . DS : '') . ($path ? $path . DS : ''));
- return $this->fileBasePath;
- }
- /**
- * 设置文件保存就路径名称
- * @param string $filePathName
- * @return $this
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/7
- */
- public function setFilePathName(string $filePathName = '')
- {
- if ($filePathName) {
- $this->filePathName = $filePathName;
- }
- return $this;
- }
- /**
- * 生成tab
- * @param int $num
- * @return string
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/3/29
- */
- public function tab(int $num = 1): string
- {
- return str_pad('', 4 * $num);
- }
- /**
- * 执行创建
- * @return Make
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/3/13
- */
- public function handle(string $name, array $options = [])
- {
- $path = $options['path'] ?? '';
- [$nameData, $content] = $this->getStubContent($name);
- $this->value['NAME'] = $nameData;
- if (isset($this->value['NAME_CAMEL']) && !$this->value['NAME_CAMEL']) {
- $this->value['NAME_CAMEL'] = Str::studly($name);
- }
- if (isset($this->value['PATH'])) {
- $this->value['PATH'] = $this->getfolderPath($path);
- }
- if (isset($this->value['USE_PHP']) && !empty($options['usePath'])) {
- $this->value['USE_PHP'] = "use " . str_replace('/', '\\', $options['usePath']) . ";\n";
- }
- if (isset($this->value['MODEL_NAME']) && !$this->value['MODEL_NAME'] && !empty($options['modelName'])) {
- $this->value['MODEL_NAME'] = $options['modelName'];
- }
- $contentStr = str_replace($this->var, $this->value, $content);
- $filePath = $this->getFilePathName($path, $this->value['NAME_CAMEL']);
- $this->usePath = $this->baseDir . '\\' . $this->value['NAME_CAMEL'];
- $this->setPathname($filePath);
- $this->setContent($contentStr);
- return $this;
- }
- /**
- * 模板文件配置
- * @param string $type
- * @return mixed
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/3/13
- */
- abstract protected function getStub(string $type = '');
- /**
- * 自动获取模板变量
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/3/29
- */
- protected function authDrawVar(): array
- {
- $content = file_get_contents($this->getStub());
- $pattern = '/\{\%+[a-zA-Z0-9_-]+\%\}/';
- preg_match_all($pattern, $content, $var);
- $varData = $var[0] ?? [];
- $varData = array_unique($varData);
- return $varData;
- }
- /**
- * 提取value key
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/3/29
- */
- protected function drawValueKeys(): array
- {
- $data = [];
- foreach ($this->var as $value) {
- $data[str_replace(['{%', '%}'], '', $value)] = '';
- }
- return $data;
- }
- /**
- * 设置默认值
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/3/13
- */
- protected function setDefaultValue()
- {
- if (isset($this->value['YEAR'])) {
- $this->value['YEAR'] = date('Y');
- }
- if (isset($this->value['TIME'])) {
- $this->value['TIME'] = date('Y/m/d H:i:s');
- }
- if (isset($this->value['DATE'])) {
- $this->value['DATE'] = date('Y/m/d');
- }
- }
- /**
- * 提取模板文件
- * @param string $name
- * @return array
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/3/13
- */
- protected function getStubContent(string $name, string $type = '')
- {
- $stub = file_get_contents($this->getStub($type));
- $namespace = trim(implode('\\', array_slice(explode('\\', $name), 0, -1)), '\\');
- $class = str_replace($namespace . '\\', '', $name);
- return [$class, $stub];
- }
- /**
- * 获取文件路径
- * @param string $path
- * @param string $name
- * @return string
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/3/13
- */
- protected function getFilePathName(string $path, string $name): string
- {
- $path = ltrim(str_replace('\\', '/', $path), '/');
- return $this->getBasePath($path) . $name . ucwords($this->name) . '.' . $this->fileMime;
- }
- /**
- * @param string $path
- * @return mixed|string|null
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/3/13
- */
- protected function getfolderPath(string $path)
- {
- $path = $path ?: $this->filePathName;
- $path = str_replace([$this->basePath, $this->baseDir], '', $path);
- $path = ltrim(str_replace('\\', '/', $path), '/');
- $pathArr = explode('/', $path);
- array_pop($pathArr);
- if ($pathArr) {
- return '\\' . implode('\\', $pathArr);
- } else {
- return '';
- }
- }
- /**
- * 获取保存文件路径
- * @param string $name
- * @return string
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/3/13
- */
- protected function getPathName(string $name): string
- {
- $name = str_replace('app\\', '', $name);
- return $this->app->getBasePath() . ltrim(str_replace('\\', '/', $name), '/') . '.php';
- }
- /**
- * 获取类名
- * @param string $name
- * @return string
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/3/13
- */
- protected function getClassName(string $name): string
- {
- if (strpos($name, '\\') !== false) {
- return $name;
- }
- if (strpos($name, '@')) {
- [$app, $name] = explode('@', $name);
- } else {
- $app = '';
- }
- if (strpos($name, '/') !== false) {
- $name = str_replace('/', '\\', $name);
- }
- return $this->getNamespace($app) . '\\' . $name;
- }
- /**
- * 获取命名空间名
- * @param string $app
- * @return string
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/3/13
- */
- protected function getNamespace(string $app): string
- {
- return 'app' . ($app ? '\\' . $app : '');
- }
- /**
- * 设置内容
- * @param string $content
- * @return array|string|string[]
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/14
- */
- 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/4/18
- */
- public function getUsePath()
- {
- return $this->usePath;
- }
- /**
- * 获取内容
- * @return string
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/18
- */
- public function getContent()
- {
- 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 = '';
- }
- }
|