1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace crocodicstudio\crudbooster\Modules\ApiGeneratorModule; |
4
|
|
|
|
5
|
|
|
use crocodicstudio\crudbooster\controllers\CBController; |
6
|
|
|
use crocodicstudio\crudbooster\helpers\CRUDBooster; |
7
|
|
|
|
8
|
|
|
class AdminApiGeneratorController extends CBController |
9
|
|
|
{ |
10
|
|
|
public function cbInit() |
11
|
|
|
{ |
12
|
|
|
$this->table = 'cms_apicustom'; |
13
|
|
|
$this->primaryKey = "id"; |
14
|
|
|
$this->titleField = "nama"; |
15
|
|
|
$this->buttonShow = false; |
16
|
|
|
$this->deleteBtn = false; |
17
|
|
|
$this->buttonAdd = false; |
18
|
|
|
$this->button_import = false; |
19
|
|
|
$this->buttonExport = false; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function getIndex() |
23
|
|
|
{ |
24
|
|
|
$this->cbLoader(); |
25
|
|
|
|
26
|
|
|
$data = []; |
27
|
|
|
|
28
|
|
|
$data['page_title'] = 'API Generator'; |
29
|
|
|
$data['apis'] = $this->table()->orderby('nama', 'asc')->get(); |
30
|
|
|
|
31
|
|
|
return view('CbApiGen::api_documentation', $data); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function apiDocumentation() |
35
|
|
|
{ |
36
|
|
|
$this->cbLoader(); |
37
|
|
|
$apis = $this->table()->orderby('nama', 'asc')->get(); |
38
|
|
|
return view('CbApiGen::api_documentation_public', compact('apis')); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getGenerator() |
42
|
|
|
{ |
43
|
|
|
$this->cbLoader(); |
44
|
|
|
$data = [ |
45
|
|
|
'page_title' => 'API Generator', |
46
|
|
|
'tables' => CRUDBooster::listCbTables(), |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
return view('CbApiGen::api_generator', $data); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getEditApi($id) |
53
|
|
|
{ |
54
|
|
|
$this->cbLoader(); |
55
|
|
|
|
56
|
|
|
$row = $this->findRow($id)->first(); |
57
|
|
|
$data = [ |
58
|
|
|
'row' => $row, |
59
|
|
|
'parameters' => json_encode(unserialize($row->parameters)), |
60
|
|
|
'responses' => json_encode(unserialize($row->responses)), |
61
|
|
|
'page_title' => 'API Generator', |
62
|
|
|
'tables' => CRUDBooster::listCbTables(), |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
return view('CbApiGen::api_generator', $data); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function postSaveApiCustom() |
69
|
|
|
{ |
70
|
|
|
$this->cbLoader(); |
71
|
|
|
$posts = request()->all(); |
72
|
|
|
|
73
|
|
|
$_data = [ |
74
|
|
|
'nama' => g('nama'), |
75
|
|
|
'tabel' => $posts['tabel'], |
76
|
|
|
'aksi' => $posts['aksi'], |
77
|
|
|
'permalink' => g('permalink'), |
78
|
|
|
'method_type' => g('method_type'), |
79
|
|
|
'sql_where' => g('sql_where'), |
80
|
|
|
'keterangan' => g('keterangan'), |
81
|
|
|
'parameters' => serialize(array_filter($this->json())), |
82
|
|
|
'responses' => serialize(array_filter($this->json2())), |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
$this->saveToDB($_data); |
86
|
|
|
|
87
|
|
|
return redirect(CRUDBooster::mainpath())->with(['message' => 'Yeay, your api has been saved successfully !', 'message_type' => 'success']); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getDeleteApi($id) |
91
|
|
|
{ |
92
|
|
|
$this->cbLoader(); |
93
|
|
|
$row = $this->findRow($id)->first(); |
94
|
|
|
$this->findRow($id)->delete(); |
95
|
|
|
|
96
|
|
|
$controllername = ucwords(str_replace('_', ' ', $row->permalink)); |
97
|
|
|
$controllername = str_replace(' ', '', $controllername); |
98
|
|
|
@unlink((controllers_dir()."Api".$controllername."Controller.php")); |
|
|
|
|
99
|
|
|
|
100
|
|
|
return response()->json(['status' => 1]); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
private function json() |
107
|
|
|
{ |
108
|
|
|
$params_name = g('params_name'); |
109
|
|
|
$params_type = g('params_type'); |
110
|
|
|
$params_config = g('params_config'); |
111
|
|
|
$params_required = g('params_required'); |
112
|
|
|
$params_used = g('params_used'); |
113
|
|
|
|
114
|
|
|
$json = []; |
115
|
|
|
for ($i = 0, $_count = count($params_name); $i <= $_count; $i++) { |
116
|
|
|
if (! $params_name[$i]) { |
117
|
|
|
continue; |
118
|
|
|
} |
119
|
|
|
$json[] = [ |
120
|
|
|
'name' => $params_name[$i], |
121
|
|
|
'type' => $params_type[$i], |
122
|
|
|
'config' => $params_config[$i], |
123
|
|
|
'required' => $params_required[$i], |
124
|
|
|
'used' => $params_used[$i], |
125
|
|
|
]; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $json; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
private function json2() |
135
|
|
|
{ |
136
|
|
|
$responses_name = g('responses_name'); |
137
|
|
|
$responses_type = g('responses_type'); |
138
|
|
|
$responses_subquery = g('responses_subquery'); |
139
|
|
|
$responses_used = g('responses_used'); |
140
|
|
|
|
141
|
|
|
$json = []; |
142
|
|
|
for ($i = 0, $_count = count($responses_name); $i <= $_count; $i++) { |
143
|
|
|
if (! $responses_name[$i]) { |
144
|
|
|
continue; |
145
|
|
|
} |
146
|
|
|
$json[] = [ |
147
|
|
|
'name' => $responses_name[$i], |
148
|
|
|
'type' => $responses_type[$i], |
149
|
|
|
'subquery' => $responses_subquery[$i], |
150
|
|
|
'used' => $responses_used[$i], |
151
|
|
|
]; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $json; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param $a |
159
|
|
|
*/ |
160
|
|
|
private function saveToDB($a) |
161
|
|
|
{ |
162
|
|
|
if (request('id')) { |
163
|
|
|
return $this->findRow(g('id'))->update($a); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$controllerName = ucwords(str_replace('_', ' ', $a['permalink'])); |
167
|
|
|
$controllerName = str_replace(' ', '', $controllerName); |
168
|
|
|
$this->generateAPI($controllerName, $a['tabel'], $a['permalink'], $a['method_type']); |
169
|
|
|
|
170
|
|
|
return $this->table()->insert($a); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
private function generateAPI($controller_name, $table_name, $permalink, $method_type = 'post') |
174
|
|
|
{ |
175
|
|
|
$php = '<?php '.view('CbApiGen::api_stub', compact('controller_name', 'table_name', 'permalink', 'method_type'))->render(); |
176
|
|
|
file_put_contents(controllers_dir().'Api'.$controller_name.'Controller.php', $php); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: