123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- <?php
- namespace cores\library;
- use app\common\library\helper;
- use cores\exception\BaseException;
- /**
- * 版本号工具类
- */
- class Version
- {
- /**
- * 获取当前系统版本号
- * @return string
- * @throws BaseException
- */
- public static function getVersion(): string
- {
- static $version = [];
- if (empty($version)) {
- // 读取version.json文件
- $filePath = root_path() . '/version.json';
- !file_exists($filePath) && throwError('version.json not found');
- // 解析json数据
- $version = helper::jsonDecode(file_get_contents($filePath));
- !is_array($version) && throwError('version cannot be decoded');
- }
- return $version['version'];
- }
- /**
- * 获取下一个版本号
- * @param string $currentVersion 当前的版本号
- * @param array $versionCollection 版本号列表
- * @return false|string
- * @throws BaseException
- */
- public static function nextVersion(string $currentVersion, array $versionCollection)
- {
- $vers1 = self::versionToInteger($currentVersion);
- $dataset = [];
- foreach ($versionCollection as $value) {
- $vers2 = self::versionToInteger($value);
- $vers2 > $vers1 && $dataset[] = $vers2;
- }
- if (empty($dataset)) {
- return false;
- }
- return self::integerToVersion(min($dataset));
- }
- /**
- * 将版本转为数字
- * @param string $version
- * @return int
- * @throws BaseException
- */
- public static function versionToInteger(string $version): int
- {
- if (!self::check($version)) {
- throwError('version Validate Error');
- }
- list($major, $minor, $sub) = explode('.', $version);
- return intval($major * 10000 + $minor * 100 + $sub);
- }
- /**
- * 将数字转为版本
- * @param int $versionCode 版本的数字表示
- * @return string
- * @throws BaseException
- */
- public static function integerToVersion(int $versionCode): string
- {
- if (!is_numeric($versionCode) || $versionCode >= 100000) {
- throwError('version code Validate Error');
- }
- $version = array();
- $version[0] = (int)($versionCode / 10000);
- $version[1] = (int)($versionCode % 10000 / 100);
- $version[2] = $versionCode % 100;
- return implode('.', $version);
- }
- /**
- * 检查版本格式是否正确
- * @param string $version 版本
- * @return bool
- */
- public static function check(string $version): bool
- {
- return (bool)preg_match('/^[0-9]{1,3}\.[0-9]{1,2}\.[0-9]{1,2}$/', $version);
- }
- /**
- * 比较两个版本的值
- * @param string $version1 版本1
- * @param string $version2 版本2
- * @return int -1:版本1小于版本2, 0:相等, 1:版本1大于版本2
- * @throws BaseException
- */
- public static function compare(string $version1, string $version2): int
- {
- if (!self::check($version1) || !self::check($version2)) {
- throwError('version1 or version2 Validate Error');
- }
- return version_compare($version1, $version2);
- }
- }
|