| Total Complexity | 42 |
| Total Lines | 423 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ModuleMake 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 ModuleMake, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class ModuleMake extends Command |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * The name and signature of the console command. |
||
| 16 | * |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | protected $signature = 'twill:module {moduleName} |
||
| 20 | {--B|hasBlocks} |
||
| 21 | {--T|hasTranslation} |
||
| 22 | {--S|hasSlug} |
||
| 23 | {--M|hasMedias} |
||
| 24 | {--F|hasFiles} |
||
| 25 | {--P|hasPosition} |
||
| 26 | {--R|hasRevisions} |
||
| 27 | {--all}'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The console command description. |
||
| 31 | * |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $description = 'Create a new Twill Module'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var Filesystem |
||
| 38 | */ |
||
| 39 | protected $files; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var Composer |
||
| 43 | */ |
||
| 44 | protected $composer; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string[] |
||
| 48 | */ |
||
| 49 | protected $modelTraits; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var string[] |
||
| 53 | */ |
||
| 54 | protected $repositoryTraits; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var Config |
||
| 58 | */ |
||
| 59 | protected $config; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param Filesystem $files |
||
| 63 | * @param Composer $composer |
||
| 64 | * @param Config $config |
||
| 65 | */ |
||
| 66 | public function __construct(Filesystem $files, Composer $composer, Config $config) |
||
| 67 | { |
||
| 68 | parent::__construct(); |
||
| 69 | |||
| 70 | $this->files = $files; |
||
| 71 | $this->composer = $composer; |
||
| 72 | $this->config = $config; |
||
| 73 | |||
| 74 | $this->blockable = false; |
||
|
|
|||
| 75 | $this->translatable = false; |
||
| 76 | $this->sluggable = false; |
||
| 77 | $this->mediable = false; |
||
| 78 | $this->fileable = false; |
||
| 79 | $this->sortable = false; |
||
| 80 | $this->revisionable = false; |
||
| 81 | |||
| 82 | $this->defaultsAnswserToNo = false; |
||
| 83 | |||
| 84 | $this->modelTraits = ['HasBlocks', 'HasTranslation', 'HasSlug', 'HasMedias', 'HasFiles', 'HasRevisions', 'HasPosition']; |
||
| 85 | $this->repositoryTraits = ['HandleBlocks', 'HandleTranslations', 'HandleSlugs', 'HandleMedias', 'HandleFiles', 'HandleRevisions']; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Executes the console command. |
||
| 90 | * |
||
| 91 | * @return mixed |
||
| 92 | */ |
||
| 93 | public function handle() |
||
| 94 | { |
||
| 95 | $moduleName = Str::plural(lcfirst($this->argument('moduleName'))); |
||
| 96 | |||
| 97 | $enabledOptions = Collection::make($this->options())->only([ |
||
| 98 | 'hasBlocks', |
||
| 99 | 'hasTranslation', |
||
| 100 | 'hasSlug', |
||
| 101 | 'hasMedias', |
||
| 102 | 'hasFiles', |
||
| 103 | 'hasPosition', |
||
| 104 | 'hasRevisions', |
||
| 105 | ])->filter(function ($enabled) { |
||
| 106 | return $enabled; |
||
| 107 | }); |
||
| 108 | |||
| 109 | if (count($enabledOptions) > 0) { |
||
| 110 | $this->defaultsAnswserToNo = true; |
||
| 111 | } |
||
| 112 | |||
| 113 | $this->blockable = $this->checkOption('hasBlocks'); |
||
| 114 | $this->translatable = $this->checkOption('hasTranslation'); |
||
| 115 | $this->sluggable = $this->checkOption('hasSlug'); |
||
| 116 | $this->mediable = $this->checkOption('hasMedias'); |
||
| 117 | $this->fileable = $this->checkOption('hasFiles'); |
||
| 118 | $this->sortable = $this->checkOption('hasPosition'); |
||
| 119 | $this->revisionable = $this->checkOption('hasRevisions'); |
||
| 120 | |||
| 121 | $activeTraits = [ |
||
| 122 | $this->blockable, |
||
| 123 | $this->translatable, |
||
| 124 | $this->sluggable, |
||
| 125 | $this->mediable, |
||
| 126 | $this->fileable, |
||
| 127 | $this->revisionable, |
||
| 128 | $this->sortable, |
||
| 129 | ]; |
||
| 130 | |||
| 131 | $modelName = Str::studly(Str::singular($moduleName)); |
||
| 132 | |||
| 133 | $this->createMigration($moduleName); |
||
| 134 | $this->createModels($modelName, $activeTraits); |
||
| 135 | $this->createRepository($modelName, $activeTraits); |
||
| 136 | $this->createController($moduleName, $modelName); |
||
| 137 | $this->createRequest($modelName); |
||
| 138 | $this->createViews($moduleName); |
||
| 139 | |||
| 140 | $this->info("Add Route::module('{$moduleName}'); to your admin routes file."); |
||
| 141 | $this->info("Setup a new CMS menu item in config/twill-navigation.php:"); |
||
| 142 | |||
| 143 | $navTitle = Str::studly($moduleName); |
||
| 144 | $this->info(" |
||
| 145 | '{$moduleName}' => [ |
||
| 146 | 'title' => '{$navTitle}', |
||
| 147 | 'module' => true |
||
| 148 | ] |
||
| 149 | "); |
||
| 150 | |||
| 151 | $this->info("Migrate your database.\n"); |
||
| 152 | |||
| 153 | $this->info("Enjoy."); |
||
| 154 | |||
| 155 | $this->composer->dumpAutoloads(); |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Creates a new module database migration file. |
||
| 160 | * |
||
| 161 | * @param string $moduleName |
||
| 162 | * @return void |
||
| 163 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
||
| 164 | */ |
||
| 165 | private function createMigration($moduleName = 'items') |
||
| 166 | { |
||
| 167 | $table = Str::snake($moduleName); |
||
| 168 | $tableClassName = Str::studly($table); |
||
| 169 | |||
| 170 | $className = "Create{$tableClassName}Tables"; |
||
| 171 | |||
| 172 | $migrationName = 'create_' . $table . '_tables'; |
||
| 173 | |||
| 174 | if (!count(glob(database_path('migrations/*' . $migrationName . '.php')))) { |
||
| 175 | $migrationPath = $this->laravel->databasePath() . '/migrations'; |
||
| 176 | |||
| 177 | $fullPath = $this->laravel['migration.creator']->create($migrationName, $migrationPath); |
||
| 178 | |||
| 179 | $stub = str_replace( |
||
| 180 | ['{{table}}', '{{singularTableName}}', '{{tableClassName}}'], |
||
| 181 | [$table, Str::singular($table), $tableClassName], |
||
| 182 | $this->files->get(__DIR__ . '/stubs/migration.stub') |
||
| 183 | ); |
||
| 184 | |||
| 185 | if ($this->translatable) { |
||
| 186 | $stub = preg_replace('/{{!hasTranslation}}[\s\S]+?{{\/!hasTranslation}}/', '', $stub); |
||
| 187 | } else { |
||
| 188 | $stub = str_replace([ |
||
| 189 | '{{!hasTranslation}}', |
||
| 190 | '{{/!hasTranslation}}', |
||
| 191 | ], '', $stub); |
||
| 192 | } |
||
| 193 | |||
| 194 | $stub = $this->renderStubForOption($stub, 'hasTranslation', $this->translatable); |
||
| 195 | $stub = $this->renderStubForOption($stub, 'hasSlug', $this->sluggable); |
||
| 196 | $stub = $this->renderStubForOption($stub, 'hasRevisions', $this->revisionable); |
||
| 197 | $stub = $this->renderStubForOption($stub, 'hasPosition', $this->sortable); |
||
| 198 | |||
| 199 | $stub = preg_replace('/\}\);[\s\S]+?Schema::create/', "});\n\n Schema::create", $stub); |
||
| 200 | |||
| 201 | $this->files->put($fullPath, $stub); |
||
| 202 | |||
| 203 | $this->info("Migration created successfully! Add some fields!"); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Creates new model class files for the given model name and traits. |
||
| 209 | * |
||
| 210 | * @param string $modelName |
||
| 211 | * @param array $activeTraits |
||
| 212 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
||
| 213 | */ |
||
| 214 | private function createModels($modelName = 'Item', $activeTraits = []) |
||
| 215 | { |
||
| 216 | if (!$this->files->isDirectory(twill_path('Models'))) { |
||
| 217 | $this->files->makeDirectory(twill_path('Models')); |
||
| 218 | } |
||
| 219 | |||
| 220 | if ($this->translatable) { |
||
| 221 | if (!$this->files->isDirectory(twill_path('Models/Translations'))) { |
||
| 222 | $this->files->makeDirectory(twill_path('Models/Translations')); |
||
| 223 | } |
||
| 224 | |||
| 225 | $modelTranslationClassName = $modelName . 'Translation'; |
||
| 226 | |||
| 227 | $stub = str_replace('{{modelTranslationClassName}}', $modelTranslationClassName, $this->files->get(__DIR__ . '/stubs/model_translation.stub')); |
||
| 228 | |||
| 229 | $this->files->put(twill_path('Models/Translations/' . $modelTranslationClassName . '.php'), $stub); |
||
| 230 | } |
||
| 231 | |||
| 232 | if ($this->sluggable) { |
||
| 233 | if (!$this->files->isDirectory(twill_path('Models/Slugs'))) { |
||
| 234 | $this->files->makeDirectory(twill_path('Models/Slugs')); |
||
| 235 | } |
||
| 236 | |||
| 237 | $modelSlugClassName = $modelName . 'Slug'; |
||
| 238 | |||
| 239 | $stub = str_replace(['{{modelSlugClassName}}', '{{modelName}}'], [$modelSlugClassName, Str::snake($modelName)], $this->files->get(__DIR__ . '/stubs/model_slug.stub')); |
||
| 240 | |||
| 241 | $this->files->put(twill_path('Models/Slugs/' . $modelSlugClassName . '.php'), $stub); |
||
| 242 | } |
||
| 243 | |||
| 244 | if ($this->revisionable) { |
||
| 245 | if (!$this->files->isDirectory(twill_path('Models/Revisions'))) { |
||
| 246 | $this->files->makeDirectory(twill_path('Models/Revisions')); |
||
| 247 | } |
||
| 248 | |||
| 249 | $modelRevisionClassName = $modelName . 'Revision'; |
||
| 250 | |||
| 251 | $stub = str_replace(['{{modelRevisionClassName}}', '{{modelName}}'], [$modelRevisionClassName, Str::snake($modelName)], $this->files->get(__DIR__ . '/stubs/model_revision.stub')); |
||
| 252 | |||
| 253 | $this->files->put(twill_path('Models/Revisions/' . $modelRevisionClassName . '.php'), $stub); |
||
| 254 | } |
||
| 255 | |||
| 256 | $modelClassName = $modelName; |
||
| 257 | |||
| 258 | $activeModelTraits = []; |
||
| 259 | |||
| 260 | foreach ($activeTraits as $index => $traitIsActive) { |
||
| 261 | if ($traitIsActive) { |
||
| 262 | !isset($this->modelTraits[$index]) ?: $activeModelTraits[] = $this->modelTraits[$index]; |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | $activeModelTraitsString = empty($activeModelTraits) ? '' : 'use ' . rtrim(implode(', ', $activeModelTraits), ', ') . ';'; |
||
| 267 | |||
| 268 | $activeModelTraitsImports = empty($activeModelTraits) ? '' : "use A17\Twill\Models\Behaviors\\" . implode(";\nuse A17\Twill\Models\Behaviors\\", $activeModelTraits) . ";"; |
||
| 269 | |||
| 270 | $activeModelImplements = $this->sortable ? 'implements Sortable' : ''; |
||
| 271 | |||
| 272 | if ($this->sortable) { |
||
| 273 | $activeModelTraitsImports .= "\nuse A17\Twill\Models\Behaviors\Sortable;"; |
||
| 274 | } |
||
| 275 | |||
| 276 | $stub = str_replace([ |
||
| 277 | '{{modelClassName}}', |
||
| 278 | '{{modelTraits}}', |
||
| 279 | '{{modelImports}}', |
||
| 280 | '{{modelImplements}}', |
||
| 281 | ], [ |
||
| 282 | $modelClassName, |
||
| 283 | $activeModelTraitsString, |
||
| 284 | $activeModelTraitsImports, |
||
| 285 | $activeModelImplements, |
||
| 286 | ], $this->files->get(__DIR__ . '/stubs/model.stub')); |
||
| 287 | |||
| 288 | $stub = $this->renderStubForOption($stub, 'hasTranslation', $this->translatable); |
||
| 289 | $stub = $this->renderStubForOption($stub, 'hasSlug', $this->sluggable); |
||
| 290 | $stub = $this->renderStubForOption($stub, 'hasMedias', $this->mediable); |
||
| 291 | $stub = $this->renderStubForOption($stub, 'hasPosition', $this->sortable); |
||
| 292 | |||
| 293 | $this->files->put(twill_path('Models/' . $modelClassName . '.php'), $stub); |
||
| 294 | |||
| 295 | $this->info("Models created successfully! Fill your fillables!"); |
||
| 296 | } |
||
| 297 | |||
| 298 | private function renderStubForOption($stub, $option, $enabled) |
||
| 299 | { |
||
| 300 | if ($enabled) { |
||
| 301 | $stub = str_replace([ |
||
| 302 | '{{' . $option . '}}', |
||
| 303 | '{{/' . $option . '}}', |
||
| 304 | ], '', $stub); |
||
| 305 | } else { |
||
| 306 | $stub = preg_replace('/{{' . $option . '}}[\s\S]+?{{\/' . $option . '}}/', '', $stub); |
||
| 307 | } |
||
| 308 | |||
| 309 | return $stub; |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Creates new repository class file for the given model name. |
||
| 314 | * |
||
| 315 | * @param string $modelName |
||
| 316 | * @param array $activeTraits |
||
| 317 | * @return void |
||
| 318 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
||
| 319 | */ |
||
| 320 | private function createRepository($modelName = 'Item', $activeTraits = []) |
||
| 321 | { |
||
| 322 | if (!$this->files->isDirectory(twill_path('Repositories'))) { |
||
| 323 | $this->files->makeDirectory(twill_path('Repositories')); |
||
| 324 | } |
||
| 325 | |||
| 326 | $repositoryClassName = $modelName . 'Repository'; |
||
| 327 | |||
| 328 | $activeRepositoryTraits = []; |
||
| 329 | |||
| 330 | foreach ($activeTraits as $index => $traitIsActive) { |
||
| 331 | if ($traitIsActive) { |
||
| 332 | !isset($this->repositoryTraits[$index]) ?: $activeRepositoryTraits[] = $this->repositoryTraits[$index]; |
||
| 333 | } |
||
| 334 | } |
||
| 335 | |||
| 336 | $activeRepositoryTraitsString = empty($activeRepositoryTraits) ? '' : 'use ' . (empty($activeRepositoryTraits) ? "" : rtrim(implode(', ', $activeRepositoryTraits), ', ') . ';'); |
||
| 337 | |||
| 338 | $activeRepositoryTraitsImports = empty($activeRepositoryTraits) ? '' : "use A17\Twill\Repositories\Behaviors\\" . implode(";\nuse A17\Twill\Repositories\Behaviors\\", $activeRepositoryTraits) . ";"; |
||
| 339 | |||
| 340 | $stub = str_replace(['{{repositoryClassName}}', '{{modelName}}', '{{repositoryTraits}}', '{{repositoryImports}}'], [$repositoryClassName, $modelName, $activeRepositoryTraitsString, $activeRepositoryTraitsImports], $this->files->get(__DIR__ . '/stubs/repository.stub')); |
||
| 341 | |||
| 342 | $this->files->put(twill_path('Repositories/' . $repositoryClassName . '.php'), $stub); |
||
| 343 | |||
| 344 | $this->info("Repository created successfully! Control all the things!"); |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Create a new controller class file for the given module name and model name. |
||
| 349 | * |
||
| 350 | * @param string $moduleName |
||
| 351 | * @param string $modelName |
||
| 352 | * @return void |
||
| 353 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
||
| 354 | */ |
||
| 355 | private function createController($moduleName = 'items', $modelName = 'Item') |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Creates a new request class file for the given model name. |
||
| 376 | * |
||
| 377 | * @param string $modelName |
||
| 378 | * @return void |
||
| 379 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
||
| 380 | */ |
||
| 381 | private function createRequest($modelName = 'Item') |
||
| 382 | { |
||
| 383 | if (!$this->files->isDirectory(twill_path('Http/Requests/Admin'))) { |
||
| 384 | $this->files->makeDirectory(twill_path('Http/Requests/Admin'), 0755, true); |
||
| 385 | } |
||
| 386 | |||
| 387 | $requestClassName = $modelName . 'Request'; |
||
| 388 | |||
| 389 | $stub = str_replace('{{requestClassName}}', $requestClassName, $this->files->get(__DIR__ . '/stubs/request.stub')); |
||
| 390 | |||
| 391 | $this->files->put(twill_path('Http/Requests/Admin/' . $requestClassName . '.php'), $stub); |
||
| 392 | |||
| 393 | $this->info("Form request created successfully! Add some validation rules!"); |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Creates appropriate module Blade view files. |
||
| 398 | * |
||
| 399 | * @param string $moduleName |
||
| 400 | * @return void |
||
| 401 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
||
| 402 | */ |
||
| 403 | private function createViews($moduleName = 'items') |
||
| 404 | { |
||
| 405 | $viewsPath = $this->config->get('view.paths')[0] . '/admin/' . $moduleName; |
||
| 406 | |||
| 407 | if (!$this->files->isDirectory($viewsPath)) { |
||
| 408 | $this->files->makeDirectory($viewsPath, 0755, true); |
||
| 409 | } |
||
| 410 | |||
| 411 | $formView = $this->translatable ? 'form_translatable' : 'form'; |
||
| 412 | |||
| 413 | $this->files->put($viewsPath . '/form.blade.php', $this->files->get(__DIR__ . '/stubs/' . $formView . '.blade.stub')); |
||
| 414 | |||
| 415 | $this->info("Form view created successfully! Include your form fields using @formField directives!"); |
||
| 416 | } |
||
| 417 | |||
| 418 | private function checkOption($option) |
||
| 435 | } |
||
| 436 | } |
||
| 437 |