|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EasyPanel\Parsers; |
|
4
|
|
|
|
|
5
|
|
|
use EasyPanel\Parsers\HTMLInputs\BaseInput; |
|
6
|
|
|
use Illuminate\Support\Facades\File; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
|
|
9
|
|
|
class StubParser |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
public $texts = []; |
|
13
|
|
|
private $inputName; |
|
14
|
|
|
private $parsedModel; |
|
15
|
|
|
|
|
16
|
|
|
private $fields; |
|
17
|
|
|
private $inputs; |
|
18
|
|
|
private $validationRules; |
|
19
|
|
|
private $hasAuth; |
|
20
|
|
|
private $store; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct($inputName, $parsedModel) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->inputName = $inputName; |
|
25
|
|
|
$this->parsedModel = $parsedModel; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function setValidationRules($rules) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->validationRules = $rules; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function setStore($store) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->store = $store; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function setAuthType(bool $hasAuth){ |
|
39
|
|
|
$this->hasAuth = $hasAuth; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function setFields(array $fields){ |
|
43
|
|
|
$this->fields = $fields; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function setInputs(array $inputs){ |
|
47
|
|
|
$this->inputs = $inputs; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function replaceModel($stub) |
|
51
|
|
|
{ |
|
52
|
|
|
$modelNamespace = $this->parsedModel; |
|
53
|
|
|
$modelName = $this->getModelName($modelNamespace); |
|
54
|
|
|
|
|
55
|
|
|
$array = [ |
|
56
|
|
|
'{{ modelName }}' => ucfirst($modelName), |
|
57
|
|
|
'{{ modelNamespace }}' => $modelNamespace, |
|
58
|
|
|
'{{ uploadFile }}' => $this->uploadCodeParser(), |
|
59
|
|
|
'{{ model }}' => strtolower($modelName), |
|
60
|
|
|
'{{ properties }}' => $this->parseProperties(), |
|
61
|
|
|
'{{ rules }}' => $this->parseValidationRules(), |
|
62
|
|
|
'{{ fields }}' => $this->parseActionInComponent(), |
|
63
|
|
|
'{{ setProperties }}' => $this->parseSetPropertiesValue(), |
|
64
|
|
|
]; |
|
65
|
|
|
|
|
66
|
|
|
return str_replace(array_keys($array), array_values($array), $stub); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Make Locale files |
|
71
|
|
|
*/ |
|
72
|
|
|
public function setLocaleTexts() |
|
73
|
|
|
{ |
|
74
|
|
|
$this->texts[ucfirst($this->inputName)] = ucfirst($this->inputName); |
|
75
|
|
|
$this->texts[ucfirst(Str::plural($this->inputName))] = ucfirst(Str::plural($this->inputName)); |
|
76
|
|
|
$files = File::glob(resource_path('lang').'/*_panel.json'); |
|
77
|
|
|
|
|
78
|
|
|
foreach ($files as $file) { |
|
79
|
|
|
$decodedFile = json_decode(File::get($file), 1); |
|
80
|
|
|
$texts = $this->texts; |
|
81
|
|
|
foreach ($texts as $key => $text) { |
|
82
|
|
|
if (array_key_exists($key, $decodedFile)){ |
|
83
|
|
|
unset($texts[$text]); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
$array = array_merge($decodedFile, $texts); |
|
87
|
|
|
File::put($file, json_encode($array, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get model name from namespace |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getModelName($modelNamespace) |
|
95
|
|
|
{ |
|
96
|
|
|
$array = explode('\\', $modelNamespace); |
|
97
|
|
|
|
|
98
|
|
|
return end($array); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Parse properties in Livewire component |
|
103
|
|
|
*/ |
|
104
|
|
|
public function parseProperties() |
|
105
|
|
|
{ |
|
106
|
|
|
$fields = array_keys($this->inputs); |
|
107
|
|
|
$str = ''; |
|
108
|
|
|
|
|
109
|
|
|
if(in_array($this->inputName, $fields)){ |
|
110
|
|
|
$this->error("Model name must not equal to column names, fix it and rerun command with -f flag"); |
|
|
|
|
|
|
111
|
|
|
die; |
|
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
foreach ($fields as $field) { |
|
115
|
|
|
$str .= 'public $'.$field.";".$this->makeTab(1); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $str; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Parse Uploading Code |
|
123
|
|
|
*/ |
|
124
|
|
|
public function uploadCodeParser() |
|
125
|
|
|
{ |
|
126
|
|
|
$filesInput = array_keys($this->inputs, 'file'); |
|
127
|
|
|
$str = ''; |
|
128
|
|
|
foreach ($filesInput as $file) { |
|
129
|
|
|
// We get store path which has been defined in crud's config file |
|
130
|
|
|
$storePath = $this->store[$file] ?? "{$file}"; |
|
131
|
|
|
|
|
132
|
|
|
// We get property value then store file in $storePath |
|
133
|
|
|
// A PHP Code for upload as a string will be created here |
|
134
|
|
|
$str .= $this->makeTab(2).'if($this->getPropertyValue(\''.$file.'\') and is_object($this->'.$file.')) {'.$this->makeTab(3); |
|
135
|
|
|
$str .= '$this->'.$file.' = $this->getPropertyValue(\''.$file.'\')->store(\''.$storePath.'\');'.$this->makeTab(2); |
|
136
|
|
|
$str .= '}'.PHP_EOL; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return $str; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* parse values for mount method in Livewire |
|
144
|
|
|
*/ |
|
145
|
|
|
public function parseSetPropertiesValue() |
|
146
|
|
|
{ |
|
147
|
|
|
$fields = array_keys($this->inputs); |
|
148
|
|
|
$str = ''; |
|
149
|
|
|
$action = $this->inputName; |
|
150
|
|
|
foreach ($fields as $field) { |
|
151
|
|
|
$str .= '$this->'.$field.' = $this->'.$action.'->'.$field.';'.$this->makeTab(2, end($fields) != $field); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
return $str; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Parse Validation rules |
|
159
|
|
|
*/ |
|
160
|
|
|
public function parseValidationRules() |
|
161
|
|
|
{ |
|
162
|
|
|
$str = ''; |
|
163
|
|
|
|
|
164
|
|
|
foreach ($this->validationRules as $key => $rule) { |
|
165
|
|
|
$str .= "'$key' => '$rule',".$this->makeTab(2, $rule != end($this->validationRules)); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
return $str; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Create an array of properties in Livewire component for actions |
|
173
|
|
|
*/ |
|
174
|
|
|
public function parseActionInComponent() |
|
175
|
|
|
{ |
|
176
|
|
|
$str = ''; |
|
177
|
|
|
|
|
178
|
|
|
foreach ($this->inputs as $key => $field) { |
|
179
|
|
|
$newLine = ($field != end($this->inputs) or $this->hasAuth); |
|
180
|
|
|
$str .= "'$key' => " . '$this' . "->$key,".$this->makeTab(3, $newLine); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
if($this->hasAuth){ |
|
184
|
|
|
$str .= "'user_id' => auth()->id(),"; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
return $str; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Create Blade from stub |
|
192
|
|
|
*/ |
|
193
|
|
|
public function parseBlade($stub){ |
|
194
|
|
|
$modelName = $this->getModelName($this->parsedModel); |
|
195
|
|
|
|
|
196
|
|
|
$array = [ |
|
197
|
|
|
'{{ model }}' => strtolower($modelName), |
|
198
|
|
|
'{{ modelName }}' => ucfirst($modelName), |
|
199
|
|
|
'{{ data }}' => $this->parseDataInBlade(), |
|
200
|
|
|
'{{ titles }}' => $this->parseTitlesInBlade(), |
|
201
|
|
|
'{{ inputs }}' => $this->parseInputsInBlade(), |
|
202
|
|
|
'{{ routeName }}' => crud(strtolower($modelName))->route, |
|
203
|
|
|
]; |
|
204
|
|
|
|
|
205
|
|
|
$this->setLocaleTexts(); |
|
206
|
|
|
|
|
207
|
|
|
return str_replace(array_keys($array), array_values($array), $stub); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Parse <td> tags for data |
|
212
|
|
|
*/ |
|
213
|
|
|
public function parseDataInBlade() |
|
214
|
|
|
{ |
|
215
|
|
|
$fields = $this->fields; |
|
216
|
|
|
$str = ''; |
|
217
|
|
|
$modelName = strtolower($this->getModelName($this->parsedModel)); |
|
218
|
|
|
foreach ($fields as $value) { |
|
219
|
|
|
if (!Str::contains($value, '.')) { |
|
220
|
|
|
if(!in_array($value, ['image', 'photo', 'profile', 'banner'])) { |
|
221
|
|
|
$str .= '<td> {{ $' . $modelName . '->' . $value . " }} </td>" . $this->makeTab(1, end($fields) != $value); |
|
222
|
|
|
} else { |
|
223
|
|
|
$str .= '<td><a target="_blank" href="{{ asset($' . $modelName . '->' . $value . ') }}"><img class="rounded-circle img-fluid" width="50" height="50" src="{{ asset($' . $modelName . '->' . $value . ') }}" alt="'.$value.'"></a></td>' . $this->makeTab(1, end($fields) != $value); |
|
224
|
|
|
} |
|
225
|
|
|
} else { |
|
226
|
|
|
$newValue = $this->parseDots($value); |
|
227
|
|
|
$str .= '<td> {{ $' . $modelName . '->' . $newValue .' }} </td>' . $this->makeTab(1, end($fields) != $value); |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
return $str; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Parse <td> tags for head |
|
236
|
|
|
*/ |
|
237
|
|
|
public function parseTitlesInBlade() |
|
238
|
|
|
{ |
|
239
|
|
|
$fields = $this->fields; |
|
240
|
|
|
$str = ''; |
|
241
|
|
|
foreach ($fields as $field) { |
|
242
|
|
|
if (!Str::contains($field, '.')) { |
|
243
|
|
|
$sortName = $field; |
|
244
|
|
|
$field = ucfirst($field); |
|
245
|
|
|
$str .= "<td style='cursor: pointer' wire:click=\"sort('$sortName')\"> <i class='fa @if(".'$sortType'." == 'desc' and ".'$sortColumn'." == '$sortName') fa-sort-amount-down ml-2 @elseif(".'$sortType == '."'asc' and ".'$sortColumn'." == '$sortName') fa-sort-amount-up ml-2 @endif'></i> {{ __('$field') }} </td>".$this->makeTab(6, end($fields) != $field); |
|
246
|
|
|
} else { |
|
247
|
|
|
$fieldName = $this->parseFieldNameWithDots($field); |
|
248
|
|
|
$str .= "<td> {{ __('$fieldName') }} </td>".$this->makeTab(6, end($fields) != $field); |
|
249
|
|
|
} |
|
250
|
|
|
$this->texts[$field] = $field; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
return $str; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Create inputs HTML |
|
258
|
|
|
*/ |
|
259
|
|
|
public function parseInputsInBlade() |
|
260
|
|
|
{ |
|
261
|
|
|
$str = ''; |
|
262
|
|
|
foreach ($this->inputs as $name => $type) { |
|
263
|
|
|
$title = ucfirst($name); |
|
264
|
|
|
$this->texts[$title] = ucfirst($name); |
|
265
|
|
|
$str .= (new BaseInput($name, $type, $this->inputName))->render(); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
return $str; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Tab Maker (Each tabs mean 4 space) |
|
273
|
|
|
*/ |
|
274
|
|
|
public function makeTab($count, $newLine = true){ |
|
275
|
|
|
$count = $count * 4; |
|
276
|
|
|
$tabs = str_repeat(' ', $count); |
|
277
|
|
|
|
|
278
|
|
|
return $newLine ? "\n".$tabs : $tabs; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
public function parseDots($string) |
|
282
|
|
|
{ |
|
283
|
|
|
return str_replace('.', '->', $string); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
public function parseFieldNameWithDots($field) |
|
287
|
|
|
{ |
|
288
|
|
|
$array = explode('.', $field); |
|
289
|
|
|
$str = ''; |
|
290
|
|
|
foreach ($array as $value){ |
|
291
|
|
|
$str .= ucfirst($value)." "; |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
return trim($str); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
} |
|
298
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.