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; |
||
9 | class Controller extends ClassGenerator { |
||
10 | |||
11 | use FileManipulations; |
||
12 | |||
13 | /** |
||
14 | * The name and signature of the console command. |
||
15 | * |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $name = 'jig:generate:controller'; |
||
19 | |||
20 | /** |
||
21 | * The console command description. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $description = 'Generate a web controller class'; |
||
26 | |||
27 | /** |
||
28 | * Path for view |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | protected string $view = 'controller'; |
||
|
|||
33 | |||
34 | /** |
||
35 | * Controller has also export method |
||
36 | * |
||
37 | * @return mixed |
||
38 | */ |
||
39 | protected bool $export = false; |
||
40 | |||
41 | /** |
||
42 | * Controller has also bulk options method |
||
43 | * |
||
44 | * @return mixed |
||
45 | */ |
||
46 | protected bool $withoutBulk = false; |
||
47 | protected string $modelTitle; |
||
48 | |||
49 | public function handle() |
||
50 | { |
||
51 | $force = $this->option('force'); |
||
52 | |||
53 | if($this->option('with-export')){ |
||
54 | $this->export = true; |
||
55 | } |
||
56 | |||
57 | if($this->option('without-bulk')){ |
||
58 | $this->withoutBulk = true; |
||
59 | } |
||
60 | // TODO test the case, if someone passes a class_name outside Laravel's default App\Http\Controllers folder, if it's going to work |
||
61 | |||
62 | //TODO check if exists |
||
63 | //TODO make global for all generator |
||
64 | //TODO also with prefix |
||
65 | if(!empty($template = $this->option('template'))) { |
||
66 | $this->view = 'templates.'.$template.'.controller'; |
||
67 | } |
||
68 | |||
69 | if(!empty($belongsToMany = $this->option('belongs-to-many'))) { |
||
70 | $this->setBelongToManyRelation($belongsToMany); |
||
71 | } |
||
72 | |||
73 | if ($this->generateClass($force)){ |
||
74 | |||
75 | $this->info('Generating '.$this->classFullName.' finished'); |
||
76 | |||
77 | $icon = "far fa-clone"; |
||
78 | $path = resource_path("js/Layouts/Jig/Menu.json"); |
||
79 | if (file_exists($path)) { |
||
80 | $menu = collect(json_decode(file_get_contents($path))); |
||
81 | } else { |
||
82 | $menu = collect([]); |
||
83 | } |
||
84 | if (!$menu->has($this->modelRouteAndViewName)) { |
||
85 | $item = [ |
||
86 | "route" => "admin.$this->modelRouteAndViewName.index", |
||
87 | "title" => $this->titlePlural, |
||
88 | "routePattern" => "admin.$this->modelRouteAndViewName.*", |
||
89 | "faIcon" => $icon, |
||
90 | "isTitle" => false, |
||
91 | "isParent" => false, |
||
92 | "children" => [] |
||
93 | ]; |
||
94 | $menu[$this->modelRouteAndViewName] = $item; |
||
95 | if (file_put_contents($path,json_encode($menu->toArray(), JSON_PRETTY_PRINT))) { |
||
96 | $this->info("Updating Sidebar Menu"); |
||
97 | }; |
||
98 | } |
||
99 | } |
||
100 | |||
101 | } |
||
102 | |||
103 | protected function buildClass() : string { |
||
104 | |||
105 | //Set belongsTo Relations |
||
106 | $this->setBelongsToRelations(); |
||
107 | return view('jig::'.$this->view, [ |
||
108 | 'controllerBaseName' => $this->classBaseName, |
||
109 | 'controllerNamespace' => $this->classNamespace, |
||
110 | 'repoBaseName' => $this->getRepositoryBaseName(), |
||
111 | "repoFullName" => $this->getRepoNamespace($this->rootNamespace()).'\\'.$this->getRepositoryBaseName(), |
||
112 | 'modelBaseName' => $this->modelBaseName, |
||
113 | 'modelFullName' => $this->modelFullName, |
||
114 | 'modelPlural' => $this->modelPlural, |
||
115 | 'modelTitle' => $this->titleSingular, |
||
116 | 'modelVariableName' => $this->modelVariableName, |
||
117 | 'modelRouteAndViewName' => $this->modelRouteAndViewName, |
||
118 | 'modelViewsDirectory' => $this->modelViewsDirectory, |
||
119 | 'modelDotNotation' => $this->modelDotNotation, |
||
120 | 'modelWithNamespaceFromDefault' => $this->modelWithNamespaceFromDefault, |
||
121 | 'export' => $this->export, |
||
122 | 'withoutBulk' => $this->withoutBulk, |
||
123 | 'exportBaseName' => $this->exportBaseName, |
||
124 | 'resource' => $this->resource, |
||
125 | 'containsPublishedAtColumn' => in_array("published_at", array_column($this->readColumnsFromTable($this->tableName)->toArray(), 'name')), |
||
126 | // index |
||
127 | 'columnsToQuery' => $this->readColumnsFromTable($this->tableName)->filter(function($column) { |
||
128 | return !($column['type'] == 'text' || $column['name'] == "password" || $column['name'] == "remember_token" || $column['name'] == "deleted_at"||Str::contains($column['name'],"_id")); |
||
129 | })->pluck('name')->toArray(), |
||
130 | 'columnsToSearchIn' => $this->readColumnsFromTable($this->tableName)->filter(function($column) { |
||
131 | return ($column['type'] == 'json' || $column['type'] == 'text' || $column['type'] == 'string' || $column['name'] == "id") && !($column['name'] == "password" || $column['name'] == "remember_token"); |
||
132 | })->pluck('name')->toArray(), |
||
133 | // 'filters' => $this->readColumnsFromTable($tableName)->filter(function($column) { |
||
134 | // return $column['type'] == 'boolean' || $column['type'] == 'date'; |
||
135 | // }), |
||
136 | // validation in store/update |
||
137 | 'columns' => $this->getVisibleColumns($this->tableName, $this->modelVariableName), |
||
138 | 'relations' => $this->relations, |
||
139 | 'hasSoftDelete' => $this->readColumnsFromTable($this->tableName)->filter(function($column) { |
||
140 | return $column['name'] == "deleted_at"; |
||
141 | })->count() > 0, |
||
142 | ])->render(); |
||
143 | } |
||
144 | |||
145 | protected function getOptions() { |
||
146 | return [ |
||
147 | ['model-name', 'm', InputOption::VALUE_OPTIONAL, 'Generates a code for the given model'], |
||
148 | ['template', 't', InputOption::VALUE_OPTIONAL, 'Specify custom template'], |
||
149 | ['belongs-to-many', 'btm', InputOption::VALUE_OPTIONAL, 'Specify belongs to many relations'], |
||
150 | ['force', 'f', InputOption::VALUE_NONE, 'Force will delete files before regenerating controller'], |
||
151 | ['model-with-full-namespace', 'fnm', InputOption::VALUE_OPTIONAL, 'Specify model with full namespace'], |
||
152 | ['with-export', 'e', InputOption::VALUE_NONE, 'Generate an option to Export as Excel'], |
||
153 | ['without-bulk', 'wb', InputOption::VALUE_NONE, 'Generate without bulk options'], |
||
154 | ]; |
||
155 | } |
||
156 | |||
157 | public function generateClassNameFromTable($tableName): string |
||
158 | { |
||
159 | return Str::studly(Str::singular($tableName)).'Controller'; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Get the default namespace for the class. |
||
164 | * |
||
165 | * @param string $rootNamespace |
||
166 | * @return string |
||
167 | */ |
||
168 | protected function getDefaultNamespace(string $rootNamespace) :string |
||
169 | { |
||
170 | return $rootNamespace.'\Http\Controllers\Admin'; |
||
171 | } |
||
172 | protected function getRepoNamespace($rootNamespace): string |
||
173 | { |
||
174 | return $rootNamespace.'Repositories'; |
||
175 | } |
||
176 | protected function getRepositoryBaseName(): string |
||
177 | { |
||
178 | return Str::studly(Str::plural($this->tableName)); |
||
181 |