| Total Complexity | 66 |
| Total Lines | 1121 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like MultiAuthPrepare 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 MultiAuthPrepare, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class MultiAuthPrepare extends BaseCommand |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var |
||
| 16 | */ |
||
| 17 | protected $progressBar; |
||
| 18 | /** |
||
| 19 | * The name and signature of the console command. |
||
| 20 | * |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected $signature = 'make:multi-backpack {name} {--admin_theme= : chose the theme you want}'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The console command description. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $description = 'create multi authentication guards with backpack panel'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The migration creator instance. |
||
| 34 | * |
||
| 35 | * @var \Illuminate\Database\Migrations\MigrationCreator |
||
| 36 | */ |
||
| 37 | protected $creator; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The Composer instance. |
||
| 41 | * |
||
| 42 | * @var \Illuminate\Support\Composer |
||
| 43 | */ |
||
| 44 | protected $composer; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Create a new migration install command instance. |
||
| 48 | * |
||
| 49 | * @param \Illuminate\Database\Migrations\MigrationCreator $creator |
||
| 50 | * @param \Illuminate\Support\Composer $composer |
||
| 51 | */ |
||
| 52 | public function __construct(MigrationCreator $creator, Composer $composer) |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Execute the console command. |
||
| 61 | * |
||
| 62 | * @return boolean |
||
| 63 | */ |
||
| 64 | public function handle() |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Publish Prologue Alert |
||
| 150 | * |
||
| 151 | * @return boolean |
||
| 152 | */ |
||
| 153 | public function installPrologueAlert() { |
||
| 154 | $alertsConfigFile = $this->getConfigsFolderPath().DIRECTORY_SEPARATOR."prologue/alerts.php"; |
||
| 155 | if (!file_exists($alertsConfigFile)) { |
||
| 156 | $this->executeProcess('php artisan vendor:publish --provider="Prologue\Alerts\AlertsServiceProvider"', |
||
| 157 | 'publishing config for notifications - prologue/alerts'); |
||
| 158 | } |
||
| 159 | |||
| 160 | return true; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Install Migration. |
||
| 165 | * |
||
| 166 | * @return boolean |
||
| 167 | */ |
||
| 168 | public function installMigration() |
||
| 169 | { |
||
| 170 | $nameSmallPlural = str_plural(snake_case($this->getParsedNameInput())); |
||
| 171 | $name = ucfirst($this->getParsedNameInput()); |
||
| 172 | $namePlural = str_plural($name); |
||
| 173 | |||
| 174 | |||
| 175 | |||
| 176 | $modelTableContent = file_get_contents(__DIR__ . '/../Migration/modelTable.stub'); |
||
| 177 | $modelTableContentNew = str_replace([ |
||
| 178 | '{{$namePlural}}', |
||
| 179 | '{{$nameSmallPlural}}', |
||
| 180 | ], [ |
||
| 181 | $namePlural, |
||
| 182 | $nameSmallPlural |
||
| 183 | ], $modelTableContent); |
||
| 184 | |||
| 185 | |||
| 186 | $modelResetPasswordTableContent = file_get_contents(__DIR__ . '/../Migration/passwordResetsTable.stub'); |
||
| 187 | $modelResetPasswordTableContentNew = str_replace([ |
||
| 188 | '{{$namePlural}}', |
||
| 189 | '{{$nameSmallPlural}}', |
||
| 190 | ], [ |
||
| 191 | $namePlural, |
||
| 192 | $nameSmallPlural |
||
| 193 | ], $modelResetPasswordTableContent); |
||
| 194 | |||
| 195 | |||
| 196 | $migrationName = date('Y_m_d_His') . '_'.'create_' . str_plural(snake_case($name)) .'_table.php'; |
||
| 197 | $migrationModelPath = $this->getMigrationPath().DIRECTORY_SEPARATOR.$migrationName; |
||
| 198 | file_put_contents($migrationModelPath, $modelTableContentNew); |
||
| 199 | |||
| 200 | $migrationResetName = date('Y_m_d_His') . '_' |
||
| 201 | .'create_' . str_plural(snake_case($name)) |
||
| 202 | .'_password_resets_table.php'; |
||
| 203 | $migrationResetModelPath = $this->getMigrationPath().DIRECTORY_SEPARATOR.$migrationResetName; |
||
| 204 | file_put_contents($migrationResetModelPath, $modelResetPasswordTableContentNew); |
||
| 205 | |||
| 206 | return true; |
||
| 207 | |||
| 208 | } |
||
| 209 | |||
| 210 | |||
| 211 | /** |
||
| 212 | * Install Model. |
||
| 213 | * |
||
| 214 | * @return boolean |
||
| 215 | */ |
||
| 216 | public function installModel() |
||
| 278 | |||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Install View. |
||
| 283 | * |
||
| 284 | * @param string $theme_name |
||
| 285 | * @return bool |
||
| 286 | */ |
||
| 287 | public function installView($theme_name = 'adminlte') |
||
| 288 | { |
||
| 289 | if (file_exists(__DIR__ . '/../Backpack/Views/'.$theme_name)) { |
||
| 290 | $nameSmall = snake_case($this->getParsedNameInput()); |
||
| 291 | $name = ucfirst($this->getParsedNameInput()); |
||
| 292 | |||
| 293 | // layouts |
||
| 294 | $appBlade = file_get_contents(__DIR__ . '/../Backpack/Views/'.$theme_name.'/layouts/layout.blade.stub'); |
||
| 295 | $appGuestBlade = file_get_contents(__DIR__ . '/../Backpack/Views/'.$theme_name.'/layouts/layout_guest.blade.stub'); |
||
| 296 | |||
| 297 | // home |
||
| 298 | $homeBlade = file_get_contents(__DIR__ . '/../Backpack/Views/'.$theme_name.'/home.blade.stub'); |
||
| 299 | |||
| 300 | // auth |
||
| 301 | $loginBlade = file_get_contents(__DIR__ . '/../Backpack/Views/'.$theme_name.'/auth/login.blade.stub'); |
||
| 302 | $registerBlade = file_get_contents(__DIR__ . '/../Backpack/Views/'.$theme_name.'/auth/register.blade.stub'); |
||
| 303 | $verifyEmailBlade = file_get_contents(__DIR__ . '/../Backpack/Views/'.$theme_name.'/auth/verify.blade.stub'); |
||
| 304 | |||
| 305 | // auth/passwords |
||
| 306 | $resetBlade = file_get_contents(__DIR__ . '/../Backpack/Views/'.$theme_name.'/auth/passwords/reset.blade.stub'); |
||
| 307 | $emailBlade = file_get_contents(__DIR__ . '/../Backpack/Views/'.$theme_name.'/auth/passwords/email.blade.stub'); |
||
| 308 | |||
| 309 | // auth/account |
||
| 310 | $update_infoBlade = file_get_contents(__DIR__ |
||
| 311 | . '/../Backpack/Views/'.$theme_name.'/auth/account/update_info.blade.stub'); |
||
| 312 | $change_passwordBlade = file_get_contents(__DIR__ |
||
| 313 | . '/../Backpack/Views/'.$theme_name.'/auth/account/change_password.blade.stub'); |
||
| 314 | $sidemenuBlade = file_get_contents(__DIR__ |
||
| 315 | . '/../Backpack/Views/'.$theme_name.'/auth/account/sidemenu.blade.stub'); |
||
| 316 | |||
| 317 | // inc |
||
| 318 | $main_headerBlade = file_get_contents(__DIR__ |
||
| 319 | . '/../Backpack/Views/'.$theme_name.'/inc/main_header.blade.stub'); |
||
| 320 | $sidebarBlade = file_get_contents(__DIR__ |
||
| 321 | . '/../Backpack/Views/'.$theme_name.'/inc/sidebar.blade.stub'); |
||
| 322 | $alertsBlade = file_get_contents(__DIR__ |
||
| 323 | . '/../Backpack/Views/'.$theme_name.'/inc/alerts.blade.stub'); |
||
| 324 | $footerBlade = file_get_contents(__DIR__ |
||
| 325 | . '/../Backpack/Views/'.$theme_name.'/inc/footer.blade.stub'); |
||
| 326 | $footerGuestBlade = file_get_contents(__DIR__ |
||
| 327 | . '/../Backpack/Views/'.$theme_name.'/inc/footer_guest.blade.stub'); |
||
| 328 | $headBlade = file_get_contents(__DIR__ |
||
| 329 | . '/../Backpack/Views/'.$theme_name.'/inc/head.blade.stub'); |
||
| 330 | $scriptsBlade = file_get_contents(__DIR__ |
||
| 331 | . '/../Backpack/Views/'.$theme_name.'/inc/scripts.blade.stub'); |
||
| 332 | |||
| 333 | $menuBlade = null; |
||
| 334 | $account_infoBlade = null; |
||
| 335 | $authCSS = null; |
||
| 336 | $breadcrumbBlade = null; |
||
| 337 | $notifications_menuBlade = null; |
||
| 338 | |||
| 339 | |||
| 340 | if ($theme_name == 'adminlte') { |
||
| 341 | $menuBlade = file_get_contents(__DIR__ |
||
| 342 | . '/../Backpack/Views/'.$theme_name.'/inc/menu.blade.stub'); |
||
| 343 | $sidebar_user_panelBlade = file_get_contents(__DIR__ |
||
| 344 | . '/../Backpack/Views/'.$theme_name.'/inc/sidebar_user_panel.blade.stub'); |
||
| 345 | // style |
||
| 346 | $authCSS = file_get_contents(__DIR__ |
||
| 347 | . '/../Backpack/Views/'.$theme_name.'/style/backpack_auth_css.blade.stub'); |
||
| 348 | } else { |
||
| 349 | $account_infoBlade = file_get_contents(__DIR__ |
||
| 350 | . '/../Backpack/Views/'.$theme_name.'/auth/account/account_info.blade.stub'); |
||
| 351 | $sidebar_user_panelBlade = file_get_contents(__DIR__ |
||
| 352 | . '/../Backpack/Views/'.$theme_name.'/inc/user_menu.blade.stub'); |
||
| 353 | $breadcrumbBlade = file_get_contents(__DIR__ |
||
| 354 | . '/../Backpack/Views/'.$theme_name.'/inc/breadcrumb.blade.stub'); |
||
| 355 | $notifications_menuBlade = file_get_contents(__DIR__ |
||
| 356 | . '/../Backpack/Views/'.$theme_name.'/inc/notifications_menu.blade.stub'); |
||
| 357 | } |
||
| 358 | |||
| 359 | |||
| 360 | |||
| 361 | |||
| 362 | $createFolder = $this->getViewsFolderPath().DIRECTORY_SEPARATOR."$nameSmall"; |
||
| 363 | if (!file_exists($createFolder)) { |
||
| 364 | mkdir($createFolder); |
||
| 365 | } |
||
| 366 | |||
| 367 | $createFolderLayouts = $this->getViewsFolderPath().DIRECTORY_SEPARATOR |
||
| 368 | ."$nameSmall" |
||
| 369 | .DIRECTORY_SEPARATOR."layouts"; |
||
| 370 | if (!file_exists($createFolderLayouts)) { |
||
| 371 | mkdir($createFolderLayouts); |
||
| 372 | } |
||
| 373 | |||
| 374 | $createFolderStyle = $this->getViewsFolderPath().DIRECTORY_SEPARATOR |
||
| 375 | ."$nameSmall" |
||
| 376 | .DIRECTORY_SEPARATOR."style"; |
||
| 377 | if (!file_exists($createFolderStyle)) { |
||
| 378 | mkdir($createFolderStyle); |
||
| 379 | } |
||
| 380 | |||
| 381 | $createFolderInc = $this->getViewsFolderPath().DIRECTORY_SEPARATOR |
||
| 382 | ."$nameSmall" |
||
| 383 | .DIRECTORY_SEPARATOR."inc"; |
||
| 384 | if (!file_exists($createFolderInc)) { |
||
| 385 | mkdir($createFolderInc); |
||
| 386 | } |
||
| 387 | |||
| 388 | $createFolderAuth = $this->getViewsFolderPath().DIRECTORY_SEPARATOR."$nameSmall" |
||
| 389 | .DIRECTORY_SEPARATOR."auth"; |
||
| 390 | if (!file_exists($createFolderAuth)) { |
||
| 391 | mkdir($createFolderAuth); |
||
| 392 | } |
||
| 393 | |||
| 394 | $createFolderAuthPasswords = $this->getViewsFolderPath().DIRECTORY_SEPARATOR. |
||
| 395 | "$nameSmall".DIRECTORY_SEPARATOR |
||
| 396 | ."auth".DIRECTORY_SEPARATOR."passwords"; |
||
| 397 | if (!file_exists($createFolderAuthPasswords)) { |
||
| 398 | mkdir($createFolderAuthPasswords); |
||
| 399 | } |
||
| 400 | |||
| 401 | $createFolderAuthAccount = $this->getViewsFolderPath().DIRECTORY_SEPARATOR. |
||
| 402 | "$nameSmall".DIRECTORY_SEPARATOR |
||
| 403 | ."auth".DIRECTORY_SEPARATOR."account"; |
||
| 404 | if (!file_exists($createFolderAuthAccount)) { |
||
| 405 | mkdir($createFolderAuthAccount); |
||
| 406 | } |
||
| 407 | |||
| 408 | $headBladeNew = str_replace([ |
||
| 409 | '{{$nameSmall}}', |
||
| 410 | ], [ |
||
| 411 | $nameSmall |
||
| 412 | ], $headBlade); |
||
| 413 | |||
| 414 | $appBladeNew = str_replace([ |
||
| 415 | '{{$nameSmall}}', |
||
| 416 | ], [ |
||
| 417 | $nameSmall |
||
| 418 | ], $appBlade); |
||
| 419 | |||
| 420 | $appGuestBladeNew = str_replace([ |
||
| 421 | '{{$nameSmall}}', |
||
| 422 | ], [ |
||
| 423 | $nameSmall |
||
| 424 | ], $appGuestBlade); |
||
| 425 | |||
| 426 | $verifyEmailBladeNew = str_replace([ |
||
| 427 | '{{$nameSmall}}', |
||
| 428 | ], [ |
||
| 429 | $nameSmall |
||
| 430 | ], $verifyEmailBlade); |
||
| 431 | |||
| 432 | $homeBladeNew = str_replace([ |
||
| 433 | '{{$nameSmall}}', |
||
| 434 | |||
| 435 | ], [ |
||
| 436 | $nameSmall |
||
| 437 | ], $homeBlade); |
||
| 438 | |||
| 439 | $loginBladeNew = str_replace([ |
||
| 440 | '{{$nameSmall}}', |
||
| 441 | ], [ |
||
| 442 | $nameSmall |
||
| 443 | ], $loginBlade); |
||
| 444 | |||
| 445 | $registerBladeNew = str_replace([ |
||
| 446 | '{{$nameSmall}}', |
||
| 447 | ], [ |
||
| 448 | $nameSmall |
||
| 449 | ], $registerBlade); |
||
| 450 | |||
| 451 | $emailBladeNew = str_replace([ |
||
| 452 | '{{$nameSmall}}', |
||
| 453 | ], [ |
||
| 454 | $nameSmall |
||
| 455 | ], $emailBlade); |
||
| 456 | |||
| 457 | $resetBladeNew = str_replace([ |
||
| 458 | '{{$nameSmall}}', |
||
| 459 | ], [ |
||
| 460 | $nameSmall |
||
| 461 | ], $resetBlade); |
||
| 462 | |||
| 463 | $update_infoBladeNew = str_replace([ |
||
| 464 | '{{$nameSmall}}', |
||
| 465 | '{{$name}}', |
||
| 466 | ], [ |
||
| 467 | $nameSmall, |
||
| 468 | $name |
||
| 469 | ], $update_infoBlade); |
||
| 470 | |||
| 471 | $change_passwordBladeNew = str_replace([ |
||
| 472 | '{{$nameSmall}}', |
||
| 473 | ], [ |
||
| 474 | $nameSmall, |
||
| 475 | ], $change_passwordBlade); |
||
| 476 | |||
| 477 | $account_infoBladeNew = null; |
||
| 478 | if ($theme_name != 'adminlte') { |
||
| 479 | $account_infoBladeNew = str_replace([ |
||
| 480 | '{{$nameSmall}}', |
||
| 481 | ], [ |
||
| 482 | $nameSmall, |
||
| 483 | ], $account_infoBlade); |
||
| 484 | } |
||
| 485 | |||
| 486 | |||
| 487 | |||
| 488 | $sidemenuBladeNew = str_replace([ |
||
| 489 | '{{$nameSmall}}', |
||
| 490 | ], [ |
||
| 491 | $nameSmall |
||
| 492 | ], $sidemenuBlade); |
||
| 493 | |||
| 494 | $main_headerBladeNew = str_replace([ |
||
| 495 | '{{$nameSmall}}', |
||
| 496 | ], [ |
||
| 497 | $nameSmall |
||
| 498 | ], $main_headerBlade); |
||
| 499 | |||
| 500 | $menuBladeNew = null; |
||
| 501 | if ($theme_name == 'adminlte') { |
||
| 502 | $menuBladeNew = str_replace([ |
||
| 503 | '{{$nameSmall}}', |
||
| 504 | ], [ |
||
| 505 | $nameSmall |
||
| 506 | ], $menuBlade); |
||
| 507 | } |
||
| 508 | |||
| 509 | |||
| 510 | $sidebarBladeNew = str_replace([ |
||
| 511 | '{{$nameSmall}}', |
||
| 512 | ], [ |
||
| 513 | $nameSmall |
||
| 514 | ], $sidebarBlade); |
||
| 515 | |||
| 516 | $sidebar_user_panelBladeNew = str_replace([ |
||
| 517 | '{{$nameSmall}}', |
||
| 518 | ], [ |
||
| 519 | $nameSmall |
||
| 520 | ], $sidebar_user_panelBlade); |
||
| 521 | |||
| 522 | |||
| 523 | file_put_contents($createFolderLayouts.'/layout.blade.php', $appBladeNew); |
||
| 524 | file_put_contents($createFolderLayouts.'/layout_guest.blade.php', $appGuestBladeNew); |
||
| 525 | file_put_contents($createFolder.'/home.blade.php', $homeBladeNew); |
||
| 526 | |||
| 527 | |||
| 528 | file_put_contents($createFolderInc.'/main_header.blade.php', $main_headerBladeNew); |
||
| 529 | file_put_contents($createFolderInc.'/sidebar.blade.php', $sidebarBladeNew); |
||
| 530 | |||
| 531 | file_put_contents($createFolderInc.'/alerts.blade.php', $alertsBlade); |
||
| 532 | file_put_contents($createFolderInc.'/footer.blade.php', $footerBlade); |
||
| 533 | file_put_contents($createFolderInc.'/footer_guest.blade.php', $footerGuestBlade); |
||
| 534 | file_put_contents($createFolderInc.'/head.blade.php', $headBladeNew); |
||
| 535 | file_put_contents($createFolderInc.'/scripts.blade.php', $scriptsBlade); |
||
| 536 | |||
| 537 | file_put_contents($createFolderAuth.'/login.blade.php', $loginBladeNew); |
||
| 538 | file_put_contents($createFolderAuth.'/verify.blade.php', $verifyEmailBladeNew); |
||
| 539 | file_put_contents($createFolderAuth.'/register.blade.php', $registerBladeNew); |
||
| 540 | file_put_contents($createFolderAuthPasswords.'/email.blade.php', $emailBladeNew); |
||
| 541 | file_put_contents($createFolderAuthPasswords.'/reset.blade.php', $resetBladeNew); |
||
| 542 | |||
| 543 | file_put_contents($createFolderAuthAccount.'/sidemenu.blade.php', $sidemenuBladeNew); |
||
| 544 | file_put_contents($createFolderAuthAccount.'/update_info.blade.php', $update_infoBladeNew); |
||
| 545 | file_put_contents($createFolderAuthAccount.'/change_password.blade.php', $change_passwordBladeNew); |
||
| 546 | |||
| 547 | if ($theme_name == 'adminlte') { |
||
| 548 | file_put_contents($createFolderStyle.'/backpack_auth_css.blade.php', $authCSS); |
||
| 549 | file_put_contents($createFolderInc.'/menu.blade.php', $menuBladeNew); |
||
| 550 | file_put_contents($createFolderInc.'/sidebar_user_panel.blade.php', $sidebar_user_panelBladeNew); |
||
| 551 | } else { |
||
| 552 | file_put_contents($createFolderAuthAccount.'/account_info.blade.php', $account_infoBladeNew); |
||
| 553 | file_put_contents($createFolderInc.'/user_menu.blade.php', $sidebar_user_panelBladeNew); |
||
| 554 | file_put_contents($createFolderInc.'/breadcrumb.blade.php', $breadcrumbBlade); |
||
| 555 | file_put_contents($createFolderInc.'/notifications_menu.blade.php', $notifications_menuBlade); |
||
| 556 | } |
||
| 557 | |||
| 558 | |||
| 559 | return true; |
||
| 560 | } |
||
| 561 | return false; |
||
| 562 | |||
| 563 | } |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Install RouteMaps. |
||
| 567 | * |
||
| 568 | * @return boolean |
||
| 569 | */ |
||
| 570 | |||
| 571 | public function installRouteMaps() |
||
| 572 | { |
||
| 573 | $nameSmall = snake_case($this->getParsedNameInput()); |
||
| 574 | $name = ucfirst($this->getParsedNameInput()); |
||
| 575 | $mapCallFunction = file_get_contents(__DIR__ . '/../Route/mapRoute.stub'); |
||
| 576 | $mapCallFunctionNew = str_replace('{{$name}}', "$name", $mapCallFunction); |
||
| 577 | $this->insert($this->getRouteServicesPath(), '$this->mapWebRoutes();', $mapCallFunctionNew, true); |
||
| 578 | $mapFunction = file_get_contents(__DIR__ . '/../Route/mapRouteFunction.stub'); |
||
| 579 | $mapFunctionNew = str_replace([ |
||
| 580 | '{{$name}}', |
||
| 581 | '{{$nameSmall}}' |
||
| 582 | ], [ |
||
| 583 | "$name", |
||
| 584 | "$nameSmall" |
||
| 585 | ], $mapFunction); |
||
| 586 | $this->insert($this->getRouteServicesPath(), ' // |
||
| 587 | }', $mapFunctionNew, true); |
||
| 588 | return true; |
||
| 589 | } |
||
| 590 | |||
| 591 | public function isAlreadySetup() { |
||
| 592 | $name = ucfirst($this->getParsedNameInput()); |
||
| 593 | |||
| 594 | $routeServicesContent = file_get_contents($this->getRouteServicesPath()); |
||
| 595 | |||
| 596 | if (str_contains($routeServicesContent,'$this->map'.$name.'Routes();')) { |
||
| 597 | return true; |
||
| 598 | } |
||
| 599 | return false; |
||
| 600 | } |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Install RouteFile. |
||
| 604 | * |
||
| 605 | * @return boolean |
||
| 606 | */ |
||
| 607 | |||
| 608 | public function installRouteFiles() |
||
| 627 | } |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Install Requests. |
||
| 631 | * |
||
| 632 | * @return boolean |
||
| 633 | */ |
||
| 634 | |||
| 635 | public function installRequests() |
||
| 636 | { |
||
| 637 | $nameSmall = snake_case($this->getParsedNameInput()); |
||
| 638 | $name = ucfirst($this->getParsedNameInput()); |
||
| 639 | |||
| 640 | $nameFolder = $this->getControllersPath().DIRECTORY_SEPARATOR.$name; |
||
| 641 | if (!file_exists($nameFolder)) { |
||
| 642 | mkdir($nameFolder); |
||
| 643 | } |
||
| 644 | |||
| 645 | $requestsFolder = $nameFolder.DIRECTORY_SEPARATOR."Requests"; |
||
| 646 | if (!file_exists($requestsFolder)) { |
||
| 647 | mkdir($requestsFolder); |
||
| 648 | } |
||
| 649 | $accountInfoContent = file_get_contents(__DIR__ . '/../Request/AccountInfoRequest.stub'); |
||
| 650 | $changePasswordContent = file_get_contents(__DIR__ . '/../Request/ChangePasswordRequest.stub'); |
||
| 651 | |||
| 652 | $accountInfoContentNew = str_replace([ |
||
| 653 | '{{$name}}', |
||
| 654 | '{{$nameSmall}}' |
||
| 655 | ], [ |
||
| 656 | "$name", |
||
| 657 | "$nameSmall" |
||
| 658 | ], $accountInfoContent); |
||
| 659 | |||
| 660 | $changePasswordContentNew = str_replace([ |
||
| 661 | '{{$name}}', |
||
| 662 | '{{$nameSmall}}' |
||
| 663 | ], [ |
||
| 664 | "$name", |
||
| 665 | "$nameSmall" |
||
| 666 | ], $changePasswordContent); |
||
| 667 | |||
| 668 | $accountInfoFile = $requestsFolder.DIRECTORY_SEPARATOR."{$name}AccountInfoRequest.php"; |
||
| 669 | $changePasswordFile = $requestsFolder.DIRECTORY_SEPARATOR."{$name}ChangePasswordRequest.php"; |
||
| 670 | |||
| 671 | file_put_contents($accountInfoFile, $accountInfoContentNew); |
||
| 672 | file_put_contents($changePasswordFile, $changePasswordContentNew); |
||
| 673 | |||
| 674 | return true; |
||
| 675 | |||
| 676 | } |
||
| 677 | /** |
||
| 678 | * Install Controller. |
||
| 679 | * |
||
| 680 | * @return boolean |
||
| 681 | */ |
||
| 682 | |||
| 683 | public function installControllers() |
||
| 684 | { |
||
| 685 | $nameSmall = snake_case($this->getParsedNameInput()); |
||
| 686 | $nameSmallPlural = str_plural(snake_case($this->getParsedNameInput())); |
||
| 687 | $name = ucfirst($this->getParsedNameInput()); |
||
| 688 | |||
| 689 | $nameFolder = $this->getControllersPath().DIRECTORY_SEPARATOR.$name; |
||
| 690 | if (!file_exists($nameFolder)) { |
||
| 691 | mkdir($nameFolder); |
||
| 692 | } |
||
| 693 | |||
| 694 | $authFolder = $nameFolder.DIRECTORY_SEPARATOR."Auth"; |
||
| 695 | if (!file_exists($authFolder)) { |
||
| 696 | mkdir($authFolder); |
||
| 697 | } |
||
| 698 | |||
| 699 | $controllerContent = file_get_contents(__DIR__ . '/../Controllers/Controller.stub'); |
||
| 700 | $homeControllerContent = file_get_contents(__DIR__ . '/../Controllers/HomeController.stub'); |
||
| 701 | $loginControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/LoginController.stub'); |
||
| 702 | $forgotControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/ForgotPasswordController.stub'); |
||
| 703 | $registerControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/RegisterController.stub'); |
||
| 704 | $resetControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/ResetPasswordController.stub'); |
||
| 705 | $myAccountControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/MyAccountController.stub'); |
||
| 706 | $verificationControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/VerificationController.stub'); |
||
| 707 | |||
| 708 | $controllerFileContentNew = str_replace('{{$name}}', "$name", $controllerContent); |
||
| 709 | |||
| 710 | $homeFileContentNew = str_replace([ |
||
| 711 | '{{$name}}', |
||
| 712 | '{{$nameSmall}}' |
||
| 713 | ], [ |
||
| 714 | "$name", |
||
| 715 | "$nameSmall" |
||
| 716 | ], $homeControllerContent); |
||
| 717 | |||
| 718 | $loginFileContentNew = str_replace([ |
||
| 719 | '{{$name}}', |
||
| 720 | '{{$nameSmall}}' |
||
| 721 | ], [ |
||
| 722 | "$name", |
||
| 723 | "$nameSmall" |
||
| 724 | ], $loginControllerContent); |
||
| 725 | |||
| 726 | $forgotFileContentNew = str_replace([ |
||
| 727 | '{{$name}}', |
||
| 728 | '{{$nameSmall}}', |
||
| 729 | '{{$nameSmallPlural}}' |
||
| 730 | ], [ |
||
| 731 | "$name", |
||
| 732 | "$nameSmall", |
||
| 733 | "$nameSmallPlural" |
||
| 734 | ], $forgotControllerContent); |
||
| 735 | |||
| 736 | $registerFileContentNew = str_replace([ |
||
| 737 | '{{$name}}', |
||
| 738 | '{{$nameSmall}}', |
||
| 739 | '{{$nameSmallPlural}}' |
||
| 740 | ], [ |
||
| 741 | "$name", |
||
| 742 | "$nameSmall", |
||
| 743 | "$nameSmallPlural" |
||
| 744 | ], $registerControllerContent); |
||
| 745 | |||
| 746 | $resetFileContentNew = str_replace([ |
||
| 747 | '{{$name}}', |
||
| 748 | '{{$nameSmall}}', |
||
| 749 | '{{$nameSmallPlural}}' |
||
| 750 | ], [ |
||
| 751 | "$name", |
||
| 752 | "$nameSmall", |
||
| 753 | "$nameSmallPlural" |
||
| 754 | ], $resetControllerContent); |
||
| 755 | |||
| 756 | $myAccountFileContentNew = str_replace([ |
||
| 757 | '{{$name}}', |
||
| 758 | '{{$nameSmall}}' |
||
| 759 | ], [ |
||
| 760 | "$name", |
||
| 761 | "$nameSmall" |
||
| 762 | ], $myAccountControllerContent); |
||
| 763 | |||
| 764 | $verificationControllerContentNew = str_replace([ |
||
| 765 | '{{$name}}', |
||
| 766 | '{{$nameSmall}}' |
||
| 767 | ], [ |
||
| 768 | "$name", |
||
| 769 | "$nameSmall" |
||
| 770 | ], $verificationControllerContent); |
||
| 771 | |||
| 772 | $controllerFile = $nameFolder.DIRECTORY_SEPARATOR."Controller.php"; |
||
| 773 | $homeFile = $nameFolder.DIRECTORY_SEPARATOR."HomeController.php"; |
||
| 774 | $loginFile = $authFolder.DIRECTORY_SEPARATOR."LoginController.php"; |
||
| 775 | $forgotFile = $authFolder.DIRECTORY_SEPARATOR."ForgotPasswordController.php"; |
||
| 776 | $registerFile = $authFolder.DIRECTORY_SEPARATOR."RegisterController.php"; |
||
| 777 | $resetFile = $authFolder.DIRECTORY_SEPARATOR."ResetPasswordController.php"; |
||
| 778 | $verificationFile = $authFolder.DIRECTORY_SEPARATOR."VerificationController.php"; |
||
| 779 | |||
| 780 | $myAccountFile = $authFolder.DIRECTORY_SEPARATOR."{$name}AccountController.php"; |
||
| 781 | |||
| 782 | |||
| 783 | file_put_contents($controllerFile, $controllerFileContentNew); |
||
| 784 | file_put_contents($homeFile, $homeFileContentNew); |
||
| 785 | file_put_contents($loginFile, $loginFileContentNew); |
||
| 786 | file_put_contents($forgotFile, $forgotFileContentNew); |
||
| 787 | file_put_contents($registerFile, $registerFileContentNew); |
||
| 788 | file_put_contents($resetFile, $resetFileContentNew); |
||
| 789 | file_put_contents($myAccountFile, $myAccountFileContentNew); |
||
| 790 | file_put_contents($verificationFile, $verificationControllerContentNew); |
||
| 791 | |||
| 792 | return true; |
||
| 793 | |||
| 794 | } |
||
| 795 | |||
| 796 | /** |
||
| 797 | * Install Configs. |
||
| 798 | * |
||
| 799 | * @return boolean |
||
| 800 | */ |
||
| 801 | |||
| 802 | public function installConfigs() |
||
| 803 | { |
||
| 804 | $nameSmall = snake_case($this->getParsedNameInput()); |
||
| 805 | $nameSmallPlural = str_plural(snake_case($this->getParsedNameInput())); |
||
| 806 | $name = ucfirst($this->getParsedNameInput()); |
||
| 807 | |||
| 808 | $authConfigFile = $this->getConfigsFolderPath().DIRECTORY_SEPARATOR."auth.php"; |
||
| 809 | |||
| 810 | $guardContent = file_get_contents(__DIR__ . '/../Config/guard.stub'); |
||
| 811 | $passwordContent = file_get_contents(__DIR__ . '/../Config/password.stub'); |
||
| 812 | $providerContent = file_get_contents(__DIR__ . '/../Config/provider.stub'); |
||
| 813 | |||
| 814 | $guardFileContentNew = str_replace([ |
||
| 815 | '{{$nameSmall}}', |
||
| 816 | '{{$nameSmallPlural}}' |
||
| 817 | ], [ |
||
| 818 | "$nameSmall", |
||
| 819 | "$nameSmallPlural" |
||
| 820 | ], $guardContent); |
||
| 821 | |||
| 822 | $passwordFileContentNew = str_replace('{{$nameSmallPlural}}', "$nameSmallPlural", $passwordContent); |
||
| 823 | |||
| 824 | $providerFileContentNew = str_replace([ |
||
| 825 | '{{$name}}', |
||
| 826 | '{{$nameSmallPlural}}' |
||
| 827 | ], [ |
||
| 828 | "$name", |
||
| 829 | "$nameSmallPlural" |
||
| 830 | ], $providerContent); |
||
| 831 | |||
| 832 | $this->insert($authConfigFile, ' \'guards\' => [', $guardFileContentNew, true); |
||
| 833 | |||
| 834 | $this->insert($authConfigFile, ' \'passwords\' => [', $passwordFileContentNew, true); |
||
| 835 | |||
| 836 | $this->insert($authConfigFile, ' \'providers\' => [', $providerFileContentNew, true); |
||
| 837 | |||
| 838 | return true; |
||
| 839 | |||
| 840 | } |
||
| 841 | |||
| 842 | /** |
||
| 843 | * Install Unauthenticated Handler. |
||
| 844 | * |
||
| 845 | * @return boolean |
||
| 846 | */ |
||
| 847 | public function installUnauthenticated() |
||
| 848 | { |
||
| 849 | $nameSmall = snake_case($this->getParsedNameInput()); |
||
| 850 | $exceptionHandlerFile = $this->getAppFolderPath().DIRECTORY_SEPARATOR."Exceptions".DIRECTORY_SEPARATOR |
||
| 851 | ."Handler.php"; |
||
| 852 | $exceptionHandlerFileContent = file_get_contents($exceptionHandlerFile); |
||
| 853 | $exceptionHandlerFileContentNew = file_get_contents(__DIR__ . '/../Exceptions/handlerUnauthorized.stub'); |
||
| 854 | |||
| 855 | |||
| 856 | if (!str_contains($exceptionHandlerFileContent, 'MultiAuthUnAuthenticated')) { |
||
| 857 | // replace old file |
||
| 858 | $deleted = unlink($exceptionHandlerFile); |
||
| 859 | if ($deleted) { |
||
| 860 | file_put_contents($exceptionHandlerFile, $exceptionHandlerFileContentNew); |
||
| 861 | } |
||
| 862 | } |
||
| 863 | |||
| 864 | $exceptionHandlerGuardContentNew = file_get_contents(__DIR__ . '/../Exceptions/handlerGuard.stub'); |
||
| 865 | $exceptionHandlerGuardContentNew2 = str_replace('{{$nameSmall}}', "$nameSmall", |
||
| 866 | $exceptionHandlerGuardContentNew); |
||
| 867 | |||
| 868 | $this->insert($exceptionHandlerFile, ' switch(array_get($exception->guards(), 0)) {', |
||
| 869 | $exceptionHandlerGuardContentNew2, true); |
||
| 870 | |||
| 871 | return true; |
||
| 872 | |||
| 873 | } |
||
| 874 | |||
| 875 | /** |
||
| 876 | * Install Middleware. |
||
| 877 | * |
||
| 878 | * @return boolean |
||
| 879 | */ |
||
| 880 | |||
| 881 | public function installMiddleware() |
||
| 938 | |||
| 939 | } |
||
| 940 | |||
| 941 | /** |
||
| 942 | * Run a SSH command. |
||
| 943 | * |
||
| 944 | * @param string $command The SSH command that needs to be run |
||
| 945 | * @param string $beforeNotice Information for the user before the command is run |
||
| 946 | * @param string $afterNotice Information for the user after the command is run |
||
| 947 | * |
||
| 948 | * @return mixed Command-line output |
||
| 949 | */ |
||
| 950 | public function executeProcess($command, $beforeNotice = '', $afterNotice = '') |
||
| 951 | { |
||
| 952 | if (!is_null($beforeNotice)) { |
||
| 953 | $this->info('### '.$beforeNotice); |
||
| 954 | } else { |
||
| 955 | $this->info('### Running: '.$command); |
||
| 956 | } |
||
| 957 | $process = new Process($command); |
||
| 958 | $process->run(function ($type, $buffer) { |
||
| 959 | if (Process::ERR === $type) { |
||
| 960 | echo '... > '.$buffer; |
||
| 961 | } else { |
||
| 962 | echo 'OUT > '.$buffer; |
||
| 963 | } |
||
| 964 | }); |
||
| 965 | // executes after the command finishes |
||
| 966 | if (!$process->isSuccessful()) { |
||
| 967 | throw new ProcessFailedException($process); |
||
| 968 | } |
||
| 969 | if (!is_null($afterNotice)) { |
||
| 970 | $this->info('### '.$afterNotice); |
||
| 971 | } |
||
| 972 | } |
||
| 973 | |||
| 974 | /** |
||
| 975 | * Get the desired class name from the input. |
||
| 976 | * |
||
| 977 | * @return string |
||
| 978 | */ |
||
| 979 | protected function getParsedNameInput() |
||
| 980 | { |
||
| 981 | return mb_strtolower(str_singular($this->getNameInput())); |
||
| 982 | } |
||
| 983 | /** |
||
| 984 | * Get the desired class name from the input. |
||
| 985 | * |
||
| 986 | * @return string |
||
| 987 | */ |
||
| 988 | protected function getNameInput() |
||
| 989 | { |
||
| 990 | $name = $this->argument('name'); |
||
| 991 | return trim($name); |
||
| 992 | } |
||
| 993 | |||
| 994 | /** |
||
| 995 | * Write the migration file to disk. |
||
| 996 | * |
||
| 997 | * @param string $name |
||
| 998 | * @param string $table |
||
| 999 | * @param bool $create |
||
| 1000 | * @return mixed |
||
| 1001 | */ |
||
| 1002 | protected function writeMigration($name, $table, $create) |
||
| 1003 | { |
||
| 1004 | $file = pathinfo($this->creator->create( |
||
| 1005 | $name, $this->getMigrationPath(), $table, $create |
||
| 1006 | ), PATHINFO_FILENAME); |
||
| 1007 | $this->line("<info>Created Migration:</info> {$file}"); |
||
| 1008 | } |
||
| 1009 | |||
| 1010 | /** |
||
| 1011 | * Get migration path. |
||
| 1012 | * |
||
| 1013 | * @return string |
||
| 1014 | */ |
||
| 1015 | protected function getMigrationPath() |
||
| 1016 | { |
||
| 1017 | return parent::getMigrationPath(); |
||
| 1018 | } |
||
| 1019 | |||
| 1020 | /** |
||
| 1021 | * Get Routes Provider Path. |
||
| 1022 | * |
||
| 1023 | * @return string |
||
| 1024 | */ |
||
| 1025 | protected function getRouteServicesPath() |
||
| 1026 | { |
||
| 1027 | return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Providers'.DIRECTORY_SEPARATOR.'RouteServiceProvider.php'; |
||
| 1028 | } |
||
| 1029 | |||
| 1030 | /** |
||
| 1031 | * Get Routes Folder Path. |
||
| 1032 | * |
||
| 1033 | * @return string |
||
| 1034 | */ |
||
| 1035 | protected function getAppFolderPath() |
||
| 1036 | { |
||
| 1037 | return $this->laravel->basePath().DIRECTORY_SEPARATOR.'app'; |
||
| 1038 | } |
||
| 1039 | |||
| 1040 | /** |
||
| 1041 | * Get Routes Folder Path. |
||
| 1042 | * |
||
| 1043 | * @return string |
||
| 1044 | */ |
||
| 1045 | protected function getRoutesFolderPath() |
||
| 1046 | { |
||
| 1047 | return $this->laravel->basePath().DIRECTORY_SEPARATOR.'routes'; |
||
| 1048 | } |
||
| 1049 | |||
| 1050 | /** |
||
| 1051 | * Get Config Folder Path. |
||
| 1052 | * |
||
| 1053 | * @return string |
||
| 1054 | */ |
||
| 1055 | protected function getConfigsFolderPath() |
||
| 1056 | { |
||
| 1057 | return $this->laravel->basePath().DIRECTORY_SEPARATOR.'config'; |
||
| 1058 | } |
||
| 1059 | |||
| 1060 | /** |
||
| 1061 | * Get Config Folder Path. |
||
| 1062 | * |
||
| 1063 | * @return string |
||
| 1064 | */ |
||
| 1065 | protected function getViewsFolderPath() |
||
| 1066 | { |
||
| 1067 | return $this->laravel->basePath().DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'views'; |
||
| 1068 | } |
||
| 1069 | |||
| 1070 | /** |
||
| 1071 | * Get Controllers Path. |
||
| 1072 | * |
||
| 1073 | * @return string |
||
| 1074 | */ |
||
| 1075 | protected function getControllersPath() |
||
| 1078 | } |
||
| 1079 | |||
| 1080 | /** |
||
| 1081 | * Get Http Path. |
||
| 1082 | * |
||
| 1083 | * @return string |
||
| 1084 | */ |
||
| 1085 | protected function getHttpPath() |
||
| 1086 | { |
||
| 1087 | return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Http'; |
||
| 1088 | } |
||
| 1089 | |||
| 1090 | /** |
||
| 1091 | * Get Middleware Path. |
||
| 1092 | * |
||
| 1093 | * @return string |
||
| 1094 | */ |
||
| 1095 | protected function getMiddlewarePath() |
||
| 1098 | } |
||
| 1099 | |||
| 1100 | /** |
||
| 1101 | * insert text into file |
||
| 1102 | * |
||
| 1103 | * @param string $filePath |
||
| 1104 | * @param string $insertMarker |
||
| 1105 | * @param string $text |
||
| 1106 | * @param boolean $after |
||
| 1107 | * |
||
| 1108 | * @return integer |
||
| 1109 | */ |
||
| 1110 | public function insertIntoFile($filePath, $insertMarker, $text, $after = true) { |
||
| 1111 | $contents = file_get_contents($filePath); |
||
| 1112 | $new_contents = preg_replace($insertMarker,($after) ? '$0' . $text : $text . '$0', $contents); |
||
| 1113 | return file_put_contents($filePath, $new_contents); |
||
| 1114 | } |
||
| 1115 | |||
| 1116 | /** |
||
| 1117 | * insert text into file |
||
| 1118 | * |
||
| 1119 | * @param string $filePath |
||
| 1120 | * @param string $keyword |
||
| 1121 | * @param string $body |
||
| 1122 | * @param boolean $after |
||
| 1123 | * |
||
| 1124 | * @return integer |
||
| 1125 | */ |
||
| 1126 | public function insert($filePath, $keyword, $body, $after = true) { |
||
| 1133 | } |
||
| 1134 | } |
||
| 1135 |