Install.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. // +---------------------------------------------------------------------+
  3. // | NiuCloud | [ WE CAN DO IT JUST NiuCloud ]  |
  4. // +---------------------------------------------------------------------+
  5. // | Copy right 2019-2029 www.niucloud.com  |
  6. // +---------------------------------------------------------------------+
  7. // | Author | NiuCloud <niucloud@outlook.com>  |
  8. // +---------------------------------------------------------------------+
  9. // | Repository | https://github.com/niucloud/framework.git  |
  10. // +---------------------------------------------------------------------+
  11. declare(strict_types=1);
  12. namespace addon\servicer\event;
  13. /**
  14. * 应用安装
  15. */
  16. class Install
  17. {
  18. /**
  19. * 执行安装
  20. */
  21. public function handle()
  22. {
  23. $source_file = './' . ADDON_PATH . 'servicer/' . 'config/source/Events.php';
  24. $target_file = './' . ADDON_PATH . 'servicer/gateway/Applications/Service/Events.php';
  25. // 读取配置文件
  26. $fp = fopen($source_file, 'r');
  27. $config = fread($fp, filesize($source_file));
  28. fclose($fp);
  29. // 替换内容
  30. $database_config = config('database');
  31. $config = str_replace('model_hostname', $database_config['connections']['mysql']['hostname'], $config);
  32. $config = str_replace('model_database', $database_config['connections']['mysql']['database'], $config);
  33. $config = str_replace('model_username', $database_config['connections']['mysql']['username'], $config);
  34. $config = str_replace('model_password', $database_config['connections']['mysql']['password'], $config);
  35. $config = str_replace('model_port', $database_config['connections']['mysql']['hostport'], $config);
  36. $config = str_replace('model_prefix', $database_config['connections']['mysql']['prefix'], $config);
  37. // 检测文件是否可写
  38. $fp = fopen($target_file, 'w');
  39. if ($fp == false) {
  40. return error(-1, '写入配置失败,请检查' . $target_file . '是否可写入!');
  41. }
  42. fwrite($fp, $config);
  43. fclose($fp);
  44. return success();
  45. }
  46. }