FrontForm.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 FrontForm 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 array
  56. */
  57. protected array $structures;
  58. /**
  59. * @param string $controller
  60. */
  61. public function __construct(protected readonly string $controller)
  62. {
  63. }
  64. /**
  65. * get content
  66. *
  67. * @return string
  68. */
  69. public function getContent(): string
  70. {
  71. // TODO: Implement getContent() method.
  72. return Str::of(File::get($this->getFormStub()))->replace($this->formItems, $this->getFormContent())->toString();
  73. }
  74. /**
  75. * get file
  76. *
  77. * @return string
  78. */
  79. public function getFile(): string
  80. {
  81. // TODO: Implement getFile() method.
  82. return CatchAdmin::makeDir(CatchAdmin::getModuleViewsPath($this->module).Str::of($this->controller)->replace('Controller', '')->lcfirst()).DIRECTORY_SEPARATOR.'create.vue';
  83. }
  84. /**
  85. * get form content
  86. *
  87. * @return string
  88. */
  89. protected function getFormContent(): string
  90. {
  91. $form = Str::of('');
  92. $formComponents = $this->formComponents();
  93. foreach ($this->structures as $structure) {
  94. if ($structure['label'] && $structure['form_component'] && $structure['form']) {
  95. if (isset($formComponents[$structure['form_component']])) {
  96. $form = $form->append(
  97. Str::of($formComponents[$structure['form_component']])
  98. ->replace(
  99. [$this->label, $this->prop, $this->modelValue],
  100. [$structure['label'], $structure['field'], sprintf('formData.%s', $structure['field'])]
  101. )
  102. );
  103. }
  104. }
  105. }
  106. return $form->trim(PHP_EOL)->toString();
  107. }
  108. /**
  109. * form components
  110. *
  111. * @return array
  112. */
  113. protected function formComponents(): array
  114. {
  115. $components = [];
  116. foreach (File::glob(
  117. $this->getFormItemStub()
  118. ) as $stub) {
  119. $components[File::name($stub)] = File::get($stub);
  120. }
  121. return $components;
  122. }
  123. /**
  124. * get formItem stub
  125. *
  126. * @return string
  127. */
  128. protected function getFormItemStub(): string
  129. {
  130. return dirname(__DIR__).DIRECTORY_SEPARATOR.'stubs'
  131. .DIRECTORY_SEPARATOR.'vue'.DIRECTORY_SEPARATOR
  132. .'formItems'.DIRECTORY_SEPARATOR.'*.stub';
  133. }
  134. /**
  135. * get form stub
  136. *
  137. * @return string
  138. */
  139. public function getFormStub(): string
  140. {
  141. return dirname(__DIR__).DIRECTORY_SEPARATOR.'stubs'
  142. .DIRECTORY_SEPARATOR.'vue'.DIRECTORY_SEPARATOR.'form.stub';
  143. }
  144. /**
  145. * set structures
  146. *
  147. * @param array $structures
  148. * @return $this
  149. */
  150. public function setStructures(array $structures): static
  151. {
  152. $this->structures = $structures;
  153. return $this;
  154. }
  155. }