| Total Complexity | 91 |
| Total Lines | 843 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| 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:make:module {moduleName} |
||
| 20 | {--B|hasBlocks} |
||
| 21 | {--T|hasTranslation} |
||
| 22 | {--S|hasSlug} |
||
| 23 | {--M|hasMedias} |
||
| 24 | {--F|hasFiles} |
||
| 25 | {--P|hasPosition} |
||
| 26 | {--R|hasRevisions} |
||
| 27 | {--N|hasNesting} |
||
| 28 | {--all}'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The console command description. |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $description = 'Create a new Twill Module'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var Filesystem |
||
| 39 | */ |
||
| 40 | protected $files; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var Composer |
||
| 44 | */ |
||
| 45 | protected $composer; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string[] |
||
| 49 | */ |
||
| 50 | protected $modelTraits; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string[] |
||
| 54 | */ |
||
| 55 | protected $repositoryTraits; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var Config |
||
| 59 | */ |
||
| 60 | protected $config; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var bool |
||
| 64 | */ |
||
| 65 | protected $blockable; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var bool |
||
| 69 | */ |
||
| 70 | protected $translatable; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var bool |
||
| 74 | */ |
||
| 75 | protected $sluggable; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var bool |
||
| 79 | */ |
||
| 80 | protected $mediable; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var bool |
||
| 84 | */ |
||
| 85 | protected $fileable; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var bool |
||
| 89 | */ |
||
| 90 | protected $sortable; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var bool |
||
| 94 | */ |
||
| 95 | protected $revisionable; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var bool |
||
| 99 | */ |
||
| 100 | protected $nestable; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var bool |
||
| 104 | */ |
||
| 105 | protected $defaultsAnswserToNo; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var bool |
||
| 109 | */ |
||
| 110 | protected $isCapsule = false; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var bool |
||
| 114 | */ |
||
| 115 | protected $isSingleton = false; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var string |
||
| 119 | */ |
||
| 120 | protected $moduleBasePath; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var string |
||
| 124 | */ |
||
| 125 | protected $capsule; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @param Filesystem $files |
||
| 129 | * @param Composer $composer |
||
| 130 | * @param Config $config |
||
| 131 | */ |
||
| 132 | public function __construct(Filesystem $files, Composer $composer, Config $config) |
||
| 133 | { |
||
| 134 | parent::__construct(); |
||
| 135 | |||
| 136 | $this->files = $files; |
||
| 137 | $this->composer = $composer; |
||
| 138 | $this->config = $config; |
||
| 139 | |||
| 140 | $this->blockable = false; |
||
| 141 | $this->translatable = false; |
||
| 142 | $this->sluggable = false; |
||
| 143 | $this->mediable = false; |
||
| 144 | $this->fileable = false; |
||
| 145 | $this->sortable = false; |
||
| 146 | $this->revisionable = false; |
||
| 147 | $this->nestable = false; |
||
| 148 | |||
| 149 | $this->defaultsAnswserToNo = false; |
||
| 150 | |||
| 151 | $this->modelTraits = ['HasBlocks', 'HasTranslation', 'HasSlug', 'HasMedias', 'HasFiles', 'HasRevisions', 'HasPosition', 'HasNesting']; |
||
| 152 | $this->repositoryTraits = ['HandleBlocks', 'HandleTranslations', 'HandleSlugs', 'HandleMedias', 'HandleFiles', 'HandleRevisions', '', 'HandleNesting']; |
||
| 153 | } |
||
| 154 | |||
| 155 | protected function checkCapsuleDirectory($dir) |
||
| 156 | { |
||
| 157 | if (file_exists($dir)) { |
||
| 158 | if (!$this->option('force')) { |
||
| 159 | $answer = $this->choice("Capsule path exists ({$dir}). Erase and overwrite?", |
||
| 160 | ['no', 'yes'], $this->defaultsAnswserToNo |
||
| 161 | ? 0 |
||
| 162 | : 1); |
||
| 163 | } |
||
| 164 | |||
| 165 | if ('yes' === ($answer ?? 'no') || $this->option('force')) { |
||
| 166 | File::deleteDirectory($dir); |
||
| 167 | |||
| 168 | if (file_exists($dir)) { |
||
| 169 | $this->info("Directory could not be deleted. Aborted."); |
||
| 170 | die; |
||
|
|
|||
| 171 | } |
||
| 172 | } else { |
||
| 173 | $this->info("Aborted"); |
||
| 174 | |||
| 175 | die; |
||
| 176 | } |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Executes the console command. |
||
| 182 | * |
||
| 183 | * @return mixed |
||
| 184 | */ |
||
| 185 | public function handle() |
||
| 186 | { |
||
| 187 | // e.g. newsItems |
||
| 188 | $moduleName = Str::camel(Str::plural(lcfirst($this->argument('moduleName')))); |
||
| 189 | |||
| 190 | // e.g. newsItem |
||
| 191 | $singularModuleName = Str::camel(lcfirst($this->argument('moduleName'))); |
||
| 192 | |||
| 193 | // e.g. NewsItems |
||
| 194 | $moduleTitle = Str::studly($moduleName); |
||
| 195 | |||
| 196 | // e.g. NewsItem |
||
| 197 | $modelName = Str::studly(Str::singular($moduleName)); |
||
| 198 | |||
| 199 | $this->capsule = app('twill.capsules.manager')->makeCapsule(['name' => $moduleName], config("twill.capsules.path")); |
||
| 200 | |||
| 201 | $enabledOptions = Collection::make($this->options())->only([ |
||
| 202 | 'hasBlocks', |
||
| 203 | 'hasTranslation', |
||
| 204 | 'hasSlug', |
||
| 205 | 'hasMedias', |
||
| 206 | 'hasFiles', |
||
| 207 | 'hasPosition', |
||
| 208 | 'hasRevisions', |
||
| 209 | 'hasNesting', |
||
| 210 | ])->filter(function ($enabled) { |
||
| 211 | return $enabled; |
||
| 212 | }); |
||
| 213 | |||
| 214 | if (count($enabledOptions) > 0) { |
||
| 215 | $this->defaultsAnswserToNo = true; |
||
| 216 | } |
||
| 217 | |||
| 218 | $this->blockable = $this->checkOption('hasBlocks'); |
||
| 219 | $this->translatable = $this->checkOption('hasTranslation'); |
||
| 220 | $this->sluggable = $this->checkOption('hasSlug'); |
||
| 221 | $this->mediable = $this->checkOption('hasMedias'); |
||
| 222 | $this->fileable = $this->checkOption('hasFiles'); |
||
| 223 | $this->sortable = $this->checkOption('hasPosition'); |
||
| 224 | $this->revisionable = $this->checkOption('hasRevisions'); |
||
| 225 | $this->nestable = $this->checkOption('hasNesting'); |
||
| 226 | |||
| 227 | if ($this->nestable) { |
||
| 228 | $this->sortable = true; |
||
| 229 | } |
||
| 230 | |||
| 231 | $activeTraits = [ |
||
| 232 | $this->blockable, |
||
| 233 | $this->translatable, |
||
| 234 | $this->sluggable, |
||
| 235 | $this->mediable, |
||
| 236 | $this->fileable, |
||
| 237 | $this->revisionable, |
||
| 238 | $this->sortable, |
||
| 239 | $this->nestable, |
||
| 240 | ]; |
||
| 241 | |||
| 242 | $this->createCapsuleNamespace($moduleTitle, $modelName); |
||
| 243 | $this->createCapsulePath($moduleTitle, $modelName); |
||
| 244 | |||
| 245 | $this->createMigration($moduleName); |
||
| 246 | $this->createModels($modelName, $activeTraits); |
||
| 247 | $this->createRepository($modelName, $activeTraits); |
||
| 248 | $this->createController($moduleName, $modelName); |
||
| 249 | $this->createRequest($modelName); |
||
| 250 | $this->createViews($moduleName); |
||
| 251 | |||
| 252 | if ($this->isCapsule) { |
||
| 253 | $this->createCapsuleRoutes($moduleName); |
||
| 254 | $this->createCapsuleSeed($moduleName); |
||
| 255 | } elseif ($this->isSingleton) { |
||
| 256 | $this->createSingletonSeed($modelName); |
||
| 257 | $this->info("\nAdd to routes/admin.php:\n"); |
||
| 258 | $this->info(" Route::singleton('{$singularModuleName}');\n"); |
||
| 259 | } else { |
||
| 260 | $this->info("\nAdd to routes/admin.php:\n"); |
||
| 261 | $this->info(" Route::module('{$moduleName}');\n"); |
||
| 262 | } |
||
| 263 | |||
| 264 | $navModuleName = $this->isSingleton ? $singularModuleName : $moduleName; |
||
| 265 | $navTitle = $this->isSingleton ? $modelName : $moduleTitle; |
||
| 266 | $navType = $this->isSingleton ? 'singleton' : 'module'; |
||
| 267 | |||
| 268 | $this->info("Setup a new CMS menu item in config/twill-navigation.php:\n"); |
||
| 269 | $this->info(" '{$navModuleName}' => ["); |
||
| 270 | $this->info(" 'title' => '{$navTitle}',"); |
||
| 271 | $this->info(" '{$navType}' => true,"); |
||
| 272 | $this->info(" ],\n"); |
||
| 273 | |||
| 274 | if ($this->isCapsule) { |
||
| 275 | $this->info("Setup your new Capsule in config/twill.php:\n"); |
||
| 276 | $this->info(" 'capsules' => ["); |
||
| 277 | $this->info(" 'list' => ["); |
||
| 278 | $this->info(" ["); |
||
| 279 | $this->info(" 'name' => '{$this->capsule['name']}',"); |
||
| 280 | $this->info(" 'enabled' => true,"); |
||
| 281 | $this->info(" ],"); |
||
| 282 | $this->info(" ],"); |
||
| 283 | $this->info(" ],\n"); |
||
| 284 | } |
||
| 285 | |||
| 286 | if ($this->isSingleton) { |
||
| 287 | $this->info("Migrate your database & seed your singleton module:\n"); |
||
| 288 | $this->info(" php artisan migrate\n"); |
||
| 289 | $this->info(" php artisan db:seed {$modelName}Seeder\n"); |
||
| 290 | } else { |
||
| 291 | $this->info("Migrate your database.\n"); |
||
| 292 | } |
||
| 293 | |||
| 294 | $this->info("Enjoy."); |
||
| 295 | |||
| 296 | if ($this->nestable && !class_exists('\Kalnoy\Nestedset\NestedSet')) { |
||
| 297 | $this->warn("\nTo support module nesting, you must install the `kalnoy/nestedset` package:"); |
||
| 298 | $this->warn("\n composer require kalnoy/nestedset\n"); |
||
| 299 | } |
||
| 300 | |||
| 301 | $this->composer->dumpAutoloads(); |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Creates a new module database migration file. |
||
| 306 | * |
||
| 307 | * @param string $moduleName |
||
| 308 | * @return void |
||
| 309 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
||
| 310 | */ |
||
| 311 | private function createMigration($moduleName = 'items') |
||
| 312 | { |
||
| 313 | $table = Str::snake($moduleName); |
||
| 314 | $tableClassName = Str::studly($table); |
||
| 315 | |||
| 316 | $className = "Create{$tableClassName}Tables"; |
||
| 317 | |||
| 318 | $migrationName = 'create_' . $table . '_tables'; |
||
| 319 | |||
| 320 | if (!count(glob($this->databasePath('migrations/*' . $migrationName . '.php')))) { |
||
| 321 | $migrationPath = $this->databasePath() . '/migrations'; |
||
| 322 | |||
| 323 | $this->makeDir($migrationPath); |
||
| 324 | |||
| 325 | $fullPath = $this->laravel['migration.creator']->create($migrationName, $migrationPath); |
||
| 326 | |||
| 327 | $stub = str_replace( |
||
| 328 | ['{{table}}', '{{singularTableName}}', '{{tableClassName}}'], |
||
| 329 | [$table, Str::singular($table), $tableClassName], |
||
| 330 | $this->files->get(__DIR__ . '/stubs/migration.stub') |
||
| 331 | ); |
||
| 332 | |||
| 333 | if ($this->translatable) { |
||
| 334 | $stub = preg_replace('/{{!hasTranslation}}[\s\S]+?{{\/!hasTranslation}}/', '', $stub); |
||
| 335 | } else { |
||
| 336 | $stub = str_replace([ |
||
| 337 | '{{!hasTranslation}}', |
||
| 338 | '{{/!hasTranslation}}', |
||
| 339 | ], '', $stub); |
||
| 340 | } |
||
| 341 | |||
| 342 | $stub = $this->renderStubForOption($stub, 'hasTranslation', $this->translatable); |
||
| 343 | $stub = $this->renderStubForOption($stub, 'hasSlug', $this->sluggable); |
||
| 344 | $stub = $this->renderStubForOption($stub, 'hasRevisions', $this->revisionable); |
||
| 345 | $stub = $this->renderStubForOption($stub, 'hasPosition', $this->sortable); |
||
| 346 | $stub = $this->renderStubForOption($stub, 'hasNesting', $this->nestable); |
||
| 347 | |||
| 348 | $stub = preg_replace('/\}\);[\s\S]+?Schema::create/', "});\n\n Schema::create", $stub); |
||
| 349 | |||
| 350 | $this->files->put($fullPath, $stub); |
||
| 351 | |||
| 352 | $this->info("Migration created successfully! Add some fields!"); |
||
| 353 | } |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Creates new model class files for the given model name and traits. |
||
| 358 | * |
||
| 359 | * @param string $modelName |
||
| 360 | * @param array $activeTraits |
||
| 361 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
||
| 362 | */ |
||
| 363 | private function createModels($modelName = 'Item', $activeTraits = []) |
||
| 364 | { |
||
| 365 | $modelClassName = $this->namespace('models', 'Models', $modelName); |
||
| 366 | |||
| 367 | $modelsDir = $this->isCapsule ? $this->capsule['models_dir'] : 'Models'; |
||
| 368 | |||
| 369 | $this->makeTwillDirectory($modelsDir); |
||
| 370 | |||
| 371 | if ($this->translatable) { |
||
| 372 | $this->makeTwillDirectory($baseDir = $this->isCapsule ? $modelsDir : "{$modelsDir}/Translations"); |
||
| 373 | |||
| 374 | $modelTranslationClassName = $modelName . 'Translation'; |
||
| 375 | |||
| 376 | $stub = str_replace( |
||
| 377 | ['{{modelTranslationClassName}}', '{{modelClassWithNamespace}}', '{{modelClassName}}', '{{namespace}}', '{{baseTranslationModel}}'], |
||
| 378 | [$modelTranslationClassName, $modelClassName, $modelName, $this->namespace('models', 'Models\Translations'), config('twill.base_translation_model')], |
||
| 379 | $this->files->get(__DIR__ . '/stubs/model_translation.stub') |
||
| 380 | ); |
||
| 381 | |||
| 382 | twill_put_stub(twill_path("{$baseDir}/" . $modelTranslationClassName . '.php'), $stub); |
||
| 383 | } |
||
| 384 | |||
| 385 | if ($this->sluggable) { |
||
| 386 | $this->makeTwillDirectory($baseDir = $this->isCapsule ? $modelsDir : "{$modelsDir}/Slugs"); |
||
| 387 | |||
| 388 | $modelSlugClassName = $modelName . 'Slug'; |
||
| 389 | |||
| 390 | $stub = str_replace( |
||
| 391 | ['{{modelSlugClassName}}', '{{modelClassWithNamespace}}', '{{modelName}}', '{{namespace}}', '{{baseSlugModel}}'], |
||
| 392 | [$modelSlugClassName, $modelClassName, Str::snake($modelName), $this->namespace('models', 'Models\Slugs'), config('twill.base_slug_model')], |
||
| 393 | $this->files->get(__DIR__ . '/stubs/model_slug.stub') |
||
| 394 | ); |
||
| 395 | |||
| 396 | twill_put_stub(twill_path("{$baseDir}/" . $modelSlugClassName . '.php'), $stub); |
||
| 397 | } |
||
| 398 | |||
| 399 | if ($this->revisionable) { |
||
| 400 | $this->makeTwillDirectory($baseDir = $this->isCapsule ? $modelsDir : "{$modelsDir}/Revisions"); |
||
| 401 | |||
| 402 | $modelRevisionClassName = $modelName . 'Revision'; |
||
| 403 | |||
| 404 | $stub = str_replace( |
||
| 405 | ['{{modelRevisionClassName}}', '{{modelClassWithNamespace}}', '{{modelName}}', '{{namespace}}', '{{baseRevisionModel}}'], |
||
| 406 | [$modelRevisionClassName, $modelClassName, Str::snake($modelName), $this->namespace('models', 'Models\Revisions'), config('twill.base_revision_model')], |
||
| 407 | $this->files->get(__DIR__ . '/stubs/model_revision.stub') |
||
| 408 | ); |
||
| 409 | |||
| 410 | twill_put_stub(twill_path("{$baseDir}/" . $modelRevisionClassName . '.php'), $stub); |
||
| 411 | } |
||
| 412 | |||
| 413 | $activeModelTraits = []; |
||
| 414 | |||
| 415 | foreach ($activeTraits as $index => $traitIsActive) { |
||
| 416 | if ($traitIsActive) { |
||
| 417 | !isset($this->modelTraits[$index]) ?: $activeModelTraits[] = $this->modelTraits[$index]; |
||
| 418 | } |
||
| 419 | } |
||
| 420 | |||
| 421 | $activeModelTraitsString = empty($activeModelTraits) ? '' : 'use ' . rtrim(implode(', ', $activeModelTraits), ', ') . ';'; |
||
| 422 | |||
| 423 | $activeModelTraitsImports = empty($activeModelTraits) ? '' : "use A17\Twill\Models\Behaviors\\" . implode(";\nuse A17\Twill\Models\Behaviors\\", $activeModelTraits) . ";"; |
||
| 424 | |||
| 425 | $activeModelImplements = $this->sortable ? 'implements Sortable' : ''; |
||
| 426 | |||
| 427 | if ($this->sortable) { |
||
| 428 | $activeModelTraitsImports .= "\nuse A17\Twill\Models\Behaviors\Sortable;"; |
||
| 429 | } |
||
| 430 | |||
| 431 | $stub = str_replace([ |
||
| 432 | '{{modelClassName}}', |
||
| 433 | '{{modelTraits}}', |
||
| 434 | '{{modelImports}}', |
||
| 435 | '{{modelImplements}}', |
||
| 436 | '{{namespace}}', |
||
| 437 | '{{baseModel}}', |
||
| 438 | ], [ |
||
| 439 | $modelName, |
||
| 440 | $activeModelTraitsString, |
||
| 441 | $activeModelTraitsImports, |
||
| 442 | $activeModelImplements, |
||
| 443 | $this->namespace('models', 'Models'), |
||
| 444 | config('twill.base_model'), |
||
| 445 | ], $this->files->get(__DIR__ . '/stubs/model.stub')); |
||
| 446 | |||
| 447 | $stub = $this->renderStubForOption($stub, 'hasTranslation', $this->translatable); |
||
| 448 | $stub = $this->renderStubForOption($stub, 'hasSlug', $this->sluggable); |
||
| 449 | $stub = $this->renderStubForOption($stub, 'hasMedias', $this->mediable); |
||
| 450 | $stub = $this->renderStubForOption($stub, 'hasPosition', $this->sortable); |
||
| 451 | |||
| 452 | twill_put_stub(twill_path("{$modelsDir}/" . $modelName . '.php'), $stub); |
||
| 453 | |||
| 454 | $this->info("Models created successfully! Fill your fillables!"); |
||
| 455 | } |
||
| 456 | |||
| 457 | private function renderStubForOption($stub, $option, $enabled) |
||
| 458 | { |
||
| 459 | if ($enabled) { |
||
| 460 | $stub = str_replace([ |
||
| 461 | '{{' . $option . '}}', |
||
| 462 | '{{/' . $option . '}}', |
||
| 463 | ], '', $stub); |
||
| 464 | } else { |
||
| 465 | $stub = preg_replace('/{{' . $option . '}}[\s\S]+?{{\/' . $option . '}}/', '', $stub); |
||
| 466 | } |
||
| 467 | |||
| 468 | return $stub; |
||
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Creates new repository class file for the given model name. |
||
| 473 | * |
||
| 474 | * @param string $modelName |
||
| 475 | * @param array $activeTraits |
||
| 476 | * @return void |
||
| 477 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
||
| 478 | */ |
||
| 479 | private function createRepository($modelName = 'Item', $activeTraits = []) |
||
| 480 | { |
||
| 481 | $modelsDir = $this->isCapsule ? $this->capsule['repositories_dir'] : 'Repositories'; |
||
| 482 | |||
| 483 | $modelClass = $this->isCapsule ? $this->capsule['model'] : config('twill.namespace') . "\Models\\{$this->capsule['singular']}"; |
||
| 484 | |||
| 485 | $this->makeTwillDirectory($modelsDir); |
||
| 486 | |||
| 487 | $repositoryClassName = $modelName . 'Repository'; |
||
| 488 | |||
| 489 | $activeRepositoryTraits = []; |
||
| 490 | |||
| 491 | foreach ($activeTraits as $index => $traitIsActive) { |
||
| 492 | if ($traitIsActive) { |
||
| 493 | !isset($this->repositoryTraits[$index]) ?: $activeRepositoryTraits[] = $this->repositoryTraits[$index]; |
||
| 494 | } |
||
| 495 | } |
||
| 496 | |||
| 497 | $activeRepositoryTraits = array_filter($activeRepositoryTraits); |
||
| 498 | |||
| 499 | $activeRepositoryTraitsString = empty($activeRepositoryTraits) ? '' : 'use ' . (empty($activeRepositoryTraits) ? "" : rtrim(implode(', ', $activeRepositoryTraits), ', ') . ';'); |
||
| 500 | |||
| 501 | $activeRepositoryTraitsImports = empty($activeRepositoryTraits) ? '' : "use A17\Twill\Repositories\Behaviors\\" . implode(";\nuse A17\Twill\Repositories\Behaviors\\", $activeRepositoryTraits) . ";"; |
||
| 502 | |||
| 503 | $stub = str_replace( |
||
| 504 | ['{{repositoryClassName}}', '{{modelName}}', '{{repositoryTraits}}', '{{repositoryImports}}', '{{namespace}}', '{{modelClass}}', '{{baseRepository}}'], |
||
| 505 | [$repositoryClassName, $modelName, $activeRepositoryTraitsString, $activeRepositoryTraitsImports, $this->namespace('repositories', 'Repositories'), $modelClass, config('twill.base_repository')], |
||
| 506 | $this->files->get(__DIR__ . '/stubs/repository.stub') |
||
| 507 | ); |
||
| 508 | |||
| 509 | twill_put_stub(twill_path("{$modelsDir}/" . $repositoryClassName . '.php'), $stub); |
||
| 510 | |||
| 511 | $this->info("Repository created successfully! Control all the things!"); |
||
| 512 | } |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Create a new controller class file for the given module name and model name. |
||
| 516 | * |
||
| 517 | * @param string $moduleName |
||
| 518 | * @param string $modelName |
||
| 519 | * @return void |
||
| 520 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
||
| 521 | */ |
||
| 522 | private function createController($moduleName = 'items', $modelName = 'Item') |
||
| 523 | { |
||
| 524 | $controllerClassName = $modelName . 'Controller'; |
||
| 525 | |||
| 526 | $dir = $this->isCapsule ? $this->capsule['controllers_dir'] : 'Http/Controllers/Admin'; |
||
| 527 | |||
| 528 | if ($this->isSingleton) { |
||
| 529 | $baseController = config('twill.base_singleton_controller'); |
||
| 530 | } elseif ($this->nestable) { |
||
| 531 | $baseController = config('twill.base_nested_controller'); |
||
| 532 | } else { |
||
| 533 | $baseController = config('twill.base_controller'); |
||
| 534 | } |
||
| 535 | |||
| 536 | $this->makeTwillDirectory($dir); |
||
| 537 | |||
| 538 | $stub = str_replace( |
||
| 539 | ['{{moduleName}}', '{{controllerClassName}}', '{{namespace}}', '{{baseController}}'], |
||
| 540 | [$moduleName, $controllerClassName, $this->namespace('controllers', 'Http\Controllers\Admin'), $baseController], |
||
| 541 | $this->files->get(__DIR__ . '/stubs/controller.stub') |
||
| 542 | ); |
||
| 543 | |||
| 544 | $permalinkOption = ''; |
||
| 545 | $reorderOption = ''; |
||
| 546 | |||
| 547 | if (!$this->sluggable) { |
||
| 548 | $permalinkOption = "'permalink' => false,"; |
||
| 549 | } |
||
| 550 | |||
| 551 | if ($this->nestable) { |
||
| 552 | $reorderOption = "'reorder' => true,"; |
||
| 553 | |||
| 554 | $stub = str_replace(['{{hasNesting}}', '{{/hasNesting}}'], '', $stub); |
||
| 555 | } else { |
||
| 556 | $stub = preg_replace('/{{hasNesting}}[\s\S]+?{{\/hasNesting}}/', '', $stub); |
||
| 557 | } |
||
| 558 | |||
| 559 | $stub = str_replace( |
||
| 560 | ['{{permalinkOption}}', '{{reorderOption}}'], |
||
| 561 | [$permalinkOption, $reorderOption], |
||
| 562 | $stub |
||
| 563 | ); |
||
| 564 | |||
| 565 | // Remove lines including only whitespace, leave true empty lines untouched |
||
| 566 | $stub = preg_replace('/^[\s]+\n/m', '', $stub); |
||
| 567 | |||
| 568 | twill_put_stub(twill_path("{$dir}/" . $controllerClassName . '.php'), $stub); |
||
| 569 | |||
| 570 | $this->info("Controller created successfully! Define your index/browser/form endpoints options!"); |
||
| 571 | } |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Creates a new request class file for the given model name. |
||
| 575 | * |
||
| 576 | * @param string $modelName |
||
| 577 | * @return void |
||
| 578 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
||
| 579 | */ |
||
| 580 | private function createRequest($modelName = 'Item') |
||
| 581 | { |
||
| 582 | $dir = $this->isCapsule ? $this->capsule['requests_dir'] : 'Http/Requests/Admin'; |
||
| 583 | |||
| 584 | $this->makeTwillDirectory($dir); |
||
| 585 | |||
| 586 | $requestClassName = $modelName . 'Request'; |
||
| 587 | |||
| 588 | $stub = str_replace( |
||
| 589 | ['{{requestClassName}}', '{{namespace}}', '{{baseRequest}}'], |
||
| 590 | [$requestClassName, $this->namespace('requests', 'Http\Requests\Admin'), config('twill.base_request')], |
||
| 591 | $this->files->get(__DIR__ . '/stubs/request.stub') |
||
| 592 | ); |
||
| 593 | |||
| 594 | twill_put_stub(twill_path("{$dir}/" . $requestClassName . '.php'), $stub); |
||
| 595 | |||
| 596 | $this->info("Form request created successfully! Add some validation rules!"); |
||
| 597 | } |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Creates appropriate module Blade view files. |
||
| 601 | * |
||
| 602 | * @param string $moduleName |
||
| 603 | * @return void |
||
| 604 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
||
| 605 | */ |
||
| 606 | private function createViews($moduleName = 'items') |
||
| 607 | { |
||
| 608 | $viewsPath = $this->viewPath($moduleName); |
||
| 609 | |||
| 610 | $this->makeTwillDirectory($viewsPath); |
||
| 611 | |||
| 612 | $formView = $this->translatable ? 'form_translatable' : 'form'; |
||
| 613 | |||
| 614 | twill_put_stub($viewsPath . '/form.blade.php', $this->files->get(__DIR__ . '/stubs/' . $formView . '.blade.stub')); |
||
| 615 | |||
| 616 | $this->info("Form view created successfully! Include your form fields using @formField directives!"); |
||
| 617 | } |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Creates a basic routes file for the Capsule. |
||
| 621 | * |
||
| 622 | * @param string $moduleName |
||
| 623 | * @return void |
||
| 624 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
||
| 625 | */ |
||
| 626 | public function createCapsuleRoutes() |
||
| 627 | { |
||
| 628 | $this->makeDir($this->capsule['routes_file']); |
||
| 629 | |||
| 630 | $contents = str_replace( |
||
| 631 | '{{moduleName}}', |
||
| 632 | $this->capsule['module'], |
||
| 633 | $this->files->get(__DIR__ . '/stubs/routes_admin.stub') |
||
| 634 | ); |
||
| 635 | |||
| 636 | twill_put_stub($this->capsule['routes_file'], $contents); |
||
| 637 | |||
| 638 | $this->info("Routes file created successfully!"); |
||
| 639 | } |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Creates a new capsule database seed file. |
||
| 643 | * |
||
| 644 | * @param string $moduleName |
||
| 645 | * @return void |
||
| 646 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
||
| 647 | */ |
||
| 648 | private function createCapsuleSeed($moduleName = 'items') |
||
| 649 | { |
||
| 650 | $this->makeTwillDirectory($this->capsule['seeds_psr4_path']); |
||
| 651 | |||
| 652 | $stub = $this->files->get(__DIR__ . '/stubs/database_seeder_capsule.stub'); |
||
| 653 | |||
| 654 | $stub = str_replace('{moduleName}', $this->capsule['plural'], $stub); |
||
| 655 | |||
| 656 | $this->files->put("{$this->capsule['seeds_psr4_path']}/DatabaseSeeder.php", $stub); |
||
| 657 | |||
| 658 | $this->info("Seed created successfully!"); |
||
| 659 | } |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Creates a new singleton module database seed file. |
||
| 663 | * |
||
| 664 | * @param string $modelName |
||
| 665 | * @return void |
||
| 666 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
||
| 667 | */ |
||
| 668 | private function createSingletonSeed($modelName = 'Item') |
||
| 669 | { |
||
| 670 | $repositoryName = $modelName . 'Repository'; |
||
| 671 | $seederName = $modelName . 'Seeder'; |
||
| 672 | |||
| 673 | $dir = $this->databasePath('seeders'); |
||
| 674 | |||
| 675 | $this->makeTwillDirectory($dir); |
||
| 676 | |||
| 677 | $stub = $this->files->get(__DIR__ . '/stubs/database_seeder_singleton.stub'); |
||
| 678 | |||
| 679 | $stub = $this->replaceVariables([ |
||
| 680 | 'seederClassName' => $seederName, |
||
| 681 | 'modelClassName' => $modelName, |
||
| 682 | 'repositoryClassName' => $repositoryName, |
||
| 683 | ], $stub); |
||
| 684 | |||
| 685 | $stub = $this->replaceConditionals([ |
||
| 686 | 'hasTranslations' => $this->translatable, |
||
| 687 | '!hasTranslations' => !$this->translatable, |
||
| 688 | ], $stub); |
||
| 689 | |||
| 690 | $stub = $this->removeEmptyLinesWithOnlySpaces($stub); |
||
| 691 | |||
| 692 | $this->files->put("{$dir}/{$seederName}.php", $stub); |
||
| 693 | |||
| 694 | $this->info("Seed created successfully!"); |
||
| 695 | } |
||
| 696 | |||
| 697 | private function checkOption($option) |
||
| 698 | { |
||
| 699 | if (!$this->hasOption($option)) { |
||
| 700 | return false; |
||
| 701 | } |
||
| 702 | |||
| 703 | if ($this->option($option) || $this->option('all')) { |
||
| 704 | return true; |
||
| 705 | } |
||
| 706 | |||
| 707 | $questions = [ |
||
| 708 | 'hasBlocks' => 'Do you need to use the block editor on this module?', |
||
| 709 | 'hasTranslation' => 'Do you need to translate content on this module?', |
||
| 710 | 'hasSlug' => 'Do you need to generate slugs on this module?', |
||
| 711 | 'hasMedias' => 'Do you need to attach images on this module?', |
||
| 712 | 'hasFiles' => 'Do you need to attach files on this module?', |
||
| 713 | 'hasPosition' => 'Do you need to manage the position of records on this module?', |
||
| 714 | 'hasRevisions' => 'Do you need to enable revisions on this module?', |
||
| 715 | 'hasNesting' => 'Do you need to enable nesting on this module?', |
||
| 716 | ]; |
||
| 717 | |||
| 718 | $defaultAnswers = [ |
||
| 719 | 'hasNesting' => 0, |
||
| 720 | ]; |
||
| 721 | |||
| 722 | $currentDefaultAnswer = $this->defaultsAnswserToNo ? 0 : ($defaultAnswers[$option] ?? 1); |
||
| 723 | |||
| 724 | return 'yes' === $this->choice($questions[$option], ['no', 'yes'], $currentDefaultAnswer); |
||
| 725 | } |
||
| 726 | |||
| 727 | public function createCapsulePath($moduleName, $modelName) |
||
| 728 | { |
||
| 729 | if (!$this->isCapsule) { |
||
| 730 | $this->moduleBasePath = base_path(); |
||
| 731 | |||
| 732 | return; |
||
| 733 | } |
||
| 734 | |||
| 735 | $this->checkCapsuleDirectory( |
||
| 736 | $this->moduleBasePath = config('twill.capsules.path') . "/{$moduleName}" |
||
| 737 | ); |
||
| 738 | |||
| 739 | $this->makeDir($this->moduleBasePath); |
||
| 740 | } |
||
| 741 | |||
| 742 | public function createCapsuleNamespace($module, $model) |
||
| 743 | { |
||
| 744 | $base = config('twill.capsules.namespace'); |
||
| 745 | |||
| 746 | $this->capsuleNamespace = "{$base}\\{$module}"; |
||
| 747 | } |
||
| 748 | |||
| 749 | public function databasePath($path = '') |
||
| 756 | } |
||
| 757 | |||
| 758 | public function makeDir($dir) |
||
| 759 | { |
||
| 760 | $info = pathinfo($dir); |
||
| 761 | |||
| 762 | $dir = isset($info['extension']) ? $info['dirname'] : $dir; |
||
| 763 | |||
| 764 | if (!is_dir($dir)) { |
||
| 765 | mkdir($dir, 0755, true); |
||
| 766 | } |
||
| 767 | |||
| 768 | if (!is_dir($dir)) { |
||
| 769 | $this->info("It wasn't possible to create capsule directory {$dir}"); |
||
| 770 | |||
| 771 | die; |
||
| 772 | } |
||
| 773 | } |
||
| 774 | |||
| 775 | public function makeTwillDirectory($path) |
||
| 776 | { |
||
| 777 | make_twill_directory($path); |
||
| 778 | } |
||
| 779 | |||
| 780 | public function namespace ($type, $suffix, $class = null) { |
||
| 788 | } |
||
| 789 | |||
| 790 | public function viewPath($moduleName) |
||
| 791 | { |
||
| 792 | if (!$this->isCapsule) { |
||
| 799 | } |
||
| 800 | |||
| 801 | /** |
||
| 802 | * @param array $variables |
||
| 803 | * @param string $stub |
||
| 804 | * @param array|null $delimiters |
||
| 805 | * @return string |
||
| 806 | */ |
||
| 807 | public function replaceVariables($variables, $stub, $delimiters = null) |
||
| 808 | { |
||
| 809 | $delimiters = $delimiters ?: ['{{', '}}']; |
||
| 810 | |||
| 811 | foreach ($variables as $key => $value) { |
||
| 812 | $key = "{$delimiters[0]}{$key}{$delimiters[1]}"; |
||
| 813 | |||
| 814 | $stub = str_replace($key, $value, $stub); |
||
| 815 | } |
||
| 816 | |||
| 817 | return $stub; |
||
| 818 | } |
||
| 819 | |||
| 820 | /** |
||
| 821 | * @param array $variables |
||
| 822 | * @param string $stub |
||
| 823 | * @param array|null $delimiters |
||
| 824 | * @return string |
||
| 825 | */ |
||
| 826 | public function replaceConditionals($conditionals, $stub, $delimiters = null) |
||
| 846 | } |
||
| 847 | |||
| 848 | /** |
||
| 849 | * @param string $stub |
||
| 850 | * @return string |
||
| 851 | */ |
||
| 852 | public function removeEmptyLinesWithOnlySpaces($stub) |
||
| 855 | } |
||
| 856 | } |
||
| 857 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.