InstallCommand.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace Dcat\Admin\Console;
  3. use Illuminate\Console\Command;
  4. class InstallCommand extends Command
  5. {
  6. /**
  7. * The console command name.
  8. *
  9. * @var string
  10. */
  11. protected $signature = 'admin:install';
  12. /**
  13. * The console command description.
  14. *
  15. * @var string
  16. */
  17. protected $description = 'Install the admin package';
  18. /**
  19. * Install directory.
  20. *
  21. * @var string
  22. */
  23. protected $directory = '';
  24. /**
  25. * Execute the console command.
  26. *
  27. * @return void
  28. */
  29. public function handle()
  30. {
  31. $this->initDatabase();
  32. $this->initAdminDirectory();
  33. }
  34. /**
  35. * Create tables and seed it.
  36. *
  37. * @return void
  38. */
  39. public function initDatabase()
  40. {
  41. $this->call('migrate');
  42. $userModel = config('admin.database.users_model');
  43. if ($userModel::count() == 0) {
  44. $this->call('db:seed', ['--class' => \Dcat\Admin\Models\AdminTablesSeeder::class]);
  45. }
  46. }
  47. /**
  48. * Initialize the admAin directory.
  49. *
  50. * @return void
  51. */
  52. protected function initAdminDirectory()
  53. {
  54. $this->directory = config('admin.directory');
  55. if (is_dir($this->directory)) {
  56. $this->line("<error>{$this->directory} directory already exists !</error> ");
  57. return;
  58. }
  59. $this->makeDir('/');
  60. $this->line('<info>Admin directory was created:</info> '.str_replace(base_path(), '', $this->directory));
  61. $this->makeDir('Controllers');
  62. $this->createHomeController();
  63. $this->createAuthController();
  64. $this->createBootstrapFile();
  65. $this->createRoutesFile();
  66. }
  67. /**
  68. * Create HomeController.
  69. *
  70. * @return void
  71. */
  72. public function createHomeController()
  73. {
  74. $homeController = $this->directory.'/Controllers/HomeController.php';
  75. $contents = $this->getStub('HomeController');
  76. $this->laravel['files']->put(
  77. $homeController,
  78. str_replace('DummyNamespace', config('admin.route.namespace'), $contents)
  79. );
  80. $this->line('<info>HomeController file was created:</info> '.str_replace(base_path(), '', $homeController));
  81. }
  82. /**
  83. * Create AuthController.
  84. *
  85. * @return void
  86. */
  87. public function createAuthController()
  88. {
  89. $authController = $this->directory.'/Controllers/AuthController.php';
  90. $contents = $this->getStub('AuthController');
  91. $this->laravel['files']->put(
  92. $authController,
  93. str_replace('DummyNamespace', config('admin.route.namespace'), $contents)
  94. );
  95. $this->line('<info>AuthController file was created:</info> '.str_replace(base_path(), '', $authController));
  96. }
  97. /**
  98. * Create routes file.
  99. *
  100. * @return void
  101. */
  102. protected function createBootstrapFile()
  103. {
  104. $file = $this->directory.'/bootstrap.php';
  105. $contents = $this->getStub('bootstrap');
  106. $this->laravel['files']->put($file, $contents);
  107. $this->line('<info>Bootstrap file was created:</info> '.str_replace(base_path(), '', $file));
  108. }
  109. /**
  110. * Create routes file.
  111. *
  112. * @return void
  113. */
  114. protected function createRoutesFile()
  115. {
  116. $file = $this->directory.'/routes.php';
  117. $contents = $this->getStub('routes');
  118. $this->laravel['files']->put($file, str_replace('DummyNamespace', config('admin.route.namespace'), $contents));
  119. $this->line('<info>Routes file was created:</info> '.str_replace(base_path(), '', $file));
  120. }
  121. /**
  122. * Get stub contents.
  123. *
  124. * @param $name
  125. *
  126. * @return string
  127. */
  128. protected function getStub($name)
  129. {
  130. return $this->laravel['files']->get(__DIR__."/stubs/$name.stub");
  131. }
  132. /**
  133. * Make new directory.
  134. *
  135. * @param string $path
  136. */
  137. protected function makeDir($path = '')
  138. {
  139. $this->laravel['files']->makeDirectory("{$this->directory}/$path", 0755, true, true);
  140. }
  141. }