Model.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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\services\crud\enum\FormTypeEnum;
  15. use think\helper\Str;
  16. /**
  17. * Class Model
  18. * @author 等风来
  19. * @email 136327134@qq.com
  20. * @date 2023/3/13
  21. * @package crmeb\command\crud
  22. */
  23. class Model extends Make
  24. {
  25. /**
  26. * 当前命令名称
  27. * @var string
  28. */
  29. protected $name = "model";
  30. /**
  31. * @return string
  32. * @author 等风来
  33. * @email 136327134@qq.com
  34. * @date 2023/4/4
  35. */
  36. protected function setBaseDir(): string
  37. {
  38. return 'app' . DS . 'model' . DS . 'crud';
  39. }
  40. /**
  41. * @param string $name
  42. * @param array $options
  43. * @return Model
  44. * @author 等风来
  45. * @email 136327134@qq.com
  46. * @date 2023/4/12
  47. */
  48. public function handle(string $name, array $options = [])
  49. {
  50. $this->options = $options;
  51. $field = $options['fromField'] ?? [];
  52. $hasOneFields = $options['hasOneField'] ?? [];
  53. $this->value['KEY'] = $options['key'] ?? 'id';
  54. $this->value['MODEL_NAME'] = $options['modelName'] ?? $name;
  55. $this->setUseDeleteContent()
  56. ->setAttrFnContent($field)
  57. ->setHasOneContent($hasOneFields);
  58. return parent::handle($name, $options);
  59. }
  60. /**
  61. * 设置命令空间
  62. * @return $this
  63. * @author 等风来
  64. * @email 136327134@qq.com
  65. * @date 2023/8/12
  66. */
  67. protected function setUseDeleteContent()
  68. {
  69. if (isset($this->options['softDelete']) && $this->options['softDelete']) {
  70. $this->value['USE_PHP'] = "use think\model\concern\SoftDelete;\n";
  71. $this->value['CONTENT_PHP'] = $this->tab() . "use SoftDelete;\n";
  72. }
  73. return $this;
  74. }
  75. /**
  76. * 设置获取字段方法内容
  77. * @param array $field
  78. * @return $this
  79. * @author 等风来
  80. * @email 136327134@qq.com
  81. * @date 2023/8/12
  82. */
  83. protected function setAttrFnContent(array $field)
  84. {
  85. $attrFnContent = [];
  86. foreach ($field as $item) {
  87. if (in_array($item['type'], [FormTypeEnum::RADIO, FormTypeEnum::SELECT]) && !empty($item['option'])) {
  88. $attrFnContent[] = $this->getAttrFnContent($item['field'], $item['name'], $item['option']);
  89. } else if (FormTypeEnum::CHECKBOX == $item['type']) {
  90. $attrFnContent[] = $this->getAttrFnCheckboxContent($item['field'], $item['name'], $item['option']);
  91. } else if (in_array($item['type'], [FormTypeEnum::FRAME_IMAGES, FormTypeEnum::DATE_TIME_RANGE])) {
  92. $attrFnContent[] = $this->getAttrJoinFnContent($item['field'], $item['name']);
  93. }
  94. }
  95. if ($attrFnContent) {
  96. $this->value['ATTR_PHP'] = "\n" . implode("\n", $attrFnContent);
  97. }
  98. return $this;
  99. }
  100. /**
  101. * 设置hasone方法内容
  102. * @param array $hasOneFields
  103. * @return $this
  104. * @author 等风来
  105. * @email 136327134@qq.com
  106. * @date 2023/8/12
  107. */
  108. protected function setHasOneContent(array $hasOneFields)
  109. {
  110. $hasOneContent = $this->getHasPhpContent($hasOneFields);
  111. if ($hasOneContent) {
  112. $this->value['ATTR_PHP'] .= "\n" . $hasOneContent;
  113. }
  114. return $this;
  115. }
  116. /**
  117. * 转JSON数据获取器
  118. * @param string $key
  119. * @param string $name
  120. * @return array|false|string|string[]
  121. * @author 等风来
  122. * @email 136327134@qq.com
  123. * @date 2023/9/5
  124. */
  125. public function getAttrJoinFnContent(string $key, string $name)
  126. {
  127. $attrFnStub = file_get_contents($this->getStub('attr'));
  128. $var = [
  129. '{%FIELD%}',
  130. '{%DATE%}',
  131. '{%NAME%}',
  132. '{%CONTENT_PHP%}'
  133. ];
  134. $tab = $this->tab(2);
  135. $content = <<<CONTENT
  136. $tab\$value = \$value ? json_decode(\$value, true) : [];
  137. {$tab}return \$value;
  138. CONTENT;
  139. $value = [
  140. Str::studly($key . $this->attrPrefix),
  141. date('Y-m-d'),
  142. $name,
  143. $content
  144. ];
  145. return str_replace($var, $value, $attrFnStub);
  146. }
  147. /**
  148. * Checkbox代码获取
  149. * @param string $key
  150. * @param string $comment
  151. * @param array $options
  152. * @return array|false|string|string[]
  153. * @author 等风来
  154. * @email 136327134@qq.com
  155. * @date 2023/8/9
  156. */
  157. protected function getAttrFnCheckboxContent(string $key, string $comment, array $options)
  158. {
  159. $optionsStr = '';
  160. $tab2 = $this->tab(2);
  161. $tab3 = $this->tab(3);
  162. $tab4 = $this->tab(4);
  163. foreach ($options as $i => $option) {
  164. if (0 == $i) {
  165. $n = '';
  166. } else {
  167. $n = "\n";
  168. }
  169. $optionsStr .= <<<CONTENT
  170. {$n}{$tab3}[
  171. $tab4'value' => '$option[value]',
  172. $tab4'label' => '$option[label]',
  173. {$tab3}],
  174. CONTENT;
  175. }
  176. $content = <<<CONTENT
  177. {$tab2}\$options = [
  178. $optionsStr
  179. {$tab2}];
  180. {$tab2}\$var = [];
  181. {$tab2}\$value = \$value ? json_decode(\$value, true) : [];
  182. {$tab2}foreach(\$options as \$item) {
  183. {$tab2} if (is_array(\$value) && in_array(\$item['value'], \$value)) {
  184. {$tab2} \$var[] = \$item['label'];
  185. {$tab2} }
  186. {$tab2}}
  187. {$tab2}return implode(',', \$var);
  188. CONTENT;
  189. $var = [
  190. '{%FIELD%}',
  191. '{%DATE%}',
  192. '{%NAME%}',
  193. '{%CONTENT_PHP%}'
  194. ];
  195. $value = [
  196. Str::studly($key . $this->attrPrefix),
  197. date('Y-m-d'),
  198. $comment,
  199. $content
  200. ];
  201. $attrFnStub = file_get_contents($this->getStub('attr'));
  202. return str_replace($var, $value, $attrFnStub);
  203. }
  204. /**
  205. * 获取获取器的方法内容
  206. * @param string $key
  207. * @param string $comment
  208. * @param array $options
  209. * @return array|false|string|string[]
  210. * @author 等风来
  211. * @email 136327134@qq.com
  212. * @date 2023/5/11
  213. */
  214. protected function getAttrFnContent(string $key, string $comment, array $options)
  215. {
  216. $attrFnStub = file_get_contents($this->getStub('attr'));
  217. $var = [
  218. '{%FIELD%}',
  219. '{%DATE%}',
  220. '{%NAME%}',
  221. '{%CONTENT_PHP%}'
  222. ];
  223. $value = [
  224. Str::studly($key . $this->attrPrefix),
  225. date('Y-m-d'),
  226. $comment,
  227. $this->getSwithAndSelectPhpContent($options)
  228. ];
  229. return str_replace($var, $value, $attrFnStub);
  230. }
  231. /**
  232. * 获取开关和下拉框获取器内容
  233. * @param array $options
  234. * @return string
  235. * @author 等风来
  236. * @email 136327134@qq.com
  237. * @date 2023/5/11
  238. */
  239. protected function getSwithAndSelectPhpContent(array $options)
  240. {
  241. if (!$options) {
  242. return '';
  243. }
  244. $case = [];
  245. foreach ($options as $option) {
  246. $case[] = $this->tab(3) . "case " . $option['value'] . ":\n" . $this->tab(4) . "\$attr = '$option[label]';\n" . $this->tab(4) . "break;";
  247. }
  248. $caseContent = implode("\n", $case);
  249. $tab2 = $this->tab(2);
  250. $content = <<<CONTENT
  251. {$tab2}\$attr = '';
  252. {$tab2}switch ((int)\$value){
  253. {$caseContent}
  254. {$tab2}}
  255. {$tab2}return \$attr;
  256. CONTENT;
  257. return $content;
  258. }
  259. /**
  260. * 获取关联数据模板
  261. * @param array $fields
  262. * @return string
  263. * @author 等风来
  264. * @email 136327134@qq.com
  265. * @date 2023/8/4
  266. */
  267. protected function getHasPhpContent(array $fields)
  268. {
  269. $hasOneStub = file_get_contents($this->getStub('hasOne'));
  270. $date = date('Y/m/d');
  271. $content = '';
  272. foreach ($fields as $item) {
  273. if (isset($item['hasOne']) && $item['hasOne']) {
  274. [$modelName, $foreignKey] = is_array($item['hasOne']) ? $item['hasOne'] : [$item['hasOne'], 'id'];
  275. $modelName = self::getHasOneNamePases($modelName);
  276. if (!$modelName) {
  277. continue;
  278. }
  279. $content .= "\n" . str_replace(
  280. [
  281. '{%NAME%}',
  282. '{%DATE%}',
  283. '{%FIELD%}',
  284. '{%CLASS%}',
  285. '{%FOREIGN_KEY%}',
  286. '{%LOCAL_KEY%}'
  287. ],
  288. [
  289. $item['name'],
  290. $date,
  291. Str::camel($item['field']),
  292. $modelName,
  293. $foreignKey,
  294. $item['field']
  295. ],
  296. $hasOneStub
  297. );
  298. }
  299. }
  300. return $content;
  301. }
  302. /**
  303. * @param string $path
  304. * @param string $name
  305. * @return string
  306. * @author 等风来
  307. * @email 136327134@qq.com
  308. * @date 2023/4/12
  309. */
  310. protected function getFilePathName(string $path, string $name): string
  311. {
  312. $path = ltrim(str_replace('\\', '/', $path), '/');
  313. return $this->getBasePath($path) . $name . '.' . $this->fileMime;
  314. }
  315. /**
  316. * 模板文件
  317. * @param string $type
  318. * @return string
  319. * @author 等风来
  320. * @email 136327134@qq.com
  321. * @date 2023/3/13
  322. */
  323. protected function getStub(string $type = 'model')
  324. {
  325. $routePath = __DIR__ . DS . 'stubs' . DS . 'model' . DS;
  326. $stubs = [
  327. 'model' => $routePath . 'crudModel.stub',
  328. 'attr' => $routePath . 'getattr.stub',
  329. 'hasOne' => $routePath . 'hasOne.stub',
  330. 'hasMany' => $routePath . 'hasMany.stub',
  331. ];
  332. return $type ? $stubs[$type] : $stubs['model'];
  333. }
  334. /**
  335. * 获取模型命令空间
  336. * @param string $modelName
  337. * @return string
  338. * @author 等风来
  339. * @email 136327134@qq.com
  340. * @date 2023/8/8
  341. */
  342. public static function getHasOneNamePases(string $modelName)
  343. {
  344. $dir = root_path('app' . DS . 'model');
  345. $res = self::searchFiles($dir, '$name = \'' . $modelName . '\'');
  346. $namepases = '';
  347. foreach ($res as $item) {
  348. $namepases = self::getFileNamespace($item);
  349. }
  350. return $namepases ? "\\" . $namepases . '\\' . Str::studly($modelName) . "::class" : '';
  351. }
  352. /**
  353. * 搜索文件内容包含某个字符串,返回包含的文件路径
  354. * @param string $dir
  355. * @param string $searchString
  356. * @return array
  357. * @author 等风来
  358. * @email 136327134@qq.com
  359. * @date 2023/8/8
  360. */
  361. public static function searchFiles(string $dir, string $searchString)
  362. {
  363. $foundFiles = [];
  364. $files = scandir($dir);
  365. foreach ($files as $file) {
  366. if ($file === '.' || $file === '..') {
  367. continue;
  368. }
  369. $path = $dir . '/' . $file;
  370. if (is_dir($path)) {
  371. $foundFiles = array_merge($foundFiles, self::searchFiles($path, $searchString));
  372. } else {
  373. $content = file_get_contents($path);
  374. if (strpos($content, $searchString) !== false) {
  375. $foundFiles[] = $path;
  376. }
  377. }
  378. }
  379. return $foundFiles;
  380. }
  381. /**
  382. * 获取文件的命名空间
  383. * @param string $filePath
  384. * @return string
  385. * @author 等风来
  386. * @email 136327134@qq.com
  387. * @date 2023/8/8
  388. */
  389. public static function getFileNamespace(string $filePath)
  390. {
  391. $content = file_get_contents($filePath);
  392. $tokens = token_get_all($content);
  393. $namespace = '';
  394. foreach ($tokens as $token) {
  395. if ($token[0] === T_NAMESPACE) {
  396. $namespace = '';
  397. } elseif ($namespace !== null && in_array($token[0], [T_STRING, T_NS_SEPARATOR])) {
  398. $namespace .= $token[1];
  399. } elseif ($token === ';') {
  400. break;
  401. }
  402. }
  403. return $namespace;
  404. }
  405. }