| Total Complexity | 45 |
| Total Lines | 345 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ScaffoldController 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 ScaffoldController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class ScaffoldController extends BaseController |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * 主页. |
||
| 36 | * |
||
| 37 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 38 | */ |
||
| 39 | public function index() |
||
| 40 | { |
||
| 41 | $dbTypes = [ |
||
| 42 | 'string', |
||
| 43 | 'integer', |
||
| 44 | 'text', |
||
| 45 | 'float', |
||
| 46 | 'double', |
||
| 47 | 'decimal', |
||
| 48 | 'boolean', |
||
| 49 | 'date', |
||
| 50 | 'time', |
||
| 51 | 'dateTime', |
||
| 52 | 'timestamp', |
||
| 53 | 'char', |
||
| 54 | 'mediumText', |
||
| 55 | 'longText', |
||
| 56 | 'tinyInteger', |
||
| 57 | 'smallInteger', |
||
| 58 | 'mediumInteger', |
||
| 59 | 'bigInteger', |
||
| 60 | 'unsignedTinyInteger', |
||
| 61 | 'unsignedSmallInteger', |
||
| 62 | 'unsignedMediumInteger', |
||
| 63 | 'unsignedInteger', |
||
| 64 | 'unsignedBigInteger', |
||
| 65 | 'enum', |
||
| 66 | 'json', |
||
| 67 | 'jsonb', |
||
| 68 | 'dateTimeTz', |
||
| 69 | 'timeTz', |
||
| 70 | 'timestampTz', |
||
| 71 | 'nullableTimestamps', |
||
| 72 | 'binary', |
||
| 73 | 'ipAddress', |
||
| 74 | 'macAddress', |
||
| 75 | ]; |
||
| 76 | $action = URL::current(); |
||
| 77 | |||
| 78 | return view('backend.tools.scaffold.index', [ |
||
| 79 | 'header_description' => '脚手架', |
||
| 80 | 'dbTypes' => $dbTypes, |
||
| 81 | 'action' => $action, |
||
| 82 | ]); |
||
| 83 | } |
||
| 84 | |||
| 85 | public function store(Request $request) |
||
| 86 | { |
||
| 87 | $paths = []; |
||
| 88 | $message = ''; |
||
| 89 | |||
| 90 | try { |
||
| 91 | $model_name = Str::camel( |
||
| 92 | ucfirst(str_singular(lcfirst(ucwords($request->get('model_name'))))) |
||
| 93 | ); |
||
| 94 | |||
| 95 | $force = false; |
||
| 96 | if (in_array('force', $request->get('create'))) { |
||
| 97 | $force = true; |
||
| 98 | } |
||
| 99 | |||
| 100 | $presenter = $validator = 'no'; |
||
| 101 | $fillable = $rules = ''; |
||
| 102 | $fields = $request->get('fields'); |
||
| 103 | |||
| 104 | if (!empty($fields)) { |
||
| 105 | foreach ($fields as $index => $field) { |
||
| 106 | if ($field['name'] && $field['type']) { |
||
| 107 | $fillable .= $field['name'].':'.$field['type']; |
||
| 108 | if (isset($field['nullable'])) { |
||
| 109 | $fillable .= ':nullable()'; |
||
| 110 | } |
||
| 111 | if ($field['key']) { |
||
| 112 | $fillable .= ':'.$field['key']; |
||
| 113 | } |
||
| 114 | if ($field['default']) { |
||
| 115 | $fillable .= ':default'."('{$field['default']}')"; |
||
| 116 | } |
||
| 117 | if ($field['comment']) { |
||
| 118 | $fillable .= ':comment'."('{$field['comment']}')"; |
||
| 119 | } |
||
| 120 | $fillable .= ','; |
||
| 121 | if ($field['rule']) { |
||
| 122 | $rules .= $field['name'].'=>'.$field['rule'].','; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | $fillable = rtrim($fillable, ','); |
||
| 129 | |||
| 130 | $rules = rtrim($rules, ','); |
||
| 131 | |||
| 132 | // 1. Create presenter. |
||
| 133 | if (in_array('presenter', $request->get('create'))) { |
||
| 134 | Artisan::call('yl:presenter', [ |
||
| 135 | 'name' => $model_name, |
||
| 136 | '--force' => $force, |
||
| 137 | ]); |
||
| 138 | $paths['presenters'] = $this->getBasePath().'/'.$this->getConfigGeneratorClassPath('presenters', true).'/'.$this->getName($model_name).'Presenter.php'; |
||
| 139 | |||
| 140 | Artisan::call('yl:transformer', [ |
||
| 141 | 'name' => $model_name, |
||
| 142 | '--fields' => $fields, |
||
| 143 | '--force' => $force, |
||
| 144 | ]); |
||
| 145 | $paths['transformers'] = $this->getBasePath().'/'.$this->getConfigGeneratorClassPath('transformers', true).'/'.$this->getName($model_name).'Transformer.php'; |
||
| 146 | |||
| 147 | $presenter = 'yes'; |
||
| 148 | } |
||
| 149 | |||
| 150 | // 2. Create validator. |
||
| 151 | if (in_array('validator', $request->get('create'))) { |
||
| 152 | Artisan::call('yl:validator', [ |
||
| 153 | 'name' => $model_name, |
||
| 154 | '--rules' => $rules, |
||
| 155 | '--force' => $force, |
||
| 156 | ]); |
||
| 157 | $paths['validators'] = $this->getBasePath().'/'.$this->getConfigGeneratorClassPath('validators', true).'/'.$this->getName($model_name).'Validator.php'; |
||
| 158 | $validator = 'yes'; |
||
| 159 | } |
||
| 160 | |||
| 161 | // 3. Create controller. |
||
| 162 | if (in_array('controller', $request->get('create'))) { |
||
| 163 | Artisan::call('yl:controller', [ |
||
| 164 | 'name' => $model_name, |
||
| 165 | '--fields' => $fields, |
||
| 166 | '--force' => $force, |
||
| 167 | ]); |
||
| 168 | $paths['controllers'] = $this->getBasePath().'/'.$this->getConfigGeneratorClassPath('controllers', true).'/'.Str::plural($this->getName($model_name)).'Controller.php'; |
||
| 169 | $paths['create_request'] = $this->getBasePath().'/Http/Requests/'.$this->getName($model_name).'CreateRequest.php'; |
||
| 170 | $paths['update_request'] = $this->getBasePath().'/Http/Requests/'.$this->getName($model_name).'UpdateRequest.php'; |
||
| 171 | } |
||
| 172 | |||
| 173 | // 4. Create views. |
||
| 174 | if (in_array('views', $request->get('create'))) { |
||
| 175 | Artisan::call('yl:views', [ |
||
| 176 | 'name' => $model_name, |
||
| 177 | '--fields' => $fields, |
||
| 178 | '--force' => $force, |
||
| 179 | ]); |
||
| 180 | $paths['views_index'] = resource_path($this->getConfigGeneratorClassPath('views', true)).'/'.$this->getSnakeName($model_name).'/index.blade.php'; |
||
| 181 | $paths['views_create'] = resource_path($this->getConfigGeneratorClassPath('views', true)).'/'.$this->getSnakeName($model_name).'/create.blade.php'; |
||
| 182 | $paths['views_edit'] = resource_path($this->getConfigGeneratorClassPath('views', true)).'/'.$this->getSnakeName($model_name).'/edit.blade.php'; |
||
| 183 | } |
||
| 184 | |||
| 185 | // 5. Create language package. |
||
| 186 | if (in_array('lang', $request->get('create'))) { |
||
| 187 | Artisan::call('yl:lang', [ |
||
| 188 | 'name' => $model_name, |
||
| 189 | '--fields' => $fields, |
||
| 190 | '--force' => $force, |
||
| 191 | ]); |
||
| 192 | $filesystem = new Filesystem(); |
||
| 193 | $directories = $filesystem->directories(resource_path($this->getConfigGeneratorClassPath('lang', true))); |
||
| 194 | foreach ($directories as $index => $directory) { |
||
| 195 | $paths['lang_'.$index] = $directory.'/'.$this->getSnakeName($model_name).'.php'; |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | // 6. Create api controller. |
||
| 200 | if (in_array('api_controller', $request->get('create'))) { |
||
| 201 | Artisan::call('yl:api_controller', [ |
||
| 202 | 'name' => $model_name, |
||
| 203 | '--fields' => $fields, |
||
| 204 | '--force' => $force, |
||
| 205 | ]); |
||
| 206 | $paths['api_controllers'] = $this->getBasePath().'/'.$this->getConfigGeneratorClassPath('api_controllers', true).'/'.Str::plural($this->getName($model_name)).'Controller.php'; |
||
| 207 | $paths['api_create_request'] = $this->getBasePath().'/Http/Requests/Api/'.$this->getName($model_name).'CreateRequest.php'; |
||
| 208 | $paths['api_update_request'] = $this->getBasePath().'/Http/Requests/Api/'.$this->getName($model_name).'UpdateRequest.php'; |
||
| 209 | } |
||
| 210 | |||
| 211 | //7. Create repository |
||
| 212 | Artisan::call('yl:repository', [ |
||
| 213 | 'name' => $model_name, |
||
| 214 | '--fillable' => $fillable, |
||
| 215 | '--rules' => $rules, |
||
| 216 | '--fields' => $fields, |
||
| 217 | '--validator' => $validator, |
||
| 218 | '--presenter' => $presenter, |
||
| 219 | '--force' => $force, |
||
| 220 | ]); |
||
| 221 | $paths['repositories'] = $this->getBasePath().'/'.$this->getConfigGeneratorClassPath('repositories', true).'/'.$this->getName($model_name).'RepositoryEloquent.php'; |
||
| 222 | $paths['interfaces'] = $this->getBasePath().'/'.$this->getConfigGeneratorClassPath('interfaces', true).'/'.$this->getName($model_name).'Repository.php'; |
||
| 223 | $paths['models'] = $this->getBasePath().'/'.$this->getConfigGeneratorClassPath('models', true).'/'.($model_name).'.php'; |
||
| 224 | |||
| 225 | //8. Bind repository |
||
| 226 | Artisan::call('yl:bindings', [ |
||
| 227 | 'name' => $model_name, |
||
| 228 | '--force' => $force, |
||
| 229 | ]); |
||
| 230 | |||
| 231 | // 9. Run migrate. |
||
| 232 | if (in_array('migrate', $request->get('create'))) { |
||
| 233 | Artisan::call('migrate'); |
||
| 234 | $message = Artisan::output(); |
||
| 235 | } |
||
| 236 | } catch (\Exception $exception) { |
||
| 237 | // Delete generated files if exception thrown. |
||
| 238 | app('files')->delete($paths); |
||
| 239 | |||
| 240 | return $this->backWithException($exception); |
||
| 241 | } |
||
| 242 | |||
| 243 | return $this->backWithSuccess($model_name, $paths, $message); |
||
| 244 | } |
||
| 245 | |||
| 246 | protected function backWithException(\Exception $exception) |
||
| 247 | { |
||
| 248 | $error = new MessageBag([ |
||
| 249 | 'title' => 'Error', |
||
| 250 | 'message' => $exception->getMessage()."\n Please delete the migration files", |
||
| 251 | ]); |
||
| 252 | |||
| 253 | return back()->withInput()->with(compact('error')); |
||
| 254 | } |
||
| 255 | |||
| 256 | protected function backWithSuccess($model_name, $paths, $message) |
||
| 257 | { |
||
| 258 | $messages = []; |
||
| 259 | foreach ($paths as $name => $path) { |
||
| 260 | $messages[] = ucfirst($name).": $path"; |
||
| 261 | } |
||
| 262 | $messages[] = 'Migrations: '.database_path('migrations').'/'.date('Y_m_d').'_xxxxxx_create_'.$this->getSnakeName($model_name).'.php'; |
||
| 263 | $messages[] = "<br />$message"; |
||
| 264 | $success = new MessageBag([ |
||
| 265 | 'title' => 'Success', |
||
| 266 | 'message' => implode('<br />', $messages), |
||
| 267 | ]); |
||
| 268 | |||
| 269 | return back()->with(compact('success')); |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Get base path of destination file. |
||
| 274 | * |
||
| 275 | * @return string |
||
| 276 | */ |
||
| 277 | public function getBasePath() |
||
| 278 | { |
||
| 279 | return config('repository.generator.basePath', app_path()); |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Get name input. |
||
| 284 | * |
||
| 285 | * @param $name |
||
| 286 | * |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | public function getName($name) |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Get name input. |
||
| 303 | * |
||
| 304 | * @param $name |
||
| 305 | * |
||
| 306 | * @return string |
||
| 307 | */ |
||
| 308 | public function getSnakeName($name) |
||
| 309 | { |
||
| 310 | if (str_contains($name, '\\')) { |
||
| 311 | $name = str_replace('\\', '/', $name); |
||
| 312 | } |
||
| 313 | if (str_contains($name, '/')) { |
||
| 314 | $name = str_replace('/', '/', $name); |
||
| 315 | } |
||
| 316 | |||
| 317 | return Str::plural(Str::snake(str_replace(' ', '/', ucwords(str_replace('/', ' ', $name))))); |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Get class-specific output paths. |
||
| 322 | * |
||
| 323 | * @param $class |
||
| 324 | * |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | private function getConfigGeneratorClassPath($class, $directoryPath = false) |
||
| 377 | } |
||
| 378 | } |
||
| 379 |