Controller.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. namespace catcher\generate\template;
  3. use catcher\base\CatchController;
  4. class Controller
  5. {
  6. use Content;
  7. /**
  8. * use
  9. *
  10. * @time 2020年04月27日
  11. * @return string
  12. */
  13. public function uses()
  14. {
  15. return <<<TMP
  16. use catcher\base\CatchRequest as Request;
  17. use catcher\CatchResponse;
  18. use catcher\base\CatchController;
  19. {USE}
  20. TMP;
  21. }
  22. /**
  23. * construct
  24. *
  25. * @time 2020年04月27日
  26. * @param $model
  27. * @return string
  28. */
  29. public function construct($model)
  30. {
  31. return <<<TMP
  32. protected \$model;
  33. public function __construct({$model} \$model)
  34. {
  35. \$this->model = \$model;
  36. }
  37. TMP;
  38. }
  39. public function createClass($class)
  40. {
  41. return <<<TMP
  42. class {$class} extends CatchController
  43. {
  44. {CONTENT}
  45. }
  46. TMP;
  47. }
  48. /**
  49. * list template
  50. *
  51. * @time 2020年04月24日
  52. * @return string
  53. */
  54. public function index()
  55. {
  56. return <<<TMP
  57. {$this->controllerFunctionComment('列表', '')}
  58. public function index()
  59. {
  60. return CatchResponse::paginate(\$this->model->getList());
  61. }
  62. TMP;
  63. }
  64. /**
  65. * create template
  66. *
  67. * @time 2020年04月24日
  68. * @return string
  69. */
  70. public function create()
  71. {
  72. return <<<TMP
  73. {$this->controllerFunctionComment('单页')}
  74. public function create()
  75. {
  76. //
  77. }
  78. TMP;
  79. }
  80. /**
  81. * save template
  82. *
  83. * @time 2020年04月24日
  84. * @param $createRequest
  85. * @return string
  86. */
  87. public function save($createRequest = '')
  88. {
  89. $request = $createRequest ? 'CreateRequest' : 'Request';
  90. return <<<TMP
  91. {$this->controllerFunctionComment('保存', 'Request ' . $request)}
  92. public function save({$request} \$request)
  93. {
  94. return CatchResponse::success(\$this->model->storeBy(\$request->post()));
  95. }
  96. TMP;
  97. }
  98. /**
  99. * read template
  100. *
  101. * @time 2020年04月24日
  102. * @return string
  103. */
  104. public function read()
  105. {
  106. return <<<TMP
  107. {$this->controllerFunctionComment('读取', '$id')}
  108. public function read(\$id)
  109. {
  110. return CatchResponse::success(\$this->model->findBy(\$id));
  111. }
  112. TMP;
  113. }
  114. /**
  115. * edit template
  116. *
  117. * @time 2020年04月24日
  118. * @return string
  119. */
  120. public function edit()
  121. {
  122. return <<<TMP
  123. {$this->controllerFunctionComment('编辑', '\$id')}
  124. public function edit(\$id)
  125. {
  126. //
  127. }
  128. TMP;
  129. }
  130. /**
  131. * update template
  132. *
  133. * @time 2020年04月24日
  134. * @param $updateRequest
  135. * @return string
  136. */
  137. public function update($updateRequest = '')
  138. {
  139. $updateRequest = ($updateRequest ? 'UpdateRequest' : 'Request') . ' $request';
  140. return <<<TMP
  141. {$this->controllerFunctionComment('更新', $updateRequest)}
  142. public function update({$updateRequest}, \$id)
  143. {
  144. return CatchResponse::success(\$this->model->updateBy(\$id, \$request->post()));
  145. }
  146. TMP;
  147. }
  148. /**
  149. * delete template
  150. *
  151. * @time 2020年04月24日
  152. * @return string
  153. */
  154. public function delete()
  155. {
  156. return <<<TMP
  157. {$this->controllerFunctionComment('删除', '$id')}
  158. public function delete(\$id)
  159. {
  160. return CatchResponse::success(\$this->model->deleteBy(\$id));
  161. }
  162. TMP;
  163. }
  164. /**
  165. * 其他方法
  166. *
  167. * @time 2020年04月27日
  168. * @param $function
  169. * @param string $method
  170. * @return string
  171. */
  172. public function otherFunction($function, $method = 'get')
  173. {
  174. $params = $method === 'delete' ? '$id' : 'Request $request';
  175. return <<<TMP
  176. {$this->controllerFunctionComment('', $params)}
  177. public function {$function}({$params})
  178. {
  179. // todo
  180. }
  181. TMP;
  182. }
  183. /**
  184. * 控制器方法注释
  185. *
  186. * @time 2020年04月24日
  187. * @param $des
  188. * @param $params
  189. * @return string
  190. */
  191. protected function controllerFunctionComment($des, $params = '')
  192. {
  193. $now = date('Y/m/d H:i', time());
  194. $params = $params ? '@param ' . $params : '';
  195. return <<<TMP
  196. /**
  197. * {$des}
  198. *
  199. * @time {$now}
  200. * {$params}
  201. * @return \\think\\Response
  202. */
  203. TMP;
  204. }
  205. }