PanelInstallation.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\User;
  4. use Artisan;
  5. use Exception;
  6. use File;
  7. use Illuminate\Console\Command;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Str;
  10. class PanelInstallation extends Command
  11. {
  12. protected $signature = 'panel:install';
  13. protected $description = 'ProxyPanel Installation (面板自主安装)';
  14. public function handle(): int
  15. {
  16. $bar = $this->output->createProgressBar(7);
  17. $bar->minSecondsBetweenRedraws(0);
  18. $this->displayBanner();
  19. $bar->start();
  20. $this->createEnvironmentFile();
  21. $bar->advance();
  22. $this->setDatabaseInfo(); // 设置数据库信息
  23. $bar->advance();
  24. $this->generateAppKey(); // 设置 app key
  25. $bar->advance();
  26. $this->testDatabaseConnection(); // 测试数据库连通性
  27. $bar->advance();
  28. $this->importDatabase(); // 初始化数据库
  29. $bar->advance();
  30. $this->createStorageLink(); // 文件夹软连接
  31. $bar->advance();
  32. $this->setAdminAccount(); // 设置管理员信息
  33. $bar->finish();
  34. $this->info('Initial installation completed! | 初步安装完毕');
  35. $this->line(PHP_EOL.'View your http(s)://[website url]/admin to insert Administrator Dashboard | 访问 http(s)://[你的站点]/admin 进入管理面板');
  36. return 0;
  37. }
  38. private function displayBanner(): void
  39. {
  40. $banner = <<<BANNER
  41. ___ ___ _
  42. / _ \ _ __ ___ __ __ _ _ / _ \ __ _ _ __ ___ | |
  43. / /_)/| '__| / _ \ \ \/ /| | | | / /_)/ / _` || '_ \ / _ \| |
  44. / ___/ | | | (_) | > < | |_| |/ ___/ | (_| || | | || __/| |
  45. \/ |_| \___/ /_/\_\ \__, |\/ \__,_||_| |_| \___||_|
  46. |___/
  47. BANNER;
  48. $this->info($banner);
  49. }
  50. private function createEnvironmentFile(): void
  51. {
  52. $this->line(' Creating .env | 创建.env');
  53. $envPath = app()->environmentFilePath();
  54. if (File::exists($envPath)) {
  55. $this->error('.env file already exists. | .env已存在');
  56. if (! $this->confirm('Do you wish to continue by deleting the existing .env file? | 是否删除已存.env文件, 并继续安装?')) {
  57. abort(500, 'Installation aborted by user decision. | 安装程序终止');
  58. }
  59. File::delete($envPath);
  60. }
  61. try {
  62. File::copy(base_path('.env.example'), $envPath);
  63. } catch (Exception $e) {
  64. abort(500, 'Failed to copy .env.example to .env file. Please check file permissions. | 复制环境文件失败, 请检查目录权限 '.$e->getMessage());
  65. }
  66. }
  67. private function setDatabaseInfo(): void
  68. {
  69. $this->line(' Setting up database information | 设置数据库信息');
  70. $databaseInfo = [
  71. 'DB_HOST' => $this->ask('Enter the database host (default: localhost) | 请输入数据库地址(默认:localhost)', 'localhost'),
  72. 'DB_PORT' => $this->ask('Enter the database port (default: 3306) | 请输入数据库地址(默认:3306)', 3306),
  73. 'DB_DATABASE' => $this->ask('Enter the database name | 请输入数据库名'),
  74. 'DB_USERNAME' => $this->ask('Enter the database username | 请输入数据库用户名'),
  75. 'DB_PASSWORD' => $this->ask('Enter the database password | 请输入数据库密码'),
  76. ];
  77. $this->saveToEnv($databaseInfo);
  78. }
  79. private function saveToEnv(array $data = []): void
  80. {
  81. $envPath = app()->environmentFilePath();
  82. $contents = File::get($envPath);
  83. foreach ($data as $key => $value) {
  84. $key = strtoupper($key);
  85. $value = Str::contains($value, ' ') ? '"'.$value.'"' : $value;
  86. $line = $key.'='.$value;
  87. $contents = preg_replace("/^$key=[^\r\n]*/m", $line, $contents, -1, $count);
  88. if ($count === 0) {
  89. $contents .= "\n".$line;
  90. }
  91. }
  92. File::put($envPath, $contents);
  93. $this->line('.env file updated successfully. | .env文件更新成功');
  94. }
  95. private function generateAppKey(): void
  96. {
  97. $this->line(' Generating app key | 设置 app key');
  98. Artisan::call('key:generate');
  99. }
  100. private function testDatabaseConnection(): void
  101. {
  102. $this->line(' Testing database connection | 测试数据库连通性');
  103. try {
  104. Artisan::call('config:cache');
  105. DB::connection()->getPdo();
  106. } catch (Exception $e) {
  107. File::delete(app()->environmentFilePath());
  108. abort(500, 'Failed to connect to the database: | 数据库连接失败: '.$e->getMessage());
  109. }
  110. }
  111. private function importDatabase(): void
  112. {
  113. $this->line(' Importing database | 导入数据库');
  114. try {
  115. Artisan::call('migrate --seed');
  116. } catch (Exception $e) {
  117. Artisan::call('db:wipe');
  118. abort(500, 'Failed to import database: | 导入数据库失败: '.$e->getMessage());
  119. }
  120. $this->info('Database loaded! | 数据库导入完成');
  121. }
  122. private function createStorageLink(): void
  123. {
  124. $this->line(' Creating storage link | 建立文件夹软连接');
  125. try {
  126. Artisan::call('storage:link');
  127. } catch (Exception $e) {
  128. abort(500, 'Failed to create storage link: | 建立文件夹软连接失败: '.$e->getMessage());
  129. }
  130. }
  131. private function setAdminAccount(): void
  132. {
  133. $this->line(' Setting up admin account | 设置管理员基础信息');
  134. $username = $this->ask('Please set your administrator account email address | 请输入[管理员]邮箱 默认: [email protected]', '[email protected]');
  135. $this->info('[管理员] 账号: '.$username);
  136. $password = $this->ask('Please set your administrator account password | 请输入[管理员]密码 默认: 123456', '123456');
  137. $this->info('[管理员] 密码: '.$password);
  138. if ($this->editAdmin($username, $password)) {
  139. $this->info('Admin account created successfully. | 管理员账号创建成功');
  140. } else {
  141. abort(500, '管理员账号注册失败,请重试');
  142. }
  143. $this->info('Admin account created successfully.');
  144. }
  145. private function editAdmin(string $username, string $password): bool
  146. {
  147. $user = User::find(1);
  148. $user->username = $username;
  149. $user->password = $password;
  150. return $user->save();
  151. }
  152. }