Version.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace cores\library;
  13. use app\common\library\helper;
  14. use cores\exception\BaseException;
  15. /**
  16. * 版本号工具类
  17. * Class Files
  18. * @package cores\library
  19. */
  20. class Version
  21. {
  22. /**
  23. * 获取当前系统版本号
  24. * @return string
  25. * @throws BaseException
  26. */
  27. public static function getVersion(): string
  28. {
  29. static $version = [];
  30. if (empty($version)) {
  31. // 读取version.json文件
  32. $filePath = root_path() . '/version.json';
  33. !file_exists($filePath) && throwError('version.json not found');
  34. // 解析json数据
  35. $version = helper::jsonDecode(file_get_contents($filePath));
  36. !is_array($version) && throwError('version cannot be decoded');
  37. }
  38. return $version['version'];
  39. }
  40. /**
  41. * 获取下一个版本号
  42. * @param string $currentVersion 当前的版本号
  43. * @param array $versionCollection 版本号列表
  44. * @return false|string
  45. * @throws BaseException
  46. */
  47. public static function nextVersion(string $currentVersion, array $versionCollection)
  48. {
  49. $vers1 = self::versionToInteger($currentVersion);
  50. $dataset = [];
  51. foreach ($versionCollection as $value) {
  52. $vers2 = self::versionToInteger($value);
  53. $vers2 > $vers1 && $dataset[] = $vers2;
  54. }
  55. if (empty($dataset)) {
  56. return false;
  57. }
  58. return self::integerToVersion(min($dataset));
  59. }
  60. /**
  61. * 将版本转为数字
  62. * @param string $version
  63. * @return int
  64. * @throws BaseException
  65. */
  66. public static function versionToInteger(string $version): int
  67. {
  68. if (!self::check($version)) {
  69. throwError('version Validate Error');
  70. }
  71. list($major, $minor, $sub) = explode('.', $version);
  72. return intval($major * 10000 + $minor * 100 + $sub);
  73. }
  74. /**
  75. * 将数字转为版本
  76. * @param int $versionCode 版本的数字表示
  77. * @return string
  78. * @throws BaseException
  79. */
  80. public static function integerToVersion(int $versionCode): string
  81. {
  82. if (!is_numeric($versionCode) || $versionCode >= 100000) {
  83. throwError('version code Validate Error');
  84. }
  85. $version = array();
  86. $version[0] = (int)($versionCode / 10000);
  87. $version[1] = (int)($versionCode % 10000 / 100);
  88. $version[2] = $versionCode % 100;
  89. return implode('.', $version);
  90. }
  91. /**
  92. * 检查版本格式是否正确
  93. * @param string $version 版本
  94. * @return bool
  95. */
  96. public static function check(string $version): bool
  97. {
  98. return (bool)preg_match('/^[0-9]{1,3}\.[0-9]{1,2}\.[0-9]{1,2}$/', $version);
  99. }
  100. /**
  101. * 比较两个版本的值
  102. * @param string $version1 版本1
  103. * @param string $version2 版本2
  104. * @return int -1:版本1小于版本2, 0:相等, 1:版本1大于版本2
  105. * @throws BaseException
  106. */
  107. public static function compare(string $version1, string $version2): int
  108. {
  109. if (!self::check($version1) || !self::check($version2)) {
  110. throwError('version1 or version2 Validate Error');
  111. }
  112. return version_compare($version1, $version2);
  113. }
  114. }