jelix/version
Composer 安装命令:
composer require jelix/version
包简介
Parse any version syntax, including semantic version. Compare version, using Composer version constraints syntax.
README 文档
README
This PHP library parse any version syntax, including semantic version. It allows also to compare versions, using Composer version constraints syntax.
installation
You can install it from Composer. In your project:
composer require "jelix/version"
Usage
Parsing
Use the Jelix\Version\Parser class to retrieve a Jelix\Version\Version object
containing all versions informations.
$version = \Jelix\Version\Parser::parse('1.2.3b2'); $version->toString(); // '1.2.3-beta.2' $version->getMajor(); // 1 $version->getMinor(); // 2 $version->getPatch(); // 3 $version->getStabilityVersion(); // array('beta', '2') $version->getNextMajorVersion(); // Version object for '2.0.0' $version->getNextMinorVersion(); // Version object for '1.3.0' $version->getNextPatchVersion(); // Version object for '1.2.4' $version->getBranchVersion(); // '1.2'
Example of supported version syntaxes:
- 1.0, 1.2.3
- 1.2-alpha, 1.2a, 1.2alpha, 1.2alpha.3, 1.2a1.3
- 1.2.3-beta, 1.2b, 1.2beta, 1.2beta.3, 1.2b3.4
- 1.2RC, 1.2-rc.4.5
- 1.2-dev, 1.2b1-dev, 1.2b1-dev.9, 1.2RC-dev, 1.2RC2-dev.1700
The parser support also 'secondary versions', i.e. versions appended to a version
after a - or a :.
Ex: 1.2.3-1.4.5, 1.2.3:1.4.5. Here, 1.4.5 is a secondary version.
When retrieving a Version object for such versions, you can access to the secondary
version through a method getSecondaryVersion() which returns a Version object:
$version = \Jelix\Version\Parser::parse('1.2.3:1.4.5'); $version->toString(); // '1.2.3:1.4.5' $version->toString(true, false); // '1.2.3' $version2 = $version->getSecondaryVersion(); $version2->toString(); // '1.4.5' $version->getNextMajorVersion(); // Version object for '2.0.0' $version->getNextMinorVersion(); // Version object for '1.3.0' $version->getNextPatchVersion(); // Version object for '1.2.4' $version->getBranchVersion(); // '1.2'
Simple comparison
$v1 = '1.2.3'; $v2 = '1.4.5'; $result = \Jelix\Version\VersionComparator::compareVersion($v1, $v2);
compareVersion() returns:
-1if $v1 < $v20if $v1 == $v21if $v1 > $v2
Example to compare two versions:
\Jelix\Version\VersionComparator::compareVersion('1.2pre','1.2RC');
Secondary versions are also compared if primary versions are equals. In this case, if one of the version string does not contain a secondary version, then it is considered having the '0.0' version number, so it is considered as lower version than the version having a secondary version.
You have also an other method compare() taking Version objects as parameters:
$v1 = \Jelix\Version\Parser::parse('1.2.3'); $v2 = \Jelix\Version\Parser::parse('1.4.5'); $result = \Jelix\Version\VersionComparator::compare($v1, $v2);
Comparison with range
compareVersionRange() allows you to use operator to compare version. It
is compatible with version constraints supported by Composer.
- comparison operators:
>,<,>=,<=,=,!= - no operator means
= ~: specify a range between the version and the next major version (excluding the next major version itself and its unstable variant)~1.2is equivalent to>=1.2 <2.0.0-dev~1.2.3is equivalent to>=1.2.3 <1.3.0-dev
- '^' : specify a range between the version and the next minor version
^1.2.3is equivalent to>=1.2.3 <1.3.0^0.3is equivalent to>=0.3.0 <0.4.0
- hyphen separator to specify a range : '1.2 - 1.5'
1.0 - 2.0is equivalent to>=1.0.0 <2.1(as 2.0 is interpreted as 2.0.*)1.0.0 - 2.1.0is equivalent to>=1.0.0 <=2.1.0
- It supports also version wildcards with or without operators:
1.*,1.2.*
You can combine several constraints with boolean operators :
- AND operator:
,or - OR operator:
||or|.
// check if 0.5 is between 0.8 and 1.0 or if it is higher than 2.0 \Jelix\Version\VersionComparator::compareVersionRange('0.5','<1.0,>0.8|>2.0'); // check if 0.5 is between 0.8 and 1.0 \Jelix\Version\VersionComparator::compareVersionRange('0.5','0.8 - 1.0'); // with a wildcard \Jelix\Version\VersionComparator::compareVersionRange('1.1', '1.1.*'); // returns true \Jelix\Version\VersionComparator::compareVersionRange('1.1.2', '1.2.*'); // returns false \Jelix\Version\VersionComparator::compareVersionRange('1.3.0', '>=1.2.*'); // returns true
Note: comparison with a range is done only on primary version. If a version string contains
a secondary version, this secondary version is not compared. To compare a secondary version
with a range, you should retrieve the secondary version with the getSecondaryVersion() method.
$version = Jelix\Version\Parser::parse('1.2.3:1.0.0'); \Jelix\Version\VersionComparator::compareVersionRange($version, '>=1.2.*'); // returns true $version2 = $version->getSecondaryVersion(); $version2->toString(); // '1.0.0' \Jelix\Version\VersionComparator::compareVersionRange($version2, '>=1.2.*'); // returns false
History
Ancesters of these classes have been included for years into the Jelix Framework until Jelix 1.6, and has been released in 2016 into a separate repository.
jelix/version 适用场景与选型建议
jelix/version 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 210.06k 次下载、GitHub Stars 达 13, 最近一次更新时间为 2016 年 05 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「composer」 「version」 「semver」 「comparator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jelix/version 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jelix/version 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jelix/version 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Semantic Versioning Parser and Comparator
Versioning behaviour for eloquent models
Converts SemVer version (like Composer packages) into integer version with operators. Helps managing versions: store, compare, sort and retrive by conditions.
A decent, standards-compliant, Semantic Versioning (SemVer) parser and library
Display a version number
PHP Version Manager for Windows CLI
统计信息
- 总下载量: 210.06k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 13
- 点击次数: 28
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-05-16