Version.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace cores\library;
  3. use app\common\library\helper;
  4. use cores\exception\BaseException;
  5. /**
  6. * 版本号工具类
  7. */
  8. class Version
  9. {
  10. /**
  11. * 获取当前系统版本号
  12. * @return string
  13. * @throws BaseException
  14. */
  15. public static function getVersion(): string
  16. {
  17. static $version = [];
  18. if (empty($version)) {
  19. // 读取version.json文件
  20. $filePath = root_path() . '/version.json';
  21. !file_exists($filePath) && throwError('version.json not found');
  22. // 解析json数据
  23. $version = helper::jsonDecode(file_get_contents($filePath));
  24. !is_array($version) && throwError('version cannot be decoded');
  25. }
  26. return $version['version'];
  27. }
  28. /**
  29. * 获取下一个版本号
  30. * @param string $currentVersion 当前的版本号
  31. * @param array $versionCollection 版本号列表
  32. * @return false|string
  33. * @throws BaseException
  34. */
  35. public static function nextVersion(string $currentVersion, array $versionCollection)
  36. {
  37. $vers1 = self::versionToInteger($currentVersion);
  38. $dataset = [];
  39. foreach ($versionCollection as $value) {
  40. $vers2 = self::versionToInteger($value);
  41. $vers2 > $vers1 && $dataset[] = $vers2;
  42. }
  43. if (empty($dataset)) {
  44. return false;
  45. }
  46. return self::integerToVersion(min($dataset));
  47. }
  48. /**
  49. * 将版本转为数字
  50. * @param string $version
  51. * @return int
  52. * @throws BaseException
  53. */
  54. public static function versionToInteger(string $version): int
  55. {
  56. if (!self::check($version)) {
  57. throwError('version Validate Error');
  58. }
  59. list($major, $minor, $sub) = explode('.', $version);
  60. return intval($major * 10000 + $minor * 100 + $sub);
  61. }
  62. /**
  63. * 将数字转为版本
  64. * @param int $versionCode 版本的数字表示
  65. * @return string
  66. * @throws BaseException
  67. */
  68. public static function integerToVersion(int $versionCode): string
  69. {
  70. if (!is_numeric($versionCode) || $versionCode >= 100000) {
  71. throwError('version code Validate Error');
  72. }
  73. $version = array();
  74. $version[0] = (int)($versionCode / 10000);
  75. $version[1] = (int)($versionCode % 10000 / 100);
  76. $version[2] = $versionCode % 100;
  77. return implode('.', $version);
  78. }
  79. /**
  80. * 检查版本格式是否正确
  81. * @param string $version 版本
  82. * @return bool
  83. */
  84. public static function check(string $version): bool
  85. {
  86. return (bool)preg_match('/^[0-9]{1,3}\.[0-9]{1,2}\.[0-9]{1,2}$/', $version);
  87. }
  88. /**
  89. * 比较两个版本的值
  90. * @param string $version1 版本1
  91. * @param string $version2 版本2
  92. * @return int -1:版本1小于版本2, 0:相等, 1:版本1大于版本2
  93. * @throws BaseException
  94. */
  95. public static function compare(string $version1, string $version2): int
  96. {
  97. if (!self::check($version1) || !self::check($version2)) {
  98. throwError('version1 or version2 Validate Error');
  99. }
  100. return version_compare($version1, $version2);
  101. }
  102. }