|
1
|
|
|
<?php namespace Savannabits\JetstreamInertiaGenerator\Generators; |
|
2
|
|
|
|
|
3
|
|
|
use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
|
4
|
|
|
use Savannabits\JetstreamInertiaGenerator\Generators\Traits\FileManipulations; |
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
8
|
|
|
|
|
9
|
|
|
class ApiController extends ClassGenerator { |
|
10
|
|
|
|
|
11
|
|
|
use FileManipulations; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* The name and signature of the console command. |
|
15
|
|
|
* |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $name = 'jig:generate:api:controller'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The console command description. |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $description = 'Generate an API controller class'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Path for view |
|
29
|
|
|
* |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected string $view = 'api-controller'; |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Controller has also export method |
|
36
|
|
|
* |
|
37
|
|
|
* @return mixed |
|
38
|
|
|
*/ |
|
39
|
|
|
protected bool $export = false; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Controller has also bulk options method |
|
43
|
|
|
* |
|
44
|
|
|
* @return mixed |
|
45
|
|
|
*/ |
|
46
|
|
|
protected bool $withoutBulk = false; |
|
47
|
|
|
|
|
48
|
|
|
public function handle() |
|
49
|
|
|
{ |
|
50
|
|
|
$force = $this->option('force'); |
|
51
|
|
|
|
|
52
|
|
|
if($this->option('with-export')){ |
|
53
|
|
|
$this->export = true; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if($this->option('without-bulk')){ |
|
57
|
|
|
$this->withoutBulk = true; |
|
58
|
|
|
} |
|
59
|
|
|
// TODO test the case, if someone passes a class_name outside Laravel's default App\Http\Controllers folder, if it's going to work |
|
60
|
|
|
|
|
61
|
|
|
//TODO check if exists |
|
62
|
|
|
//TODO make global for all generator |
|
63
|
|
|
//TODO also with prefix |
|
64
|
|
|
if(!empty($template = $this->option('template'))) { |
|
65
|
|
|
$this->view = 'templates.'.$template.'.controller'; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if(!empty($belongsToMany = $this->option('belongs-to-many'))) { |
|
69
|
|
|
$this->setBelongToManyRelation($belongsToMany); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if ($this->generateClass($force)){ |
|
73
|
|
|
|
|
74
|
|
|
$this->info('Generating '.$this->classFullName.' finished'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
protected function buildClass():string { |
|
80
|
|
|
|
|
81
|
|
|
//Set belongsTo Relations |
|
82
|
|
|
$this->setBelongsToRelations(); |
|
83
|
|
|
return view('jig::'.$this->view, [ |
|
84
|
|
|
'controllerBaseName' => $this->classBaseName, |
|
85
|
|
|
'controllerNamespace' => $this->classNamespace, |
|
86
|
|
|
'repoBaseName' => $this->getRepositoryBaseName(), |
|
87
|
|
|
"repoFullName" => $this->getRepoNamespace($this->rootNamespace()).'\\'.$this->getRepositoryBaseName(), |
|
88
|
|
|
'modelBaseName' => $this->modelBaseName, |
|
89
|
|
|
'modelFullName' => $this->modelFullName, |
|
90
|
|
|
'modelPlural' => $this->modelPlural, |
|
91
|
|
|
'modelTitle' => $this->titleSingular, |
|
92
|
|
|
'modelVariableName' => $this->modelVariableName, |
|
93
|
|
|
'modelVariableNamePlural' => Str::plural($this->modelVariableName), |
|
94
|
|
|
'modelRouteAndViewName' => $this->modelRouteAndViewName, |
|
95
|
|
|
'modelViewsDirectory' => $this->modelViewsDirectory, |
|
96
|
|
|
'modelDotNotation' => $this->modelDotNotation, |
|
97
|
|
|
'modelWithNamespaceFromDefault' => $this->modelWithNamespaceFromDefault, |
|
98
|
|
|
'export' => $this->export, |
|
99
|
|
|
'withoutBulk' => $this->withoutBulk, |
|
100
|
|
|
'exportBaseName' => $this->exportBaseName, |
|
101
|
|
|
'resource' => $this->resource, |
|
102
|
|
|
'containsPublishedAtColumn' => in_array("published_at", array_column($this->readColumnsFromTable($this->tableName)->toArray(), 'name')), |
|
103
|
|
|
// index |
|
104
|
|
|
'columnsToQuery' => $this->readColumnsFromTable($this->tableName)->filter(function($column) { |
|
105
|
|
|
return !($column['type'] == 'text' || $column['name'] == "password" || $column['name'] == "remember_token" || $column['name'] == "slug" || $column['name'] == "created_at" || $column['name'] == "deleted_at"||Str::contains($column['name'],"_id")); |
|
106
|
|
|
})->pluck('name')->toArray(), |
|
107
|
|
|
'columnsToSearchIn' => $this->readColumnsFromTable($this->tableName)->filter(function($column) { |
|
108
|
|
|
return ($column['type'] == 'json' || $column['type'] == 'text' || $column['type'] == 'string' || $column['name'] == "id") && !($column['name'] == "password" || $column['name'] == "remember_token"); |
|
109
|
|
|
})->pluck('name')->toArray(), |
|
110
|
|
|
// 'filters' => $this->readColumnsFromTable($tableName)->filter(function($column) { |
|
111
|
|
|
// return $column['type'] == 'boolean' || $column['type'] == 'date'; |
|
112
|
|
|
// }), |
|
113
|
|
|
// validation in store/update |
|
114
|
|
|
'columns' => $this->getVisibleColumns($this->tableName, $this->modelVariableName), |
|
115
|
|
|
'relations' => $this->relations, |
|
116
|
|
|
'hasSoftDelete' => $this->readColumnsFromTable($this->tableName)->filter(function($column) { |
|
117
|
|
|
return $column['name'] == "deleted_at"; |
|
118
|
|
|
})->count() > 0, |
|
119
|
|
|
])->render(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
protected function getOptions() { |
|
123
|
|
|
return [ |
|
124
|
|
|
['model-name', 'm', InputOption::VALUE_OPTIONAL, 'Generates a code for the given model'], |
|
125
|
|
|
['template', 't', InputOption::VALUE_OPTIONAL, 'Specify custom template'], |
|
126
|
|
|
['belongs-to-many', 'btm', InputOption::VALUE_OPTIONAL, 'Specify belongs to many relations'], |
|
127
|
|
|
['force', 'f', InputOption::VALUE_NONE, 'Force will delete files before regenerating controller'], |
|
128
|
|
|
['model-with-full-namespace', 'fnm', InputOption::VALUE_OPTIONAL, 'Specify model with full namespace'], |
|
129
|
|
|
['with-export', 'e', InputOption::VALUE_NONE, 'Generate an option to Export as Excel'], |
|
130
|
|
|
['without-bulk', 'wb', InputOption::VALUE_NONE, 'Generate without bulk options'], |
|
131
|
|
|
]; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function generateClassNameFromTable($tableName): string |
|
135
|
|
|
{ |
|
136
|
|
|
return Str::studly(Str::singular($tableName)).'Controller'; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Get the default namespace for the class. |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $rootNamespace |
|
143
|
|
|
* @return string |
|
144
|
|
|
*/ |
|
145
|
|
|
protected function getDefaultNamespace(string $rootNamespace): string |
|
146
|
|
|
{ |
|
147
|
|
|
return $rootNamespace.'\Http\Controllers\API'; |
|
148
|
|
|
} |
|
149
|
|
|
protected function getRepoNamespace($rootNamespace): string |
|
150
|
|
|
{ |
|
151
|
|
|
return $rootNamespace.'Repositories'; |
|
152
|
|
|
} |
|
153
|
|
|
protected function getRepositoryBaseName(): string |
|
154
|
|
|
{ |
|
155
|
|
|
return Str::studly(Str::plural($this->tableName)); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|