InstallCommand.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace Dcat\Admin\Console;
  3. use Dcat\Admin\Models\AdminTablesSeeder;
  4. use Illuminate\Console\Command;
  5. class InstallCommand extends Command
  6. {
  7. /**
  8. * The console command name.
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'admin:install';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = 'Install the admin package';
  19. /**
  20. * Install directory.
  21. *
  22. * @var string
  23. */
  24. protected $directory = '';
  25. /**
  26. * Execute the console command.
  27. *
  28. * @return void
  29. */
  30. public function handle()
  31. {
  32. $this->initDatabase();
  33. $this->initAdminDirectory();
  34. $this->info('Done.');
  35. }
  36. /**
  37. * Create tables and seed it.
  38. *
  39. * @return void
  40. */
  41. public function initDatabase()
  42. {
  43. $this->call('migrate');
  44. $userModel = config('admin.database.users_model');
  45. if ($userModel::count() == 0) {
  46. $this->call('db:seed', ['--class' => AdminTablesSeeder::class]);
  47. }
  48. }
  49. /**
  50. * Set admin directory.
  51. *
  52. * @return void
  53. */
  54. protected function setDirectory()
  55. {
  56. $this->directory = config('admin.directory');
  57. }
  58. /**
  59. * Initialize the admin directory.
  60. *
  61. * @return void
  62. */
  63. protected function initAdminDirectory()
  64. {
  65. $this->setDirectory();
  66. if (is_dir($this->directory)) {
  67. $this->warn("{$this->directory} directory already exists !");
  68. return;
  69. }
  70. $this->makeDir('/');
  71. $this->line('<info>Admin directory was created:</info> '.str_replace(base_path(), '', $this->directory));
  72. $this->makeDir('Controllers');
  73. $this->makeDir('Metrics/Examples');
  74. $this->createHomeController();
  75. $this->createAuthController();
  76. $this->createMetricCards();
  77. $this->createBootstrapFile();
  78. $this->createRoutesFile();
  79. }
  80. /**
  81. * Create HomeController.
  82. *
  83. * @return void
  84. */
  85. public function createHomeController()
  86. {
  87. $homeController = $this->directory.'/Controllers/HomeController.php';
  88. $contents = $this->getStub('HomeController');
  89. $this->laravel['files']->put(
  90. $homeController,
  91. str_replace(
  92. ['DummyNamespace', 'MetricsNamespace'],
  93. [$this->namespace('Controllers'), $this->namespace('Metrics\\Examples')],
  94. $contents
  95. )
  96. );
  97. $this->line('<info>HomeController file was created:</info> '.str_replace(base_path(), '', $homeController));
  98. }
  99. /**
  100. * Create AuthController.
  101. *
  102. * @return void
  103. */
  104. public function createAuthController()
  105. {
  106. $authController = $this->directory.'/Controllers/AuthController.php';
  107. $contents = $this->getStub('AuthController');
  108. $this->laravel['files']->put(
  109. $authController,
  110. str_replace(
  111. ['DummyNamespace'],
  112. [$this->namespace('Controllers')],
  113. $contents
  114. )
  115. );
  116. $this->line('<info>AuthController file was created:</info> '.str_replace(base_path(), '', $authController));
  117. }
  118. /**
  119. * @return void
  120. */
  121. public function createMetricCards()
  122. {
  123. $map = [
  124. '/Metrics/Examples/NewUsers.php' => 'metrics/NewUsers',
  125. '/Metrics/Examples/NewDevices.php' => 'metrics/NewDevices',
  126. '/Metrics/Examples/ProductOrders.php' => 'metrics/ProductOrders',
  127. '/Metrics/Examples/Sessions.php' => 'metrics/Sessions',
  128. '/Metrics/Examples/Tickets.php' => 'metrics/Tickets',
  129. '/Metrics/Examples/TotalUsers.php' => 'metrics/TotalUsers',
  130. ];
  131. $namespace = $this->namespace('Metrics\\Examples');
  132. foreach ($map as $path => $stub) {
  133. $this->laravel['files']->put(
  134. $this->directory.$path,
  135. str_replace(
  136. 'DummyNamespace',
  137. $namespace,
  138. $this->getStub($stub)
  139. )
  140. );
  141. }
  142. }
  143. /**
  144. * @param string $name
  145. *
  146. * @return string
  147. */
  148. protected function namespace($name = null)
  149. {
  150. $base = str_replace('\\Controllers', '\\', config('admin.route.namespace'));
  151. return trim($base, '\\').($name ? "\\{$name}" : '');
  152. }
  153. /**
  154. * Create routes file.
  155. *
  156. * @return void
  157. */
  158. protected function createBootstrapFile()
  159. {
  160. $file = $this->directory.'/bootstrap.php';
  161. $contents = $this->getStub('bootstrap');
  162. $this->laravel['files']->put($file, $contents);
  163. $this->line('<info>Bootstrap file was created:</info> '.str_replace(base_path(), '', $file));
  164. }
  165. /**
  166. * Create routes file.
  167. *
  168. * @return void
  169. */
  170. protected function createRoutesFile()
  171. {
  172. $file = $this->directory.'/routes.php';
  173. $contents = $this->getStub('routes');
  174. $this->laravel['files']->put($file, str_replace('DummyNamespace', $this->namespace('Controllers'), $contents));
  175. $this->line('<info>Routes file was created:</info> '.str_replace(base_path(), '', $file));
  176. }
  177. /**
  178. * Get stub contents.
  179. *
  180. * @param $name
  181. *
  182. * @return string
  183. */
  184. protected function getStub($name)
  185. {
  186. return $this->laravel['files']->get(__DIR__."/stubs/$name.stub");
  187. }
  188. /**
  189. * Make new directory.
  190. *
  191. * @param string $path
  192. */
  193. protected function makeDir($path = '')
  194. {
  195. $this->laravel['files']->makeDirectory("{$this->directory}/$path", 0755, true, true);
  196. }
  197. }