Make.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. <?php
  2. /**
  3. * +----------------------------------------------------------------------
  4. * | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  5. * +----------------------------------------------------------------------
  6. * | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  7. * +----------------------------------------------------------------------
  8. * | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  9. * +----------------------------------------------------------------------
  10. * | Author: CRMEB Team <admin@crmeb.com>
  11. * +----------------------------------------------------------------------
  12. */
  13. namespace crmeb\services\crud;
  14. use think\App;
  15. use think\helper\Str;
  16. /**
  17. * 创建crud基类
  18. * Class Make
  19. * @author 等风来
  20. * @email 136327134@qq.com
  21. * @date 2023/3/13
  22. * @package crmeb\services\crud
  23. */
  24. abstract class Make
  25. {
  26. /**
  27. * 名称
  28. * @var string
  29. */
  30. protected $name = '';
  31. /**
  32. * 文件类型
  33. * @var string
  34. */
  35. protected $fileMime = 'php';
  36. /**
  37. * 文件全部路径
  38. * @var string
  39. */
  40. protected $filePathName = null;
  41. /**
  42. * @var string
  43. */
  44. protected $fileBasePath;
  45. /**
  46. * 文件内容
  47. * @var string
  48. */
  49. protected $content = '';
  50. /**
  51. * 实际文件存放
  52. * @var string
  53. */
  54. protected $pathname = '';
  55. /**
  56. * 命名空间路径
  57. * @var string
  58. */
  59. protected $usePath = '';
  60. /**
  61. * 变量名称
  62. * @var array
  63. */
  64. protected $var = [];
  65. /**
  66. * 内容
  67. * @var array
  68. */
  69. protected $value = [];
  70. /**
  71. * 参数
  72. * @var array
  73. */
  74. protected $options = [];
  75. /**
  76. * 数据库获取器后缀
  77. * @var string
  78. */
  79. protected $attrPrefix = '_label';
  80. /**
  81. * 代码生成功能生成前端文件的路径
  82. * @var string
  83. */
  84. protected $adminTemplatePath;
  85. /**
  86. * 默认保存路径
  87. * @var string
  88. */
  89. protected $basePath;
  90. /**
  91. * 默认文件夹
  92. * @var string
  93. */
  94. protected $baseDir;
  95. /**
  96. * @var
  97. */
  98. protected $app;
  99. /**
  100. * Make constructor.
  101. * @param App $app
  102. */
  103. public function __construct(App $app)
  104. {
  105. $this->app = $app;
  106. $this->adminTemplatePath = self::adminTemplatePath();
  107. $this->basePath = $this->app->getRootPath();
  108. $this->baseDir = $this->setBaseDir();
  109. $this->var = $this->authDrawVar();
  110. $this->value = $this->drawValueKeys();
  111. $this->setDefaultValue();
  112. }
  113. /**
  114. * 设置默认路径
  115. * @param string $basePath
  116. * @return $this
  117. * @author 等风来
  118. * @email 136327134@qq.com
  119. * @date 2023/4/18
  120. */
  121. public function setbasePath(string $basePath)
  122. {
  123. if ($basePath) {
  124. $this->basePath = $basePath;
  125. }
  126. return $this;
  127. }
  128. /**
  129. * 获取字段后缀
  130. * @return string
  131. * @author 等风来
  132. * @email 136327134@qq.com
  133. * @date 2023/5/22
  134. */
  135. public function getAttrPrefix()
  136. {
  137. return $this->attrPrefix;
  138. }
  139. /**
  140. * @return string
  141. * @author 等风来
  142. * @email 136327134@qq.com
  143. * @date 2023/4/11
  144. */
  145. public static function adminTemplatePath()
  146. {
  147. return config('app.admin_template_path');
  148. }
  149. /**
  150. * 设置默认保存目录
  151. * @author 等风来
  152. * @email 136327134@qq.com
  153. * @date 2023/4/4
  154. */
  155. protected function setBaseDir(): string
  156. {
  157. return 'crud';
  158. }
  159. /**
  160. * 获取保存文件的目录
  161. * @param string $path
  162. * @return string
  163. * @author 等风来
  164. * @email 136327134@qq.com
  165. * @date 2023/4/4
  166. */
  167. protected function getBasePath(string $path = '')
  168. {
  169. //替换成本地路径格式
  170. $path = str_replace('/', DS, $path);
  171. $pathAttr = explode(DS, $path);
  172. $basePathAttr = explode(DS, $this->baseDir);
  173. //替换掉和基础目录相同的
  174. if (count($pathAttr) > 1) {
  175. $newsPath = array_merge(array_diff($basePathAttr, $pathAttr))[0] ?? '';
  176. if ($newsPath !== 'crud') {
  177. $path = $newsPath;
  178. } else {
  179. $this->baseDir = '';
  180. }
  181. }
  182. //多个斜杠的替换成一个
  183. $this->fileBasePath = str_replace(DS . DS, DS, $this->basePath . ($this->baseDir ? $this->baseDir . DS : '') . ($path ? $path . DS : ''));
  184. return $this->fileBasePath;
  185. }
  186. /**
  187. * 设置文件保存就路径名称
  188. * @param string $filePathName
  189. * @return $this
  190. * @author 等风来
  191. * @email 136327134@qq.com
  192. * @date 2023/4/7
  193. */
  194. public function setFilePathName(string $filePathName = '')
  195. {
  196. if ($filePathName) {
  197. $this->filePathName = $filePathName;
  198. }
  199. return $this;
  200. }
  201. /**
  202. * 生成tab
  203. * @param int $num
  204. * @return string
  205. * @author 等风来
  206. * @email 136327134@qq.com
  207. * @date 2023/3/29
  208. */
  209. public function tab(int $num = 1): string
  210. {
  211. return str_pad('', 4 * $num);
  212. }
  213. /**
  214. * 执行创建
  215. * @return Make
  216. * @author 等风来
  217. * @email 136327134@qq.com
  218. * @date 2023/3/13
  219. */
  220. public function handle(string $name, array $options = [])
  221. {
  222. $path = $options['path'] ?? '';
  223. [$nameData, $content] = $this->getStubContent($name);
  224. $this->value['NAME'] = $nameData;
  225. if (isset($this->value['NAME_CAMEL']) && !$this->value['NAME_CAMEL']) {
  226. $this->value['NAME_CAMEL'] = Str::studly($name);
  227. }
  228. if (isset($this->value['PATH'])) {
  229. $this->value['PATH'] = $this->getfolderPath($path);
  230. }
  231. if (isset($this->value['USE_PHP']) && !empty($options['usePath'])) {
  232. $this->value['USE_PHP'] = "use " . str_replace('/', '\\', $options['usePath']) . ";\n";
  233. }
  234. if (isset($this->value['MODEL_NAME']) && !$this->value['MODEL_NAME'] && !empty($options['modelName'])) {
  235. $this->value['MODEL_NAME'] = $options['modelName'];
  236. }
  237. $contentStr = str_replace($this->var, $this->value, $content);
  238. $filePath = $this->getFilePathName($path, $this->value['NAME_CAMEL']);
  239. $this->usePath = $this->baseDir . '\\' . $this->value['NAME_CAMEL'];
  240. $this->setPathname($filePath);
  241. $this->setContent($contentStr);
  242. return $this;
  243. }
  244. /**
  245. * 模板文件配置
  246. * @param string $type
  247. * @return mixed
  248. * @author 等风来
  249. * @email 136327134@qq.com
  250. * @date 2023/3/13
  251. */
  252. abstract protected function getStub(string $type = '');
  253. /**
  254. * 自动获取模板变量
  255. * @author 等风来
  256. * @email 136327134@qq.com
  257. * @date 2023/3/29
  258. */
  259. protected function authDrawVar(): array
  260. {
  261. $content = file_get_contents($this->getStub());
  262. $pattern = '/\{\%+[a-zA-Z0-9_-]+\%\}/';
  263. preg_match_all($pattern, $content, $var);
  264. $varData = $var[0] ?? [];
  265. $varData = array_unique($varData);
  266. return $varData;
  267. }
  268. /**
  269. * 提取value key
  270. * @author 等风来
  271. * @email 136327134@qq.com
  272. * @date 2023/3/29
  273. */
  274. protected function drawValueKeys(): array
  275. {
  276. $data = [];
  277. foreach ($this->var as $value) {
  278. $data[str_replace(['{%', '%}'], '', $value)] = '';
  279. }
  280. return $data;
  281. }
  282. /**
  283. * 设置默认值
  284. * @author 等风来
  285. * @email 136327134@qq.com
  286. * @date 2023/3/13
  287. */
  288. protected function setDefaultValue()
  289. {
  290. if (isset($this->value['YEAR'])) {
  291. $this->value['YEAR'] = date('Y');
  292. }
  293. if (isset($this->value['TIME'])) {
  294. $this->value['TIME'] = date('Y/m/d H:i:s');
  295. }
  296. if (isset($this->value['DATE'])) {
  297. $this->value['DATE'] = date('Y/m/d');
  298. }
  299. }
  300. /**
  301. * 提取模板文件
  302. * @param string $name
  303. * @return array
  304. * @author 等风来
  305. * @email 136327134@qq.com
  306. * @date 2023/3/13
  307. */
  308. protected function getStubContent(string $name, string $type = '')
  309. {
  310. $stub = file_get_contents($this->getStub($type));
  311. $namespace = trim(implode('\\', array_slice(explode('\\', $name), 0, -1)), '\\');
  312. $class = str_replace($namespace . '\\', '', $name);
  313. return [$class, $stub];
  314. }
  315. /**
  316. * 获取文件路径
  317. * @param string $path
  318. * @param string $name
  319. * @return string
  320. * @author 等风来
  321. * @email 136327134@qq.com
  322. * @date 2023/3/13
  323. */
  324. protected function getFilePathName(string $path, string $name): string
  325. {
  326. $path = ltrim(str_replace('\\', '/', $path), '/');
  327. return $this->getBasePath($path) . $name . ucwords($this->name) . '.' . $this->fileMime;
  328. }
  329. /**
  330. * @param string $path
  331. * @return mixed|string|null
  332. * @author 等风来
  333. * @email 136327134@qq.com
  334. * @date 2023/3/13
  335. */
  336. protected function getfolderPath(string $path)
  337. {
  338. $path = $path ?: $this->filePathName;
  339. $path = str_replace([$this->basePath, $this->baseDir], '', $path);
  340. $path = ltrim(str_replace('\\', '/', $path), '/');
  341. $pathArr = explode('/', $path);
  342. array_pop($pathArr);
  343. if ($pathArr) {
  344. return '\\' . implode('\\', $pathArr);
  345. } else {
  346. return '';
  347. }
  348. }
  349. /**
  350. * 获取保存文件路径
  351. * @param string $name
  352. * @return string
  353. * @author 等风来
  354. * @email 136327134@qq.com
  355. * @date 2023/3/13
  356. */
  357. protected function getPathName(string $name): string
  358. {
  359. $name = str_replace('app\\', '', $name);
  360. return $this->app->getBasePath() . ltrim(str_replace('\\', '/', $name), '/') . '.php';
  361. }
  362. /**
  363. * 获取类名
  364. * @param string $name
  365. * @return string
  366. * @author 等风来
  367. * @email 136327134@qq.com
  368. * @date 2023/3/13
  369. */
  370. protected function getClassName(string $name): string
  371. {
  372. if (strpos($name, '\\') !== false) {
  373. return $name;
  374. }
  375. if (strpos($name, '@')) {
  376. [$app, $name] = explode('@', $name);
  377. } else {
  378. $app = '';
  379. }
  380. if (strpos($name, '/') !== false) {
  381. $name = str_replace('/', '\\', $name);
  382. }
  383. return $this->getNamespace($app) . '\\' . $name;
  384. }
  385. /**
  386. * 获取命名空间名
  387. * @param string $app
  388. * @return string
  389. * @author 等风来
  390. * @email 136327134@qq.com
  391. * @date 2023/3/13
  392. */
  393. protected function getNamespace(string $app): string
  394. {
  395. return 'app' . ($app ? '\\' . $app : '');
  396. }
  397. /**
  398. * 设置内容
  399. * @param string $content
  400. * @return array|string|string[]
  401. * @author 等风来
  402. * @email 136327134@qq.com
  403. * @date 2023/4/14
  404. */
  405. protected function setContent(string $content)
  406. {
  407. $this->content = str_replace('', '', $content);
  408. return $this->content;
  409. }
  410. /**
  411. * @param string $pathname
  412. * @return $this
  413. * @author 等风来
  414. * @email 136327134@qq.com
  415. * @date 2023/4/18
  416. */
  417. protected function setPathname(string $pathname)
  418. {
  419. $this->pathname = $this->filePathName ?: $pathname;
  420. return $this;
  421. }
  422. /**
  423. * @param string $key
  424. * @return mixed|null
  425. * @author 等风来
  426. * @email 136327134@qq.com
  427. * @date 2023/4/18
  428. */
  429. public function getValue(string $key)
  430. {
  431. return $this->value[$key] ?? null;
  432. }
  433. /**
  434. * 获取命名空间路径
  435. * @return string
  436. * @author 等风来
  437. * @email 136327134@qq.com
  438. * @date 2023/4/18
  439. */
  440. public function getUsePath()
  441. {
  442. return $this->usePath;
  443. }
  444. /**
  445. * 获取内容
  446. * @return string
  447. * @author 等风来
  448. * @email 136327134@qq.com
  449. * @date 2023/4/18
  450. */
  451. public function getContent()
  452. {
  453. return $this->content;
  454. }
  455. /**
  456. * @return string
  457. * @author 等风来
  458. * @email 136327134@qq.com
  459. * @date 2023/4/18
  460. */
  461. public function getPath()
  462. {
  463. return $this->pathname;
  464. }
  465. /**
  466. * @return array
  467. * @author 等风来
  468. * @email 136327134@qq.com
  469. * @date 2023/4/18
  470. */
  471. public function toArray()
  472. {
  473. return [
  474. 'path' => $this->pathname,
  475. 'content' => $this->content,
  476. 'value' => $this->value,
  477. 'var' => $this->var,
  478. 'usePath' => $this->usePath,
  479. ];
  480. }
  481. public function __destruct()
  482. {
  483. $this->content = '';
  484. $this->pathname = '';
  485. $this->usePath = '';
  486. }
  487. }