Install.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // | Original reference: https://gitee.com/karson/fastadmin
  12. // +----------------------------------------------------------------------
  13. // +----------------------------------------------------------------------
  14. // | 安装主程序
  15. // +----------------------------------------------------------------------
  16. namespace app\admin\command;
  17. use app\admin\library\Install as InstallLib;
  18. use think\console\Command;
  19. use think\console\Input;
  20. use think\console\input\Option;
  21. use think\console\Output;
  22. use think\Exception;
  23. use think\facade\Cache;
  24. use think\facade\Config;
  25. use think\facade\Request;
  26. use util\Random;
  27. class Install extends Command
  28. {
  29. /**
  30. * @var \think\Request Request 实例
  31. */
  32. protected $request;
  33. protected function configure()
  34. {
  35. $config = Config::get('database.connections.mysql');
  36. $this->setName('install')
  37. ->addOption('hostname', 'a', Option::VALUE_OPTIONAL, 'mysql hostname', $config['hostname'])
  38. ->addOption('hostport', 'o', Option::VALUE_OPTIONAL, 'mysql hostport', $config['hostport'])
  39. ->addOption('database', 'd', Option::VALUE_OPTIONAL, 'mysql database', $config['database'])
  40. ->addOption('prefix', 'r', Option::VALUE_OPTIONAL, 'table prefix', $config['prefix'])
  41. ->addOption('username', 'u', Option::VALUE_OPTIONAL, 'mysql username', $config['username'])
  42. ->addOption('password', 'p', Option::VALUE_OPTIONAL, 'mysql password', $config['password'])
  43. ->addOption('force', 'f', Option::VALUE_OPTIONAL, 'force override', false)
  44. ->setDescription('New installation of YznCMS');
  45. }
  46. /**
  47. * 命令行安装
  48. */
  49. protected function execute(Input $input, Output $output)
  50. {
  51. define('INSTALL_PATH', app()->getBasePath() . 'admin' . DS . 'command' . DS . 'Install' . DS);
  52. // 覆盖安装
  53. $force = $input->getOption('force');
  54. $hostname = $input->getOption('hostname');
  55. $hostport = $input->getOption('hostport');
  56. $database = $input->getOption('database');
  57. $prefix = $input->getOption('prefix');
  58. $username = $input->getOption('username');
  59. $password = $input->getOption('password');
  60. $installLockFile = INSTALL_PATH . "install.lock";
  61. if (is_file($installLockFile) && !$force) {
  62. $output->error("\nYznCMS already installed!\nIf you need to reinstall again, use the parameter --force=true");
  63. return false;
  64. }
  65. $adminUsername = 'admin';
  66. $adminPassword = Random::alnum(10);
  67. $adminEmail = 'admin@admin.com';
  68. try {
  69. $adminName = InstallLib::installation($hostname, $hostport, $database, $username, $password, $prefix, $adminUsername, $adminPassword, $adminEmail);
  70. } catch (\Exception $e) {
  71. $output->error($e->getMessage());
  72. return false;
  73. }
  74. if ($adminName) {
  75. $output->highlight("Admin url:http://www.yoursite.com/{$adminName}");
  76. }
  77. $output->highlight("Admin username: {$adminUsername}");
  78. $output->highlight("Admin password: {$adminPassword}");
  79. Cache::delete("__menu__");
  80. $output->info("Install Successed!");
  81. }
  82. }