Controller.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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\ActionEnum;
  15. use crmeb\services\crud\enum\FormTypeEnum;
  16. use think\helper\Str;
  17. /**
  18. * 创建Controller
  19. * Class Controller
  20. * @author 等风来
  21. * @email 136327134@qq.com
  22. * @date 2023/3/13
  23. * @package crmeb\servives\crud
  24. */
  25. class Controller extends Make
  26. {
  27. /**
  28. * @var string
  29. */
  30. protected $name = 'controller';
  31. /**
  32. * @return string
  33. * @author 等风来
  34. * @email 136327134@qq.com
  35. * @date 2023/4/4
  36. */
  37. protected function setBaseDir(): string
  38. {
  39. return 'app' . DS . 'adminapi' . DS . 'controller' . DS . 'crud';
  40. }
  41. /**
  42. * @return Controller
  43. * @author 等风来
  44. * @email 136327134@qq.com
  45. * @date 2023/3/13
  46. */
  47. public function handle(string $name, array $options = [])
  48. {
  49. $this->options = $options;
  50. $path = $options['path'] ?? '';
  51. $field = $options['field'] ?? [];
  52. $hasOneFields = $options['hasOneField'] ?? [];
  53. $this->value['MODEL_NAME'] = $options['modelName'] ?? $name;
  54. $this->value['NAME_CAMEL'] = Str::studly($name);
  55. $this->value['PATH'] = $this->getfolderPath($path);
  56. return $this->setUseContent()
  57. ->setControllerContent($field,
  58. $options['searchField'] ?? [],
  59. $name,
  60. $options['columnField'] ?? [],
  61. $hasOneFields)
  62. ->setController($name, $path);
  63. }
  64. /**
  65. * 设置控制器内容
  66. * @param string $name
  67. * @param string $path
  68. * @return $this
  69. * @author 等风来
  70. * @email 136327134@qq.com
  71. * @date 2023/8/12
  72. */
  73. protected function setController(string $name, string $path)
  74. {
  75. [$className, $contentController] = $this->getStubContent($name, 'controller');
  76. $this->value['NAME'] = $className;
  77. $contentStr = str_replace($this->var, $this->value, $contentController);
  78. $filePath = $this->getFilePathName($path, $this->value['NAME_CAMEL']);
  79. $this->usePath = $this->value['PATH'];
  80. $this->setPathname($filePath);
  81. $this->setContent($contentStr);
  82. return $this;
  83. }
  84. /**
  85. * 设置use内容
  86. * @return $this
  87. * @author 等风来
  88. * @email 136327134@qq.com
  89. * @date 2023/8/12
  90. */
  91. protected function setUseContent()
  92. {
  93. $this->value['USE_PHP'] = "use " . str_replace('/', '\\', $this->options['usePath']) . "Services;\n";
  94. return $this;
  95. }
  96. /**
  97. * 设置控制器内容
  98. * @param array $field
  99. * @param array $searchField
  100. * @param string $name
  101. * @param array $columnField
  102. * @param array $hasOneFields
  103. * @return $this
  104. * @author 等风来
  105. * @email 136327134@qq.com
  106. * @date 2023/8/12
  107. */
  108. protected function setControllerContent(array $field, array $searchField, string $name, array $columnField, array $hasOneFields)
  109. {
  110. $var = [
  111. "{%DATE%}",
  112. '{%VALIDATE_NAME%}',
  113. '{%FIELD_PHP%}',
  114. '{%FIELD%}',
  115. '{%WITH%}',
  116. '{%OTHER_PHP%}',
  117. '{%FIELD_ALL_PHP%}',
  118. '{%FIELD_SEARCH_PHP%}'
  119. ];
  120. $replace = [
  121. $this->value['DATE'],
  122. $this->options['validateName'] ?? '',
  123. $this->getSearchFieldContent($field),
  124. $this->getSearchListFieldContent($columnField),
  125. $this->getSearchListWithContent($hasOneFields),
  126. $this->getSearchListOtherContent($columnField),
  127. $this->getStatusUpdateContent($columnField),
  128. $this->getSearchPhpContent($searchField)
  129. ];
  130. $this->value['CONTENT_PHP'] = str_replace($var, $replace, $this->getStubControllerContent($name));
  131. return $this;
  132. }
  133. /**
  134. * 获取搜索字段内容
  135. * @param array $field
  136. * @return string
  137. * @author 等风来
  138. * @email 136327134@qq.com
  139. * @date 2023/8/12
  140. */
  141. protected function getSearchFieldContent(array $field)
  142. {
  143. $fieldStr = '';
  144. foreach ($field as $k) {
  145. $fieldStr .= $this->tab(3) . "['$k', ''],\n";
  146. }
  147. return $fieldStr;
  148. }
  149. /**
  150. * 提取控制器模板内容
  151. * @param string $name
  152. * @return string
  153. * @author 等风来
  154. * @email 136327134@qq.com
  155. * @date 2023/8/12
  156. */
  157. protected function getStubControllerContent(string $name)
  158. {
  159. $contentPhp = '';
  160. foreach (ActionEnum::ACTION_ALL as $item) {
  161. [, $stub] = $this->getStubContent($name, $item);
  162. $contentPhp .= $stub . "\r\n";
  163. }
  164. return $contentPhp;
  165. }
  166. /**
  167. * 设置搜索字段展示
  168. * @param array $columnField
  169. * @return string
  170. * @author 等风来
  171. * @email 136327134@qq.com
  172. * @date 2023/8/12
  173. */
  174. protected function getSearchListFieldContent(array $columnField)
  175. {
  176. $select = [];
  177. foreach ($columnField as $item) {
  178. //处理查询字段
  179. if (in_array($item['type'], [
  180. FormTypeEnum::DATE_TIME_RANGE,
  181. FormTypeEnum::FRAME_IMAGES,
  182. FormTypeEnum::RADIO,
  183. FormTypeEnum::SELECT,
  184. FormTypeEnum::CHECKBOX])) {
  185. $select[] = '`' . $item['field'] . '` as ' . $item['field'] . $this->attrPrefix;
  186. }
  187. }
  188. unset($item);
  189. return $select ? '\'*\',\'' . implode('\',\'', $select) . '\'' : '\'*\'';
  190. }
  191. /**
  192. * 设置搜索关联内容
  193. * @param array $hasOneFields
  194. * @return string
  195. * @author 等风来
  196. * @email 136327134@qq.com
  197. * @date 2023/8/12
  198. */
  199. protected function getSearchListWithContent(array $hasOneFields)
  200. {
  201. $with = [];
  202. foreach ($hasOneFields as $item) {
  203. if (isset($item['hasOne'])) {
  204. [$modelName,] = is_array($item['hasOne']) ? $item['hasOne'] : [$item['hasOne'], 'id'];
  205. $modelName = Model::getHasOneNamePases($modelName);
  206. if (!$modelName) {
  207. continue;
  208. }
  209. $with[] = "'" . Str::camel($item['field']) . 'HasOne' . "'";
  210. }
  211. }
  212. unset($item);
  213. return $with ? implode(',', $with) : '[]';
  214. }
  215. /**
  216. * 获取可以修改的字段内容
  217. * @param array $columnField
  218. * @return string
  219. * @author 等风来
  220. * @email 136327134@qq.com
  221. * @date 2023/9/6
  222. */
  223. public function getStatusUpdateContent(array $columnField)
  224. {
  225. $fieldAll = [];
  226. foreach ($columnField as $item) {
  227. if ($item['type'] == FormTypeEnum::SWITCH) {
  228. $fieldAll[] = $item['field'];
  229. }
  230. }
  231. return $fieldAll ? "['" . implode("','", $fieldAll) . "']" : '[]';
  232. }
  233. /**
  234. * 设置搜索其他内容
  235. * @param array $columnField
  236. * @return string
  237. * @author 等风来
  238. * @email 136327134@qq.com
  239. * @date 2023/8/12
  240. */
  241. protected function getSearchListOtherContent(array $columnField)
  242. {
  243. $otherContent = '';
  244. foreach ($columnField as $item) {
  245. //处理查询字段
  246. if (in_array($item['type'], [FormTypeEnum::FRAME_IMAGES, FormTypeEnum::CHECKBOX, FormTypeEnum::DATE_TIME_RANGE])) {
  247. if (!$otherContent) {
  248. $otherContent .= "\n";
  249. }
  250. $otherContent .= $this->tab(2) . '$data[\'' . $item['field'] . '\'] = json_encode($data[\'' . $item['field'] . "']);\n";
  251. }
  252. }
  253. return $otherContent;
  254. }
  255. /**
  256. * 获取控制器中搜索内容
  257. * @param array $fields
  258. * @return string
  259. * @author 等风来
  260. * @email 136327134@qq.com
  261. * @date 2023/8/3
  262. */
  263. protected function getSearchPhpContent(array $fields)
  264. {
  265. $fieldStr = '';
  266. foreach ($fields as $i => $item) {
  267. if (!empty($item['search'])) {
  268. $fieldStr .= $this->tab(3) . "['$item[field]', '']," . ((count($fields) - 1) == $i ? "" : "\n");
  269. }
  270. }
  271. return $fieldStr;
  272. }
  273. /**
  274. * 返回模板路径
  275. * @param string $type
  276. * @return string|string[]
  277. * @author 等风来
  278. * @email 136327134@qq.com
  279. * @date 2023/3/13
  280. */
  281. protected function getStub(string $type = 'controller')
  282. {
  283. $controllerPath = __DIR__ . DS . 'stubs' . DS . 'controller' . DS;
  284. $stubs = [
  285. 'index' => $controllerPath . 'index.stub',
  286. 'create' => $controllerPath . 'create.stub',
  287. 'save' => $controllerPath . 'save.stub',
  288. 'status' => $controllerPath . 'status.stub',
  289. 'edit' => $controllerPath . 'edit.stub',
  290. 'update' => $controllerPath . 'update.stub',
  291. 'delete' => $controllerPath . 'delete.stub',
  292. 'read' => $controllerPath . 'read.stub',
  293. 'controller' => $controllerPath . 'crudController.stub',
  294. ];
  295. return $type ? $stubs[$type] : $stubs;
  296. }
  297. /**
  298. * @param string $path
  299. * @param string $name
  300. * @return string
  301. * @author 等风来
  302. * @email 136327134@qq.com
  303. * @date 2023/3/13
  304. */
  305. protected function getFilePathName(string $path, string $name): string
  306. {
  307. $path = str_replace(['app\\', 'app/'], '', $path);
  308. $path = ltrim(str_replace('\\', '/', $path), '/');
  309. return $this->getBasePath($path) . $name . '.' . $this->fileMime;
  310. }
  311. }