install.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. # # 在用户项目根目录执行
  3. # php vendor/xiaosongshu/elasticsearch/scripts/install.php
  4. // 1. 推导路径(关键:适配用户项目结构)
  5. $scriptPath = realpath(__FILE__); // 当前脚本绝对路径
  6. $libraryPath = dirname($scriptPath,2); // 库根目录
  7. $projectRoot = dirname($libraryPath,2) . '/..'; // 项目根目录
  8. $sourceConfig = $libraryPath . '/config/elasticsearch.php'; // 源配置文件
  9. $targetDir = $projectRoot . '/config'; // 目标配置目录(用户项目的 config 目录)
  10. $targetConfig = $targetDir . '/elasticsearch.php'; // 目标文件
  11. // 2. 检查源文件是否存在
  12. if (!file_exists($sourceConfig)) {
  13. fwrite(STDERR, "错误:配置文件不存在 -> {$sourceConfig}\n");
  14. return;
  15. }
  16. // 3. 创建目标目录(不存在则创建)
  17. if (!is_dir($targetDir)) {
  18. mkdir($targetDir, 0755, true);
  19. }
  20. // 4. 复制文件(避免覆盖用户已修改的配置)
  21. if (file_exists($targetConfig)) {
  22. fwrite(STDOUT, "提示:配置文件已存在,跳过复制 -> {$targetConfig}\n");
  23. return;
  24. }
  25. // 5. 执行复制
  26. if (copy($sourceConfig, $targetConfig)) {
  27. fwrite(STDOUT, "成功:配置文件已自动复制到 -> {$targetConfig}\n");
  28. return;
  29. } else {
  30. fwrite(STDERR, "错误:配置文件复制失败 -> {$targetConfig}\n");
  31. return;
  32. }