FrontTable.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. declare(strict_types=1);
  12. namespace Modules\Develop\Support\Generate\Create;
  13. use Catch\CatchAdmin;
  14. use Illuminate\Support\Facades\File;
  15. use Illuminate\Support\Str;
  16. class FrontTable extends Creator
  17. {
  18. /**
  19. * @var string
  20. */
  21. protected string $label = '{label}';
  22. /**
  23. * @var string
  24. */
  25. protected string $prop = '{prop}';
  26. /**
  27. * @var string
  28. */
  29. protected string $modelValue = '{model-value}';
  30. /**
  31. * @var string
  32. */
  33. protected string $table = '{table}';
  34. /**
  35. * @var string
  36. */
  37. protected string $search = '{search}';
  38. /**
  39. * @var string
  40. */
  41. protected string $api = '{api}';
  42. /**
  43. * @var string
  44. */
  45. protected string $formItems = '{formItems}';
  46. /**
  47. * @var string
  48. */
  49. protected string $paginate = '{paginate}';
  50. /**
  51. * @var string
  52. */
  53. protected string $useList = '{useList}';
  54. /**
  55. * @var string
  56. */
  57. protected string $tree = '{tree}';
  58. /**
  59. * @var array
  60. */
  61. protected array $structures;
  62. /**
  63. * @param string $controller
  64. * @param bool $hasPaginate
  65. * @param string $apiString
  66. */
  67. public function __construct(
  68. protected readonly string $controller,
  69. protected readonly bool $hasPaginate,
  70. protected readonly string $apiString
  71. ) {
  72. }
  73. /**
  74. * get content
  75. *
  76. * @return string
  77. */
  78. public function getContent(): string
  79. {
  80. // TODO: Implement getContent() method.
  81. return Str::of(File::get($this->getTableStub()))->replace([
  82. $this->table, $this->search, $this->api, $this->paginate, $this->useList, $this->tree
  83. ], [
  84. $this->getTableContent(),
  85. $this->getSearchContent(),
  86. $this->apiString,
  87. $this->getPaginateStubContent(),
  88. $this->getUseList(),
  89. $this->getTreeProps()
  90. ])->toString();
  91. }
  92. /**
  93. * get file
  94. *
  95. * @return string
  96. */
  97. public function getFile(): string
  98. {
  99. // TODO: Implement getFile() method.
  100. return CatchAdmin::makeDir(CatchAdmin::getModuleViewsPath($this->module).Str::of($this->controller)->replace('Controller', '')->lcfirst()).DIRECTORY_SEPARATOR.'index.vue';
  101. }
  102. /**
  103. * get search content
  104. *
  105. * @return string
  106. */
  107. protected function getSearchContent(): string
  108. {
  109. $search = Str::of('');
  110. $formComponents = $this->formComponents();
  111. foreach ($this->structures as $structure) {
  112. if ($structure['label'] && $structure['form_component'] && $structure['search']) {
  113. if (isset($formComponents[$structure['form_component']])) {
  114. $search = $search->append(
  115. Str::of($formComponents[$structure['form_component']])
  116. ->replace(
  117. [$this->label, $this->prop, $this->modelValue],
  118. [$structure['label'], $structure['field'], sprintf('query.%s', $structure['field'])]
  119. )
  120. );
  121. }
  122. }
  123. }
  124. return $search->trim(PHP_EOL)->toString();
  125. }
  126. /**
  127. * get list content;
  128. *
  129. * @return string
  130. */
  131. protected function getTableContent(): string
  132. {
  133. $tableColumn = <<<HTML
  134. <el-table-column prop="{prop}" label="{label}" />
  135. HTML;
  136. $table = Str::of('');
  137. foreach ($this->structures as $structure) {
  138. if ($structure['field'] && $structure['label'] && $structure['list']) {
  139. $table = $table->append(
  140. Str::of($tableColumn)->replace([$this->label, $this->prop], [$structure['label'], $structure['field']])
  141. )->newLine();
  142. }
  143. }
  144. return $table->trim(PHP_EOL)->toString();
  145. }
  146. /**
  147. * form components
  148. *
  149. * @return array
  150. */
  151. protected function formComponents(): array
  152. {
  153. $components = [];
  154. foreach (File::glob(
  155. $this->getFormItemStub()
  156. ) as $stub) {
  157. $components[File::name($stub)] = File::get($stub);
  158. }
  159. return $components;
  160. }
  161. /**
  162. * get formItem stub
  163. *
  164. * @return string
  165. */
  166. protected function getFormItemStub(): string
  167. {
  168. return dirname(__DIR__).DIRECTORY_SEPARATOR.'stubs'
  169. .DIRECTORY_SEPARATOR.'vue'.DIRECTORY_SEPARATOR
  170. .'formItems'.DIRECTORY_SEPARATOR.'*.stub';
  171. }
  172. /**
  173. * get table stub
  174. *
  175. * @return string
  176. */
  177. protected function getTableStub(): string
  178. {
  179. return dirname(__DIR__).DIRECTORY_SEPARATOR.'stubs'
  180. .DIRECTORY_SEPARATOR.'vue'.DIRECTORY_SEPARATOR.'table.stub';
  181. }
  182. /**
  183. * get paginate stub content
  184. *
  185. * @return string
  186. */
  187. protected function getPaginateStubContent(): string
  188. {
  189. return $this->hasPaginate ? '<Paginate />' : '';
  190. }
  191. /**
  192. * get use List
  193. * @return string
  194. */
  195. protected function getUseList(): string
  196. {
  197. if ($this->hasPaginate) {
  198. return 'const { data, query, search, reset, loading } = useGetList(api)';
  199. } else {
  200. return 'const { data, query, search, reset, loading } = useGetList(api, false)';
  201. }
  202. }
  203. /**
  204. * get tree props
  205. *
  206. * @return string
  207. */
  208. public function getTreeProps(): string
  209. {
  210. if (in_array('parent_id', array_column($this->structures, 'field'))) {
  211. return ' row-key="id" default-expand-all :tree-props="{ children: \'children\' }"';
  212. }
  213. return ' ';
  214. }
  215. /**
  216. * set structures
  217. *
  218. * @param array $structures
  219. * @return $this
  220. */
  221. public function setStructures(array $structures): static
  222. {
  223. $this->structures = $structures;
  224. return $this;
  225. }
  226. }