Generator.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CatchAdmin [Just Like ~ ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://catchadmin.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( https://github.com/JaguarJack/catchadmin-laravel/blob/master/LICENSE.md )
  8. // +----------------------------------------------------------------------
  9. // | Author: JaguarJack [ njphper@gmail.com ]
  10. // +----------------------------------------------------------------------
  11. namespace Modules\Develop\Support\Generate;
  12. use Catch\Exceptions\FailedException;
  13. use Exception;
  14. use Illuminate\Contracts\Filesystem\FileNotFoundException;
  15. use Modules\Develop\Support\Generate\Create\Controller;
  16. use Modules\Develop\Support\Generate\Create\FrontForm;
  17. use Modules\Develop\Support\Generate\Create\FrontTable;
  18. use Modules\Develop\Support\Generate\Create\Model;
  19. use Modules\Develop\Support\Generate\Create\Request;
  20. use Modules\Develop\Support\Generate\Create\Route;
  21. /**
  22. * @class Generator
  23. */
  24. class Generator
  25. {
  26. /**
  27. * @var array{module:string,controller:string,model:string,paginate: bool,schema: string}
  28. */
  29. protected array $gen;
  30. /**
  31. * @var array{name: string,charset: string, collection: string,
  32. * comment:string,created_at: bool, updated_at: bool, deleted_at: bool,
  33. * creator_id: bool, updated_at: bool, engine: string}
  34. */
  35. protected array $schema;
  36. /**
  37. * @var array
  38. */
  39. protected array $structures;
  40. /**
  41. * @var array
  42. */
  43. protected array $files = [];
  44. /**
  45. * this model name from controller
  46. *
  47. * @var string
  48. */
  49. protected string $modelName;
  50. /**
  51. * this request name for controller
  52. *
  53. * @var ?string
  54. */
  55. protected ?string $requestName;
  56. /**
  57. * generate
  58. *
  59. * @throws Exception
  60. * @return bool
  61. */
  62. public function generate(): bool
  63. {
  64. try {
  65. $this->files[] = $this->createModel();
  66. $this->files[] = $this->createRequest();
  67. $this->files[] = $this->createController();
  68. $this->files[] = $this->createFrontTable();
  69. $this->files[] = $this->createFrontForm();
  70. $this->files[] = $this->createRoute();
  71. } catch (Exception $e) {
  72. $this->rollback();
  73. throw new FailedException($e->getMessage());
  74. }
  75. $this->files = [];
  76. return true;
  77. }
  78. /**
  79. * create route
  80. *
  81. * @throws FileNotFoundException
  82. * @return bool|string
  83. */
  84. public function createRoute(): bool|string
  85. {
  86. // 保存之前的 route 文件
  87. $route = new Route($this->gen['controller']);
  88. return $route->setModule($this->gen['module'])->create();
  89. }
  90. /**
  91. * create font
  92. *
  93. * @throws FileNotFoundException
  94. * @return bool|string|null
  95. */
  96. public function createFrontTable(): bool|string|null
  97. {
  98. $table = new FrontTable($this->gen['controller'], $this->gen['paginate'], (new Route($this->gen['controller']))->setModule($this->gen['module'])->getApiRute());
  99. return $table->setModule($this->gen['module'])->setStructures($this->structures)->create();
  100. }
  101. /**
  102. * create font
  103. *
  104. * @throws FileNotFoundException
  105. * @return bool|string|null
  106. */
  107. public function createFrontForm(): bool|string|null
  108. {
  109. $form = new FrontForm($this->gen['controller']);
  110. return $form->setModule($this->gen['module'])->setStructures($this->structures)->create();
  111. }
  112. /**
  113. * create model
  114. *
  115. * @throws FileNotFoundException
  116. * @return bool|string
  117. */
  118. protected function createModel(): bool|string
  119. {
  120. $model = new Model($this->gen['model'], $this->gen['schema'], $this->gen['module']);
  121. $this->modelName = $model->getModelName();
  122. return $model->setModule($this->gen['module'])->setStructures($this->structures)->create();
  123. }
  124. /**
  125. * create request
  126. *
  127. * @throws FileNotFoundException
  128. * @return bool|string
  129. */
  130. protected function createRequest(): bool|string
  131. {
  132. $request = new Request($this->gen['controller']);
  133. $file = $request->setStructures($this->structures)->setModule($this->gen['module'])->create();
  134. $this->requestName = $request->getRequestName();
  135. return $file;
  136. }
  137. /**
  138. * create controller
  139. *
  140. * @throws FileNotFoundException
  141. * @return bool|string
  142. */
  143. protected function createController(): bool|string
  144. {
  145. $controller = new Controller($this->gen['controller'], $this->modelName, $this->requestName);
  146. return $controller->setModule($this->gen['module'])->create();
  147. }
  148. /**
  149. * rollback
  150. *
  151. * @return void
  152. */
  153. protected function rollback(): void
  154. {
  155. // delete controller & model & migration file
  156. foreach ($this->files as $file) {
  157. unlink($file);
  158. }
  159. // 回填之前的 route 文件
  160. }
  161. /**
  162. * set params
  163. *
  164. * @param array $params
  165. * @return $this
  166. */
  167. public function setParams(array $params): Generator
  168. {
  169. $this->gen = $params['codeGen'];
  170. $this->structures = $params['structures'];
  171. return $this;
  172. }
  173. }