1 | <?php |
||
2 | |||
3 | namespace PWWEB\Artomator\Generators\Scaffold; |
||
4 | |||
5 | use Illuminate\Support\Arr; |
||
6 | use Illuminate\Support\Str; |
||
7 | use InfyOm\Generator\Generators\BaseGenerator; |
||
8 | use InfyOm\Generator\Utils\FileUtil; |
||
9 | use PWWEB\Artomator\Common\CommandData; |
||
10 | use PWWEB\Artomator\Utils\VueFieldGenerator; |
||
11 | |||
12 | class VueGenerator extends BaseGenerator |
||
13 | { |
||
14 | /** |
||
15 | * Command data. |
||
16 | * |
||
17 | * @var CommandData |
||
18 | */ |
||
19 | private $commandData; |
||
20 | |||
21 | /** |
||
22 | * Path. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | private $path; |
||
27 | |||
28 | /** |
||
29 | * Template type. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | private $templateType; |
||
34 | |||
35 | /** |
||
36 | * Html Fields. |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | private $htmlFields; |
||
41 | |||
42 | /** |
||
43 | * Construct function. |
||
44 | * |
||
45 | * @param CommandData $commandData Command Data. |
||
46 | * |
||
47 | * @return void |
||
48 | */ |
||
49 | public function __construct(CommandData $commandData) |
||
50 | { |
||
51 | $this->commandData = $commandData; |
||
52 | $this->path = $commandData->config->pathVues; |
||
53 | $this->templateType = config('infyom.laravel_generator.templates', 'adminlte-templates'); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Generate Function. |
||
58 | * |
||
59 | * @return void |
||
60 | */ |
||
61 | public function generate() |
||
62 | { |
||
63 | if (false === file_exists($this->path)) { |
||
64 | mkdir($this->path, 0755, true); |
||
65 | } |
||
66 | |||
67 | $htmlInputs = Arr::pluck($this->commandData->fields, 'htmlInput'); |
||
68 | if (true === in_array('file', $htmlInputs)) { |
||
69 | $this->commandData->addDynamicVariable('$FILES$', ' enctype="multipart/form-data"'); |
||
70 | } |
||
71 | |||
72 | $this->commandData->commandComment("\nGenerating Vues..."); |
||
73 | |||
74 | if (true === $this->commandData->getOption('views')) { |
||
75 | $vuesToBeGenerated = explode(',', $this->commandData->getOption('views')); |
||
76 | |||
77 | if (true === in_array('index', $vuesToBeGenerated)) { |
||
78 | $this->generateIndex(); |
||
79 | } |
||
80 | |||
81 | if (count(array_intersect(['create', 'update'], $vuesToBeGenerated)) > 0) { |
||
82 | $this->generateFields(); |
||
83 | } |
||
84 | |||
85 | if (true === in_array('create', $vuesToBeGenerated)) { |
||
86 | $this->generateCreate(); |
||
87 | } |
||
88 | |||
89 | if (true === in_array('edit', $vuesToBeGenerated)) { |
||
90 | $this->generateUpdate(); |
||
91 | } |
||
92 | |||
93 | if (true === in_array('show', $vuesToBeGenerated)) { |
||
94 | $this->generateShowFields(); |
||
95 | $this->generateShow(); |
||
96 | } |
||
97 | } else { |
||
98 | $this->generateIndex(); |
||
99 | $this->generateFields(); |
||
100 | $this->generateCreate(); |
||
101 | $this->generateUpdate(); |
||
102 | $this->generateShowFields(); |
||
103 | $this->generateShow(); |
||
104 | } |
||
105 | |||
106 | $this->commandData->commandComment('Vues created: '); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Generate Table. |
||
111 | * |
||
112 | * @return void |
||
113 | */ |
||
114 | private function generateTable() |
||
115 | { |
||
116 | if (true === $this->commandData->getAddOn('datatables')) { |
||
117 | $templateData = $this->generateDataTableBody(); |
||
118 | $this->generateDataTableActions(); |
||
119 | } else { |
||
120 | $templateData = $this->generateBladeTableBody(); |
||
121 | } |
||
122 | |||
123 | return $templateData; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Generate Data Table Body. |
||
128 | * |
||
129 | * @return void |
||
130 | */ |
||
131 | private function generateDataTableBody() |
||
132 | { |
||
133 | $templateData = get_artomator_template('scaffold.views.datatable_body'); |
||
134 | |||
135 | return fill_template($this->commandData->dynamicVars, $templateData); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Generate Data Table Actions. |
||
140 | * |
||
141 | * @return void |
||
142 | */ |
||
143 | private function generateDataTableActions() |
||
144 | { |
||
145 | $templateName = 'datatables_actions'; |
||
146 | |||
147 | if (true === $this->commandData->isLocalizedTemplates()) { |
||
148 | $templateName .= '_locale'; |
||
149 | } |
||
150 | |||
151 | $templateData = get_artomator_template('scaffold.views.'.$templateName); |
||
152 | |||
153 | $templateData = fill_template($this->commandData->dynamicVars, $templateData); |
||
154 | |||
155 | FileUtil::createFile($this->path, 'datatables_actions.blade.php', $templateData); |
||
156 | |||
157 | $this->commandData->commandInfo('datatables_actions.blade.php created'); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Generate Blade Table Body. |
||
162 | * |
||
163 | * @return void |
||
164 | */ |
||
165 | private function generateBladeTableBody() |
||
166 | { |
||
167 | $templateName = 'blade_table_body'; |
||
168 | |||
169 | if (true === $this->commandData->isLocalizedTemplates()) { |
||
170 | $templateName .= '_locale'; |
||
171 | } |
||
172 | |||
173 | $templateData = get_artomator_template('scaffold.vues.'.$templateName); |
||
174 | |||
175 | $templateData = fill_template($this->commandData->dynamicVars, $templateData); |
||
176 | |||
177 | $templateData = str_replace('$FIELD_HEADERS$', $this->generateTableHeaderFields(), $templateData); |
||
178 | |||
179 | $cellFieldTemplate = get_artomator_template('scaffold.vues.table_cell'); |
||
180 | |||
181 | $tableBodyFields = []; |
||
182 | |||
183 | foreach ($this->commandData->fields as $field) { |
||
184 | if (false === $field->inIndex) { |
||
185 | continue; |
||
186 | } |
||
187 | |||
188 | $tableBodyFields[] = fill_template_with_field_data( |
||
189 | $this->commandData->dynamicVars, |
||
190 | $this->commandData->fieldNamesMapping, |
||
191 | $cellFieldTemplate, |
||
192 | $field |
||
193 | ); |
||
194 | } |
||
195 | |||
196 | $tableBodyFields = implode(infy_nl_tab(1, 3), $tableBodyFields); |
||
197 | |||
198 | return str_replace('$FIELD_BODY$', $tableBodyFields, $templateData); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Generate Table Header Fields. |
||
203 | * |
||
204 | * @return void |
||
205 | */ |
||
206 | private function generateTableHeaderFields() |
||
207 | { |
||
208 | $templateName = 'table_header'; |
||
209 | |||
210 | $localized = false; |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
211 | if (true === $this->commandData->isLocalizedTemplates()) { |
||
212 | $templateName .= '_locale'; |
||
213 | $localized = true; |
||
214 | } |
||
215 | |||
216 | $headerFieldTemplate = get_artomator_template('scaffold.vues.'.$templateName); |
||
217 | |||
218 | $headerFields = []; |
||
219 | |||
220 | foreach ($this->commandData->fields as $field) { |
||
221 | if (false === $field->inIndex) { |
||
222 | continue; |
||
223 | } |
||
224 | $singleFieldStr = str_replace( |
||
225 | '$FIELD_NAME_TITLE$', |
||
226 | Str::title(str_replace('_', ' ', $field->name)), |
||
227 | $headerFieldTemplate |
||
228 | ); |
||
229 | $singleFieldStr = str_replace('$FIELD_NAME$', $field->name, $singleFieldStr); |
||
230 | $singleFieldStr = fill_template($this->commandData->dynamicVars, $singleFieldStr); |
||
231 | |||
232 | $headerFields[] = $singleFieldStr; |
||
233 | } |
||
234 | |||
235 | return implode(infy_nl_tab(1, 2), $headerFields); |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * Generate Index. |
||
240 | * |
||
241 | * @return void |
||
242 | */ |
||
243 | private function generateIndex() |
||
244 | { |
||
245 | $templateName = 'index'; |
||
246 | |||
247 | if (true === $this->commandData->isLocalizedTemplates()) { |
||
248 | $templateName .= '_locale'; |
||
249 | } |
||
250 | |||
251 | $templateData = get_artomator_template('scaffold.vues.'.$templateName); |
||
252 | |||
253 | $templateData = fill_template($this->commandData->dynamicVars, $templateData); |
||
254 | |||
255 | $templateData = str_replace('$TABLE$', $this->generateTable(), $templateData); |
||
256 | |||
257 | FileUtil::createFile($this->path, 'Index.vue', $templateData); |
||
258 | |||
259 | $this->commandData->commandInfo('Index.vue created'); |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Generate Fields. |
||
264 | * |
||
265 | * @return void |
||
266 | */ |
||
267 | private function generateFields() |
||
268 | { |
||
269 | $templateName = 'fields'; |
||
270 | |||
271 | $localized = false; |
||
272 | if (true === $this->commandData->isLocalizedTemplates()) { |
||
273 | $templateName .= '_locale'; |
||
274 | $localized = true; |
||
275 | } |
||
276 | |||
277 | $this->htmlFields = []; |
||
278 | $createForm = []; |
||
279 | $editForm = []; |
||
280 | |||
281 | foreach ($this->commandData->fields as $field) { |
||
282 | if (false === $field->inForm) { |
||
283 | continue; |
||
284 | } |
||
285 | |||
286 | $validations = explode('|', $field->validations); |
||
287 | $minMaxRules = ''; |
||
288 | $required = ''; |
||
289 | foreach ($validations as $validation) { |
||
290 | if (false === Str::contains($validation, ['max:', 'min:'])) { |
||
291 | continue; |
||
292 | } |
||
293 | |||
294 | $validationText = substr($validation, 0, 3); |
||
295 | $sizeInNumber = substr($validation, 4); |
||
296 | |||
297 | $sizeText = ('min' === $validationText) ? 'minlength' : 'maxlength'; |
||
298 | if ('number' === $field->htmlType) { |
||
299 | $sizeText = $validationText; |
||
300 | } |
||
301 | |||
302 | if (true === Str::contains($validation, 'required')) { |
||
303 | $required = ' required'; |
||
304 | } |
||
305 | |||
306 | $size = " $sizeText=\"$sizeInNumber\""; |
||
307 | $minMaxRules .= $size; |
||
308 | } |
||
309 | |||
310 | $this->commandData->addDynamicVariable('$SIZE$', $minMaxRules); |
||
311 | |||
312 | $this->commandData->addDynamicVariable('$REQUIRED$', $required); |
||
313 | |||
314 | $fieldTemplate = VueFieldGenerator::generateHTML($field, $this->templateType, $localized); |
||
315 | |||
316 | if ('selectTable' === $field->htmlType) { |
||
317 | $inputArr = explode(',', $field->htmlValues[1]); |
||
318 | $columns = ''; |
||
319 | foreach ($inputArr as $item) { |
||
320 | $columns .= "'$item'".','; |
||
321 | //e.g 'email,id,' |
||
322 | } |
||
323 | $columns = substr_replace($columns, '', -1); |
||
324 | // remove last , |
||
325 | |||
326 | $htmlValues = explode(',', $field->htmlValues[0]); |
||
327 | $selectTable = $htmlValues[0]; |
||
328 | $modalName = null; |
||
329 | if (2 === count($htmlValues)) { |
||
330 | $modalName = $htmlValues[1]; |
||
331 | } |
||
332 | |||
333 | $tableName = $this->commandData->config->tableName; |
||
334 | $vuePath = $this->commandData->config->prefixes['vue']; |
||
335 | if (false === empty($vuePath)) { |
||
336 | $tableName = $vuePath.'.'.$tableName; |
||
337 | } |
||
338 | |||
339 | $variableName = Str::singular($selectTable).'Items'; |
||
340 | // e.g $userItems |
||
341 | |||
342 | $fieldTemplate = $this->generateVueComposer($tableName, $variableName, $columns, $selectTable, $modalName); |
||
343 | } |
||
344 | |||
345 | if (false === empty($fieldTemplate)) { |
||
346 | $fieldTemplate = fill_template_with_field_data( |
||
347 | $this->commandData->dynamicVars, |
||
348 | $this->commandData->fieldNamesMapping, |
||
349 | $fieldTemplate, |
||
350 | $field |
||
351 | ); |
||
352 | $this->htmlFields[] = $fieldTemplate; |
||
353 | $createForm[] = $field->name.': null,'; |
||
354 | $editForm[] = $field->name.': props.$MODEL_NAME_CAMEL$.'.$field->name.','; |
||
355 | } |
||
356 | } |
||
357 | |||
358 | $this->commandData->addDynamicVariable('$CREATE_DATA$', implode("\n", $createForm)); |
||
359 | $this->commandData->addDynamicVariable('$EDIT_DATA$', implode("\n", $editForm)); |
||
360 | |||
361 | $templateData = get_artomator_template('scaffold.vues.'.$templateName); |
||
362 | $templateData = fill_template($this->commandData->dynamicVars, $templateData); |
||
363 | |||
364 | $templateData = str_replace('$FIELDS$', implode("\n\n", $this->htmlFields), $templateData); |
||
365 | |||
366 | FileUtil::createFile($this->path, 'Fields.vue', $templateData); |
||
367 | $this->commandData->commandInfo('Field.vue created'); |
||
368 | } |
||
369 | |||
370 | /** |
||
371 | * Generate Vue Composer. |
||
372 | * |
||
373 | * @param string $tableName Table Name. |
||
374 | * @param string $variableName Variable Name. |
||
375 | * @param array $columns Columns. |
||
376 | * @param string $selectTable Select Table. |
||
377 | * @param string|null $modelName Model Name. |
||
378 | * |
||
379 | * @return void |
||
380 | */ |
||
381 | private function generateVueComposer($tableName, $variableName, $columns, $selectTable, $modelName = null) |
||
382 | { |
||
383 | $templateName = 'scaffold.fields.select'; |
||
384 | if (true === $this->commandData->isLocalizedTemplates()) { |
||
385 | $templateName .= '_locale'; |
||
386 | } |
||
387 | $fieldTemplate = get_artomator_template($templateName); |
||
388 | |||
389 | $fieldTemplate = str_replace( |
||
390 | '$INPUT_ARR$', |
||
391 | $variableName, |
||
392 | $fieldTemplate |
||
393 | ); |
||
394 | |||
395 | return $fieldTemplate; |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * Generate Create. |
||
400 | * |
||
401 | * @return void |
||
402 | */ |
||
403 | private function generateCreate() |
||
404 | { |
||
405 | $templateName = 'create'; |
||
406 | |||
407 | if (true === $this->commandData->isLocalizedTemplates()) { |
||
408 | $templateName .= '_locale'; |
||
409 | } |
||
410 | |||
411 | $templateData = get_artomator_template('scaffold.vues.'.$templateName); |
||
412 | |||
413 | $templateData = fill_template($this->commandData->dynamicVars, $templateData); |
||
414 | $templateData = fill_template($this->commandData->dynamicVars, $templateData); |
||
415 | |||
416 | FileUtil::createFile($this->path, 'Create.vue', $templateData); |
||
417 | $this->commandData->commandInfo('Create.vue created'); |
||
418 | } |
||
419 | |||
420 | /** |
||
421 | * Generate Update. |
||
422 | * |
||
423 | * @return void |
||
424 | */ |
||
425 | private function generateUpdate() |
||
426 | { |
||
427 | $templateName = 'edit'; |
||
428 | |||
429 | if (true === $this->commandData->isLocalizedTemplates()) { |
||
430 | $templateName .= '_locale'; |
||
431 | } |
||
432 | |||
433 | $templateData = get_artomator_template('scaffold.vues.'.$templateName); |
||
434 | |||
435 | $templateData = fill_template($this->commandData->dynamicVars, $templateData); |
||
436 | $templateData = fill_template($this->commandData->dynamicVars, $templateData); |
||
437 | |||
438 | FileUtil::createFile($this->path, 'Edit.vue', $templateData); |
||
439 | $this->commandData->commandInfo('Edit.vue created'); |
||
440 | } |
||
441 | |||
442 | /** |
||
443 | * Generate Show Fields. |
||
444 | * |
||
445 | * @return void |
||
446 | */ |
||
447 | private function generateShowFields() |
||
448 | { |
||
449 | $templateName = 'show_field'; |
||
450 | if (true === $this->commandData->isLocalizedTemplates()) { |
||
451 | $templateName .= '_locale'; |
||
452 | } |
||
453 | $fieldTemplate = get_artomator_template('scaffold.vues.'.$templateName); |
||
454 | |||
455 | $fieldsStr = ''; |
||
456 | |||
457 | foreach ($this->commandData->fields as $field) { |
||
458 | if (false === $field->inView) { |
||
459 | continue; |
||
460 | } |
||
461 | $singleFieldStr = str_replace( |
||
462 | '$FIELD_NAME_TITLE$', |
||
463 | Str::title(str_replace('_', ' ', $field->name)), |
||
464 | $fieldTemplate |
||
465 | ); |
||
466 | $singleFieldStr = str_replace('$FIELD_NAME$', $field->name, $singleFieldStr); |
||
467 | $singleFieldStr = fill_template($this->commandData->dynamicVars, $singleFieldStr); |
||
468 | |||
469 | $fieldsStr .= $singleFieldStr."\n\n"; |
||
470 | } |
||
471 | $templateName = 'show_fields'; |
||
472 | $fieldTemplate = get_artomator_template('scaffold.vues.'.$templateName); |
||
473 | |||
474 | $fieldTemplate = str_replace('$FIELDS$', $fieldsStr, $fieldTemplate); |
||
475 | $fieldTemplate = fill_template($this->commandData->dynamicVars, $fieldTemplate); |
||
476 | |||
477 | FileUtil::createFile($this->path, 'ShowFields.vue', $fieldTemplate); |
||
478 | $this->commandData->commandInfo('ShowFields.vue created'); |
||
479 | } |
||
480 | |||
481 | /** |
||
482 | * Generate Show. |
||
483 | * |
||
484 | * @return void |
||
485 | */ |
||
486 | private function generateShow() |
||
487 | { |
||
488 | $templateName = 'show'; |
||
489 | |||
490 | if (true === $this->commandData->isLocalizedTemplates()) { |
||
491 | $templateName .= '_locale'; |
||
492 | } |
||
493 | |||
494 | $templateData = get_artomator_template('scaffold.vues.'.$templateName); |
||
495 | |||
496 | $templateData = fill_template($this->commandData->dynamicVars, $templateData); |
||
497 | |||
498 | FileUtil::createFile($this->path, 'Show.vue', $templateData); |
||
499 | $this->commandData->commandInfo('Show.vue created'); |
||
500 | } |
||
501 | |||
502 | /** |
||
503 | * Rollback Function. |
||
504 | * |
||
505 | * @param array $vues Vue views to rollback. |
||
506 | * |
||
507 | * @return void |
||
508 | */ |
||
509 | public function rollback($vues = []) |
||
510 | { |
||
511 | $files = [ |
||
512 | 'Table.vue', |
||
513 | 'Index.vue', |
||
514 | 'Fields.vue', |
||
515 | 'Create.vue', |
||
516 | 'Edit.vue', |
||
517 | 'Show.vue', |
||
518 | 'ShowFields.vue', |
||
519 | ]; |
||
520 | |||
521 | if (false === empty($vues)) { |
||
522 | $files = []; |
||
523 | foreach ($vues as $vue) { |
||
524 | $files[] = $vue.'.vue'; |
||
525 | } |
||
526 | } |
||
527 | |||
528 | if (true === $this->commandData->getAddOn('datatables')) { |
||
529 | $files[] = 'datatables_actions.blade.php'; |
||
530 | } |
||
531 | |||
532 | foreach ($files as $file) { |
||
533 | if (true === $this->rollbackFile($this->path, $file)) { |
||
534 | $this->commandData->commandComment($file.' file deleted'); |
||
535 | } |
||
536 | } |
||
537 | } |
||
538 | } |
||
539 |