Make.php 12 KB

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