123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
- declare(strict_types=1);
- namespace Modules\Develop\Support\Generate\Create;
- use Catch\CatchAdmin;
- use Illuminate\Support\Facades\File;
- use Illuminate\Support\Str;
- class FrontForm extends Creator
- {
-
- protected string $label = '{label}';
-
- protected string $prop = '{prop}';
-
- protected string $modelValue = '{model-value}';
-
- protected string $table = '{table}';
-
- protected string $search = '{search}';
-
- protected string $api = '{api}';
-
- protected string $formItems = '{formItems}';
-
- protected string $paginate = '{paginate}';
-
- protected string $useList = '{useList}';
-
- protected array $structures;
-
- public function __construct(protected readonly string $controller)
- {
- }
-
- public function getContent(): string
- {
-
- return Str::of(File::get($this->getFormStub()))->replace($this->formItems, $this->getFormContent())->toString();
- }
-
- public function getFile(): string
- {
-
- return CatchAdmin::makeDir(CatchAdmin::getModuleViewsPath($this->module).Str::of($this->controller)->replace('Controller', '')->lcfirst()).DIRECTORY_SEPARATOR.'create.vue';
- }
-
- protected function getFormContent(): string
- {
- $form = Str::of('');
- $formComponents = $this->formComponents();
- foreach ($this->structures as $structure) {
- if ($structure['label'] && $structure['form_component'] && $structure['form']) {
- if (isset($formComponents[$structure['form_component']])) {
- $form = $form->append(
- Str::of($formComponents[$structure['form_component']])
- ->replace(
- [$this->label, $this->prop, $this->modelValue],
- [$structure['label'], $structure['field'], sprintf('formData.%s', $structure['field'])]
- )
- );
- }
- }
- }
- return $form->trim(PHP_EOL)->toString();
- }
-
- protected function formComponents(): array
- {
- $components = [];
- foreach (File::glob(
- $this->getFormItemStub()
- ) as $stub) {
- $components[File::name($stub)] = File::get($stub);
- }
- return $components;
- }
-
- protected function getFormItemStub(): string
- {
- return dirname(__DIR__).DIRECTORY_SEPARATOR.'stubs'
- .DIRECTORY_SEPARATOR.'vue'.DIRECTORY_SEPARATOR
- .'formItems'.DIRECTORY_SEPARATOR.'*.stub';
- }
-
- public function getFormStub(): string
- {
- return dirname(__DIR__).DIRECTORY_SEPARATOR.'stubs'
- .DIRECTORY_SEPARATOR.'vue'.DIRECTORY_SEPARATOR.'form.stub';
- }
-
- public function setStructures(array $structures): static
- {
- $this->structures = $structures;
- return $this;
- }
- }
|