Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php namespace Savannabits\JetstreamInertiaGenerator\Generators; |
||
6 | class ViewFullForm extends ViewGenerator { |
||
7 | |||
8 | /** |
||
9 | * The name and signature of the console command. |
||
10 | * |
||
11 | * @var string |
||
12 | */ |
||
13 | protected $name = 'jig:generate:full-form'; |
||
14 | |||
15 | /** |
||
16 | * The console command description. |
||
17 | * |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $description = 'Generate a full-form view template'; |
||
21 | |||
22 | /** |
||
23 | * Path for view |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | protected string $view = 'full-form'; |
||
|
|||
28 | |||
29 | /** |
||
30 | * Path for js view |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected string $viewJs = 'form-js'; |
||
35 | |||
36 | /** |
||
37 | * Name of view, will be used in directory |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected string $fileName; |
||
42 | |||
43 | /** |
||
44 | * Route to process form |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | protected string $route; |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | protected string $formJsRelativePath; |
||
54 | |||
55 | /** |
||
56 | * Execute the console command. |
||
57 | * |
||
58 | * @return void |
||
59 | */ |
||
60 | public function handle() |
||
61 | { |
||
62 | $force = $this->option('force'); |
||
63 | |||
64 | //TODO check if exists |
||
65 | //TODO make global for all generator |
||
66 | //TODO also with prefix |
||
67 | if(!empty($template = $this->option('template'))) { |
||
68 | $this->view = 'templates.'.$template.'.full-form'; |
||
69 | $this->viewJs = 'templates.'.$template.'.form-js'; |
||
70 | } |
||
71 | |||
72 | $this->fileName = $this->option('file-name') ?: $this->modelViewsDirectory; |
||
73 | $this->formJsRelativePath = str_replace([DIRECTORY_SEPARATOR, '/', '\\'], '-', $this->fileName); |
||
74 | if (!$this->option('file-name')) { |
||
75 | $this->fileName = $this->fileName . DIRECTORY_SEPARATOR . 'form'; |
||
76 | } |
||
77 | |||
78 | $this->route = $this->option('route'); |
||
79 | if (!$this->route){ |
||
80 | if ($this->option('file-name')){ |
||
81 | $this->route = 'admin/'.$this->fileName; |
||
82 | } else { |
||
83 | $this->route = 'admin/'.$this->resource.'/update'; |
||
84 | } |
||
85 | } |
||
86 | |||
87 | $viewPath = resource_path('views/admin/'.$this->fileName.'.blade.php'); |
||
88 | if ($this->alreadyExists($viewPath) && !$force) { |
||
89 | $this->error('File '.$viewPath.' already exists!'); |
||
90 | } else { |
||
91 | if ($this->alreadyExists($viewPath) && $force) { |
||
92 | $this->warn('File '.$viewPath.' already exists! File will be deleted.'); |
||
93 | $this->files->delete($viewPath); |
||
94 | } |
||
95 | |||
96 | $this->makeDirectory($viewPath); |
||
97 | |||
98 | $this->files->put($viewPath, $this->buildForm()); |
||
99 | |||
100 | $this->info('Generating '.$viewPath.' finished'); |
||
101 | } |
||
102 | |||
103 | $formJsPath = resource_path('js/admin/'.$this->formJsRelativePath.'/Form.js'); |
||
104 | $bootstrapJsPath = resource_path('js/admin/index.js'); |
||
105 | |||
106 | if ($this->alreadyExists($formJsPath) && !$force) { |
||
107 | $this->error('File '.$formJsPath.' already exists!'); |
||
108 | } else { |
||
109 | if ($this->alreadyExists($formJsPath) && $force) { |
||
110 | $this->warn('File '.$formJsPath.' already exists! File will be deleted.'); |
||
111 | $this->files->delete($formJsPath); |
||
112 | } |
||
113 | |||
114 | $this->makeDirectory($formJsPath); |
||
115 | |||
116 | $this->files->put($formJsPath, $this->buildFormJs()); |
||
117 | $this->info('Generating '.$formJsPath.' finished'); |
||
118 | |||
119 | } |
||
120 | |||
121 | $indexJsPath = resource_path('js/admin/'.$this->formJsRelativePath.'/index.js'); |
||
122 | if ($this->alreadyExists($indexJsPath) && !$force) { |
||
123 | $this->error('File '.$indexJsPath.' already exists!'); |
||
124 | } else { |
||
125 | if ($this->alreadyExists($indexJsPath) && $force) { |
||
126 | $this->warn('File '.$indexJsPath.' already exists! File will be deleted.'); |
||
127 | $this->files->delete($indexJsPath); |
||
128 | } |
||
129 | $this->makeDirectory($indexJsPath); |
||
130 | } |
||
131 | |||
132 | if ($this->appendIfNotAlreadyAppended($indexJsPath, "import './Form';".PHP_EOL)){ |
||
133 | $this->info('Appending Form to '.$indexJsPath.' finished'); |
||
134 | }; |
||
135 | if ($this->appendIfNotAlreadyAppended($bootstrapJsPath, "import './". $this->formJsRelativePath ."';".PHP_EOL)){ |
||
136 | $this->info('Appending '.$this->formJsRelativePath.'/index.js to '.$bootstrapJsPath.' finished'); |
||
137 | }; |
||
138 | |||
139 | } |
||
140 | |||
141 | protected function buildForm(): string |
||
142 | { |
||
143 | |||
144 | return view('jig::'.$this->view, [ |
||
145 | 'modelBaseName' => $this->modelBaseName, |
||
146 | 'modelVariableName' => $this->modelVariableName, |
||
147 | 'route' => $this->route, |
||
148 | 'modelJSName' => $this->formJsRelativePath, |
||
149 | 'modelDotNotation' => $this->modelDotNotation, |
||
150 | 'modelLangFormat' => $this->modelLangFormat, |
||
151 | 'modelTitle' => $this->readColumnsFromTable($this->tableName)->filter(function($column){ |
||
152 | return in_array($column['name'], ['title', 'name', 'first_name', 'email']); |
||
153 | })->first(null, ['name'=>'id'])['name'], |
||
154 | |||
155 | 'columns' => $this->getVisibleColumns($this->tableName, $this->modelVariableName)->sortByDesc(function($column) { |
||
156 | return $column['type'] == "json"; |
||
157 | }), |
||
158 | 'hasTranslatable' => $this->readColumnsFromTable($this->tableName)->filter(function($column) { |
||
159 | return $column['type'] == "json"; |
||
160 | })->count() > 0, |
||
161 | 'translatableTextarea' => ['perex', 'text'], |
||
162 | 'relations' => $this->relations, |
||
163 | ])->render(); |
||
164 | } |
||
165 | |||
166 | protected function buildFormJs(): string |
||
167 | { |
||
168 | return view('jig::'.$this->viewJs, [ |
||
169 | 'modelJSName' => $this->formJsRelativePath, |
||
170 | |||
171 | 'columns' => $this->getVisibleColumns($this->tableName, $this->modelVariableName), |
||
172 | ])->render(); |
||
173 | } |
||
174 | |||
175 | protected function getOptions() { |
||
176 | return [ |
||
177 | ['model-name', 'm', InputOption::VALUE_OPTIONAL, 'Generates a code for the given model'], |
||
178 | ['template', 't', InputOption::VALUE_OPTIONAL, 'Specify custom template'], |
||
179 | ['file-name', 'nm', InputOption::VALUE_OPTIONAL, 'Specify a blade file path'], |
||
180 | ['route', 'r', InputOption::VALUE_OPTIONAL, 'Specify custom route for form'], |
||
181 | ['force', 'f', InputOption::VALUE_NONE, 'Force will delete files before regenerating full form'], |
||
182 | ]; |
||
183 | } |
||
184 | |||
186 |