PanelInstallation.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. class PanelInstallation extends Command
  10. {
  11. protected $signature = 'panel:install';
  12. protected $description = 'ProxyPanel Installation (面板自主安装)';
  13. public function handle()
  14. {
  15. try {
  16. $bar = $this->output->createProgressBar(7);
  17. $bar->minSecondsBetweenRedraws(0);
  18. $this->info(PHP_EOL.' ___ ___ _ '.PHP_EOL." / _ \ _ __ ___ __ __ _ _ / _ \ __ _ _ __ ___ | |".PHP_EOL." / /_)/| '__| / _ \ \ \/ /| | | | / /_)/ / _` || '_ \ / _ \| |".PHP_EOL.'/ ___/ | | | (_) | > < | |_| |/ ___/ | (_| || | | || __/| |'.PHP_EOL."\/ |_| \___/ /_/\_\ \__, |\/ \__,_||_| |_| \___||_|".PHP_EOL.' |___/ '.PHP_EOL);
  19. $bar->start();
  20. $this->line(' 创建.env');
  21. if (File::exists(base_path().'/.env')) {
  22. $this->error('.env existed | .env已存在');
  23. if ($this->confirm('Do you wish to continue by deleting current exist .env file? | 是否删除已存.env文件, 并继续安装?', true)) {
  24. File::delete(base_path().'/.env');
  25. } else {
  26. abort(500, 'Installation aborted by user decision. | 安装程序终止');
  27. }
  28. }
  29. if (! copy(base_path().'/.env.example', base_path().'/.env')) {
  30. abort(500, 'copy .env.example to .env failed, please check file permissions | 复制环境文件失败,请检查目录权限');
  31. }
  32. $bar->advance();
  33. // 设置数据库信息
  34. $this->line(' 设置数据库信息');
  35. $this->saveToEnv([
  36. 'DB_HOST' => $this->ask('请输入数据库地址(默认:localhost)', 'localhost'),
  37. 'DB_PORT' => $this->ask('请输入数据库地址(默认:3306)', 3306),
  38. 'DB_DATABASE' => $this->ask('请输入数据库名'),
  39. 'DB_USERNAME' => $this->ask('请输入数据库用户名'),
  40. 'DB_PASSWORD' => $this->ask('请输入数据库密码'),
  41. ]);
  42. $bar->advance();
  43. // 设置 app key
  44. $this->line(' 设置 app key');
  45. Artisan::call('key:generate');
  46. $bar->advance();
  47. // 测试数据库连通性
  48. $this->line(' 测试数据库连通性');
  49. try {
  50. Artisan::call('config:cache');
  51. DB::connection()->getPdo();
  52. } catch (Exception $e) {
  53. File::delete(base_path().'/.env');
  54. abort(500, '数据库连接失败'.$e->getMessage());
  55. }
  56. $bar->advance();
  57. // 初始化数据库
  58. $this->line(' 导入数据库');
  59. Artisan::call('migrate --seed');
  60. $this->info('数据库导入完成');
  61. $bar->advance();
  62. // 文件夹软连接
  63. $this->line(' 建立文件夹软连接');
  64. Artisan::call('storage:link');
  65. $bar->advance();
  66. // 设置 管理员基础信息
  67. $this->line(' 设置管理员基础信息');
  68. $email = '';
  69. while (! $email) {
  70. $email = $this->ask('Please set your administrator account email address | 请输入[管理员]邮箱 默认: [email protected]', '[email protected]');
  71. $this->info('[管理员] 账号:'.$email);
  72. }
  73. $password = '';
  74. while (! $password) {
  75. $password = $this->ask('Please set your administrator account password | 请输入[管理员]密码 默认: 123456', '123456');
  76. $this->info('[管理员]密码:'.$password);
  77. }
  78. if (! $this->editAdmin($email, $password)) {
  79. abort(500, '管理员账号注册失败,请重试');
  80. }
  81. $bar->finish();
  82. $this->info(' Initial installation Completed! | 初步安装完毕');
  83. $this->line(PHP_EOL.'View your http(s)://[website url]/admin to insert Administrator Dashboard | 访问 http(s)://[你的站点]/admin 进入管理面板');
  84. } catch (Exception $e) {
  85. $this->error($e->getMessage());
  86. }
  87. return 0;
  88. }
  89. private function saveToEnv($data = [])
  90. {
  91. function set_env_var($key, $value): bool
  92. {
  93. if (! is_bool(strpos($value, ' '))) {
  94. $value = '"'.$value.'"';
  95. }
  96. $key = strtoupper($key);
  97. $envPath = app()->environmentFilePath();
  98. $contents = file_get_contents($envPath);
  99. preg_match("/^{$key}=[^\r\n]*/m", $contents, $matches);
  100. $oldValue = count($matches) ? $matches[0] : '';
  101. if ($oldValue) {
  102. $contents = str_replace((string) $oldValue, "{$key}={$value}", $contents);
  103. } else {
  104. $contents .= "\n{$key}={$value}\n";
  105. }
  106. $file = fopen($envPath, 'wb');
  107. fwrite($file, $contents);
  108. return fclose($file);
  109. }
  110. foreach ($data as $key => $value) {
  111. set_env_var($key, $value);
  112. }
  113. return true;
  114. }
  115. private function editAdmin($email, $password)
  116. {
  117. $user = User::find(1);
  118. $user->username = $email;
  119. $user->password = $password;
  120. return $user->save();
  121. }
  122. }