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