Controller.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace catcher\generate\factory;
  3. use catcher\CatchAdmin;
  4. use catcher\exceptions\FailedException;
  5. use catcher\facade\FileSystem;
  6. use catcher\generate\build\classes\Methods;
  7. use catcher\generate\build\CatchBuild;
  8. use catcher\generate\build\classes\Classes;
  9. use catcher\generate\build\classes\Property;
  10. use catcher\generate\build\classes\Uses;
  11. use PhpParser\BuilderFactory;
  12. use PhpParser\Node\Expr\Closure;
  13. use PhpParser\Node\Expr\ClosureUse;
  14. use PhpParser\PrettyPrinter\Standard;
  15. use think\helper\Str;
  16. use PhpParser\Error;
  17. use PhpParser\NodeDumper;
  18. use PhpParser\ParserFactory;
  19. use PhpParser\PrettyPrinter;
  20. use PhpParser\Node;
  21. class Controller extends Factory
  22. {
  23. protected $methods = [];
  24. protected $uses = [
  25. 'catcher\base\CatchRequest as Request',
  26. 'catcher\CatchResponse',
  27. 'catcher\base\CatchController'
  28. ];
  29. /**
  30. *
  31. * @time 2020年04月27日
  32. * @param $params
  33. * @return bool|string|string[]
  34. */
  35. public function done($params)
  36. {
  37. // 写入成功之后
  38. $controllerPath = $this->getGeneratePath($params['controller']);
  39. if (FileSystem::put($controllerPath, $this->getContent($params))) {
  40. return $controllerPath;
  41. }
  42. throw new FailedException($params['controller'] . ' generate failed~');
  43. }
  44. /**
  45. * 获取内容
  46. *
  47. * @time 2020年04月28日
  48. * @param $params
  49. * @return bool|string|string[]
  50. */
  51. public function getContent($params)
  52. {
  53. if (!$params['controller']) {
  54. throw new FailedException('params has lost~');
  55. }
  56. // parse controller
  57. [$className, $namespace] = $this->parseFilename($params['controller']);
  58. [$model, $modelNamespace] = $this->parseFilename($params['model']);
  59. $asModel = lcfirst(Str::contains($model, 'Model') ? : $model . 'Model');
  60. if (!$className) {
  61. throw new FailedException('未填写控制器名称');
  62. }
  63. $use = new Uses();
  64. $class = new Classes($className);
  65. return (new CatchBuild())->namespace($namespace)
  66. ->use($use->name('catcher\base\CatchRequest', 'Request'))
  67. ->use($use->name('catcher\CatchResponse'))
  68. ->use($use->name('catcher\base\CatchController'))
  69. ->use($use->name($modelNamespace . '\\' . ucfirst($model), $asModel))
  70. ->class($class->extend('CatchController')->docComment(), function (Classes $class) use ($asModel) {
  71. foreach ($this->getMethods($asModel) as $method) {
  72. $class->addMethod($method);
  73. }
  74. $class->addProperty(
  75. (new Property($asModel))->protected()
  76. );
  77. })
  78. ->getContent();
  79. }
  80. /**
  81. * 方法集合
  82. *
  83. * @time 2020年11月19日
  84. * @param $model
  85. * @return array
  86. */
  87. protected function getMethods($model)
  88. {
  89. $date = date('Y年m月d日 H:i');
  90. return [
  91. (new Methods('__construct'))
  92. ->public()
  93. ->param($model, ucfirst($model))
  94. ->docComment("\r\n")
  95. ->declare($model, $model),
  96. (new Methods('index'))->public()
  97. ->param('request', 'Request')
  98. ->docComment(
  99. <<<TEXT
  100. /**
  101. * 列表
  102. * @time $date
  103. * @param Request \$request
  104. */
  105. TEXT
  106. )
  107. ->returnType('\think\Response')->index($model),
  108. (new Methods('save'))
  109. ->public()
  110. ->param('request', 'Request')
  111. ->docComment(
  112. <<<TEXT
  113. /**
  114. * 保存信息
  115. * @time $date
  116. * @param Request \$request
  117. */
  118. TEXT
  119. )
  120. ->returnType('\think\Response')
  121. ->save($model),
  122. (new Methods('read'))->public()
  123. ->param('id')
  124. ->docComment(
  125. <<<TEXT
  126. /**
  127. * 读取
  128. * @time $date
  129. * @param \$id
  130. */
  131. TEXT
  132. )
  133. ->returnType('\think\Response')->read($model),
  134. (new Methods('update'))->public()
  135. ->param('request', 'Request')
  136. ->param('id')
  137. ->docComment(
  138. <<<TEXT
  139. /**
  140. * 更新
  141. * @time $date
  142. * @param Request \$request
  143. * @param \$id
  144. */
  145. TEXT
  146. )
  147. ->returnType('\think\Response')->update($model),
  148. (new Methods('delete'))->public()
  149. ->param('id')
  150. ->docComment(
  151. <<<TEXT
  152. /**
  153. * 删除
  154. * @time $date
  155. * @param \$id
  156. */
  157. TEXT
  158. )
  159. ->returnType('\think\Response')->delete($model),
  160. ];
  161. }
  162. }