1 | <?php namespace Savannabits\JetstreamInertiaGenerator\Generators; |
||
5 | class IndexRequest extends ClassGenerator { |
||
6 | |||
7 | /** |
||
8 | * The name and signature of the console command. |
||
9 | * |
||
10 | * @var string |
||
11 | */ |
||
12 | protected $name = 'jig:generate:request:index'; |
||
13 | |||
14 | /** |
||
15 | * The console command description. |
||
16 | * |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $description = 'Generate an Index request class'; |
||
20 | protected string $view = 'index-request'; |
||
|
|||
21 | |||
22 | /** |
||
23 | * Execute the console command. |
||
24 | * |
||
25 | * @return void |
||
26 | */ |
||
27 | public function handle() |
||
28 | { |
||
29 | $force = $this->option('force'); |
||
30 | |||
31 | if(!empty($template = $this->option('template'))) { |
||
32 | $this->view = 'templates.'.$template.'.index-request'; |
||
33 | } |
||
34 | |||
35 | if ($this->generateClass($force)){ |
||
36 | $this->info('Generating '.$this->classFullName.' finished'); |
||
37 | } |
||
38 | } |
||
39 | |||
40 | protected function buildClass() :string { |
||
41 | |||
42 | return view('jig::'.$this->view, [ |
||
43 | 'modelBaseName' => $this->modelBaseName, |
||
44 | 'modelDotNotation' => $this->modelDotNotation, |
||
45 | 'modelWithNamespaceFromDefault' => $this->modelWithNamespaceFromDefault, |
||
46 | 'modelVariableName' => $this->modelVariableName, |
||
47 | 'modelFullName' => $this->modelFullName, |
||
48 | 'columnsToQuery' => $this->readColumnsFromTable($this->tableName)->filter(function($column) { |
||
49 | return !($column['type'] == 'text' || $column['name'] == "password" || $column['name'] == "remember_token" || $column['name'] == "slug" || $column['name'] == "created_at" || $column['name'] == "updated_at" || $column['name'] == "deleted_at"); |
||
50 | })->pluck('name')->toArray(), |
||
51 | ])->render(); |
||
52 | } |
||
53 | |||
54 | protected function getOptions() { |
||
55 | return [ |
||
56 | ['template', 't', InputOption::VALUE_OPTIONAL, 'Specify custom template'], |
||
57 | ['model-name', 'm', InputOption::VALUE_OPTIONAL, 'Generates a code for the given model'], |
||
58 | ['force', 'f', InputOption::VALUE_NONE, 'Force will delete files before regenerating request'], |
||
59 | ]; |
||
60 | } |
||
61 | |||
62 | public function generateClassNameFromTable($tableName): string |
||
63 | { |
||
64 | return 'Index'.$this->modelBaseName; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Get the default namespace for the class. |
||
69 | * |
||
70 | * @param string $rootNamespace |
||
71 | * @return string |
||
72 | */ |
||
73 | protected function getDefaultNamespace(string $rootNamespace) :string |
||
79 |