1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pratiksh\Adminetic\Services; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\DB; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Pratiksh\Adminetic\Services\Helper\CommandHelper; |
8
|
|
|
|
9
|
|
|
class MakeAPIResource extends CommandHelper |
10
|
|
|
{ |
11
|
|
|
public static function makeRestAPI($name, $path, $version = 'v1') |
12
|
|
|
{ |
13
|
|
|
// Making API Resource Controller |
14
|
|
|
self::makeRestAPIController($name, $path, $version); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public static function makeClientAPI($name, $path, $version = 'v1') |
18
|
|
|
{ |
19
|
|
|
self::makeClientAPIResource($name, $path, $version); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public static function makeAPI($name, $path, $version = 'v1') |
23
|
|
|
{ |
24
|
|
|
// Making Resource and Collection |
25
|
|
|
self::makeAPIResource($name, $path, $version); |
26
|
|
|
// Making Resource API Controller |
27
|
|
|
self::makeClientAPIResource($name, $path, $version); |
28
|
|
|
// Making Client API Controller |
29
|
|
|
self::makeRestAPIController($name, $path, $version); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Make API Resourceful Controller. |
34
|
|
|
*/ |
35
|
|
|
protected static function makeRestAPIController($name, $path, $version) |
36
|
|
|
{ |
37
|
|
|
$version = ucfirst($version); |
38
|
|
|
$version_lc = strtolower($version); |
39
|
|
|
$swagger_json_content = self::generateSwaggerJsonContentContent($name)['content']; |
40
|
|
|
$required_fields = self::generateSwaggerJsonContentContent($name)['required_fields']; |
41
|
|
|
if (! file_exists($dir_path = app_path('Http/Controllers/Api/'.trim($version).'/Restful'))) { |
42
|
|
|
mkdir($dir_path, 0777, true); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// Making API Resource |
46
|
|
|
self::makeAPIResource($name, $path, $version); |
47
|
|
|
|
48
|
|
|
$controllerTemplate = str_replace( |
49
|
|
|
[ |
50
|
|
|
'{{version}}', |
51
|
|
|
'{{version_lc}}', |
52
|
|
|
'{{modelName}}', |
53
|
|
|
'{{modelPath}}', |
54
|
|
|
'{{modelNamePluralLowerCase}}', |
55
|
|
|
'{{modelNameSingularLowerCase}}', |
56
|
|
|
'{{swagger_json_content}}', |
57
|
|
|
'{{required_fields}}', |
58
|
|
|
], |
59
|
|
|
[ |
60
|
|
|
$version, |
61
|
|
|
$version_lc, |
62
|
|
|
$name, |
63
|
|
|
$path, |
64
|
|
|
strtolower(Str::plural($name)), |
65
|
|
|
strtolower($name), |
66
|
|
|
$swagger_json_content, |
67
|
|
|
$required_fields, |
68
|
|
|
], |
69
|
|
|
self::getStub('Api/Restful/RestAPIController') |
70
|
|
|
); |
71
|
|
|
file_put_contents(app_path('/Http/Controllers/Api/'.trim($version)."/Restful/{$name}RestAPIController.php"), $controllerTemplate); |
72
|
|
|
|
73
|
|
|
$route = strtolower($name); |
74
|
|
|
file_put_contents('routes/api.php', "\n", FILE_APPEND | LOCK_EX); |
75
|
|
|
file_put_contents('routes/api.php', "Route::get('{$route}/all',[{$name}RestAPIController::class,'all'])->name('{$route}.all'); \n", FILE_APPEND | LOCK_EX); |
76
|
|
|
file_put_contents('routes/api.php', "Route::post('{$route}/get',[{$name}RestAPIController::class,'get'])->name('{$route}.get'); \n", FILE_APPEND | LOCK_EX); |
77
|
|
|
file_put_contents('routes/api.php', "Route::apiResource('{$route}',{$name}RestAPIController::class); \n", FILE_APPEND | LOCK_EX); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Make Client API Resource. |
82
|
|
|
*/ |
83
|
|
|
protected static function makeClientAPIResource($name, $path, $version) |
84
|
|
|
{ |
85
|
|
|
$version = ucfirst($version); |
86
|
|
|
$version_lc = strtolower($version); |
87
|
|
|
// Making API Resource |
88
|
|
|
self::makeAPIResource($name, $path, $version); |
89
|
|
|
// Making Controller |
90
|
|
|
if (! file_exists($dir_path = app_path('Http/Controllers/Api/'.trim($version).'/Client'))) { |
91
|
|
|
mkdir($dir_path, 0777, true); |
92
|
|
|
} |
93
|
|
|
$controllerTemplate = str_replace( |
94
|
|
|
[ |
95
|
|
|
'{{version}}', |
96
|
|
|
'{{version_lc}}', |
97
|
|
|
'{{modelName}}', |
98
|
|
|
'{{modelPath}}', |
99
|
|
|
'{{modelNamePluralLowerCase}}', |
100
|
|
|
'{{modelNameSingularLowerCase}}', |
101
|
|
|
], |
102
|
|
|
[ |
103
|
|
|
$version, |
104
|
|
|
$version_lc, |
105
|
|
|
$name, |
106
|
|
|
$path, |
107
|
|
|
strtolower(Str::plural($name)), |
108
|
|
|
strtolower($name), |
109
|
|
|
], |
110
|
|
|
self::getStub('Api/Client/ClientAPIController') |
111
|
|
|
); |
112
|
|
|
file_put_contents(app_path('/Http/Controllers/Api/'.trim($version)."/Client/{$name}ClientAPIController.php"), $controllerTemplate); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
protected static function makeAPIResource($name, $path, $version) |
116
|
|
|
{ |
117
|
|
|
$version = ucfirst($version); |
118
|
|
|
$version_lc = strtolower($version); |
119
|
|
|
if (! file_exists($dir_path = app_path("/Http/Resources/{$version}/{$name}"))) { |
120
|
|
|
mkdir($dir_path, 0777, true); |
121
|
|
|
} |
122
|
|
|
// Making Collection |
123
|
|
|
$collectionTemplate = str_replace( |
124
|
|
|
[ |
125
|
|
|
'{{version}}', |
126
|
|
|
'{{version_lc}}', |
127
|
|
|
'{{modelName}}', |
128
|
|
|
'{{modelPath}}', |
129
|
|
|
'{{modelNamePluralLowerCase}}', |
130
|
|
|
'{{modelNameSingularLowerCase}}', |
131
|
|
|
], |
132
|
|
|
[ |
133
|
|
|
$version, |
134
|
|
|
$version_lc, |
135
|
|
|
$name, |
136
|
|
|
$path, |
137
|
|
|
strtolower(Str::plural($name)), |
138
|
|
|
strtolower($name), |
139
|
|
|
], |
140
|
|
|
self::getStub('Api/Collection') |
141
|
|
|
); |
142
|
|
|
file_put_contents(app_path("/Http/Resources/{$version}/{$name}/{$name}Collection.php"), $collectionTemplate); |
143
|
|
|
|
144
|
|
|
// Making Resource |
145
|
|
|
$resourceTemplate = str_replace( |
146
|
|
|
[ |
147
|
|
|
'{{version}}', |
148
|
|
|
'{{modelName}}', |
149
|
|
|
'{{modelPath}}', |
150
|
|
|
'{{modelNamePluralLowerCase}}', |
151
|
|
|
'{{modelNameSingularLowerCase}}', |
152
|
|
|
], |
153
|
|
|
[ |
154
|
|
|
$version, |
155
|
|
|
$name, |
156
|
|
|
$path, |
157
|
|
|
strtolower(Str::plural($name)), |
158
|
|
|
strtolower($name), |
159
|
|
|
], |
160
|
|
|
self::getStub('Api/Resource') |
161
|
|
|
); |
162
|
|
|
file_put_contents(app_path("/Http/Resources/{$version}/{$name}/{$name}Resource.php"), $resourceTemplate); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
private static function generateSwaggerJsonContentContent($name) |
166
|
|
|
{ |
167
|
|
|
$table = strtolower(Str::plural($name)); |
168
|
|
|
|
169
|
|
|
$columns = DB::select(DB::raw('SHOW COLUMNS FROM `'.$table.'`')); |
170
|
|
|
|
171
|
|
|
$fields = []; |
172
|
|
|
|
173
|
|
|
$required_fields = []; |
174
|
|
|
|
175
|
|
|
$swagger_json_content = []; |
176
|
|
|
|
177
|
|
|
foreach ($columns as $column) { |
178
|
|
|
if (! is_null($column)) { |
179
|
|
|
$null = $column->Null == 'No' ? 'true' : 'false'; |
180
|
|
|
$field_name = $column->Field; |
181
|
|
|
$field_type = $column->Type; |
182
|
|
|
$field_default = $column->Default; |
183
|
|
|
$example = DB::table($table)->count() > 0 ? DB::table($table)->first()->$field_name : ''; |
184
|
|
|
if (! in_array($field_name, ['created_at', 'updated_at']) && ! in_array($field_type, ['longtext'])) { |
185
|
|
|
if ($null != 'No') { |
186
|
|
|
$required_fields[] = '"'.$field_name.'"'; |
187
|
|
|
} |
188
|
|
|
$fields[] = [ |
189
|
|
|
'field_name' => $field_name, |
190
|
|
|
'field_type' => $field_type, |
191
|
|
|
'field_default' => $field_default, |
192
|
|
|
]; |
193
|
|
|
$swagger_json_content[] = '@OA\Property(property="'.$field_name.'",nullable='.$null.',type="'.$field_type.'",default="'.$field_default.'",example="'.$example.'")'; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return [ |
199
|
|
|
'required_fields' => implode(',', $required_fields), |
200
|
|
|
'content' => implode(',', $swagger_json_content), |
201
|
|
|
]; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|