AuthMakeCommand.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace Illuminate\Auth\Console;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Console\DetectsApplicationNamespace;
  5. class AuthMakeCommand extends Command
  6. {
  7. use DetectsApplicationNamespace;
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'make:auth
  14. {--views : Only scaffold the authentication views}
  15. {--force : Overwrite existing views by default}';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'Scaffold basic login and registration views and routes';
  22. /**
  23. * The views that need to be exported.
  24. *
  25. * @var array
  26. */
  27. protected $views = [
  28. 'auth/login.stub' => 'auth/login.blade.php',
  29. 'auth/register.stub' => 'auth/register.blade.php',
  30. 'auth/passwords/email.stub' => 'auth/passwords/email.blade.php',
  31. 'auth/passwords/reset.stub' => 'auth/passwords/reset.blade.php',
  32. 'layouts/app.stub' => 'layouts/app.blade.php',
  33. 'home.stub' => 'home.blade.php',
  34. ];
  35. /**
  36. * Execute the console command.
  37. *
  38. * @return void
  39. */
  40. public function handle()
  41. {
  42. $this->createDirectories();
  43. $this->exportViews();
  44. if (! $this->option('views')) {
  45. file_put_contents(
  46. app_path('Http/Controllers/HomeController.php'),
  47. $this->compileControllerStub()
  48. );
  49. file_put_contents(
  50. base_path('routes/web.php'),
  51. file_get_contents(__DIR__.'/stubs/make/routes.stub'),
  52. FILE_APPEND
  53. );
  54. }
  55. $this->info('Authentication scaffolding generated successfully.');
  56. }
  57. /**
  58. * Create the directories for the files.
  59. *
  60. * @return void
  61. */
  62. protected function createDirectories()
  63. {
  64. if (! is_dir($directory = resource_path('views/layouts'))) {
  65. mkdir($directory, 0755, true);
  66. }
  67. if (! is_dir($directory = resource_path('views/auth/passwords'))) {
  68. mkdir($directory, 0755, true);
  69. }
  70. }
  71. /**
  72. * Export the authentication views.
  73. *
  74. * @return void
  75. */
  76. protected function exportViews()
  77. {
  78. foreach ($this->views as $key => $value) {
  79. if (file_exists($view = resource_path('views/'.$value)) && ! $this->option('force')) {
  80. if (! $this->confirm("The [{$value}] view already exists. Do you want to replace it?")) {
  81. continue;
  82. }
  83. }
  84. copy(
  85. __DIR__.'/stubs/make/views/'.$key,
  86. $view
  87. );
  88. }
  89. }
  90. /**
  91. * Compiles the HomeController stub.
  92. *
  93. * @return string
  94. */
  95. protected function compileControllerStub()
  96. {
  97. return str_replace(
  98. '{{namespace}}',
  99. $this->getAppNamespace(),
  100. file_get_contents(__DIR__.'/stubs/make/controllers/HomeController.stub')
  101. );
  102. }
  103. }