vierbergenlars/php-semver
最新稳定版本:v3.0.4
Composer 安装命令:
composer require vierbergenlars/php-semver
包简介
The Semantic Versioner for PHP
关键字:
README 文档
README
Ported from node-semver 1.1.2 to PHP
php-semver 2.x is compatible with semver 1.0.0, and NOT with semver 2.0.0, have a look at php-semver 3.x if you require semver 2.0.0 compatibility
Usage
<?php use vierbergenlars\SemVer\version; use vierbergenlars\SemVer\expression; use vierbergenlars\SemVer\SemVerException; // Check if a version is valid $semver = new version('1.2.3'); $semver = new version('a.b.c'); //SemVerException thrown //Get a clean version string $semver = new version('=v1.2.3'); $semver->getVersion(); //'1.2.3' //Check if a version satisfies a range $semver = new version('1.2.3'); $semver->satisfies(new expression('1.x || >=2.5.0 || 5.0.0 - 7.2.3')); //true # OR $range = new expression('1.x || >=2.5.0 || 5.0.0 - 7.2.3'); $range->satisfiedBy(new version('1.2.3')); //true //Compare two versions version::gt('1.2.3', '9.8.7'); //false version::lt('1.2.3', '9.8.7'); //true
Versions
A version is the following things, in this order:
- a number (Major)
- a period
- a number (minor)
- a period
- a number (patch)
- OPTIONAL: a hyphen, followed by a number (build)
- OPTIONAL: a collection of pretty much any non-whitespace characters (tag)
A leading "=" or "v" character is stripped off and ignored.
Comparisons
The ordering of versions is done using the following algorithm, given two versions and asked to find the greater of the two:
- If the majors are numerically different, then take the one
with a bigger major number.
2.3.4 > 1.3.4 - If the minors are numerically different, then take the one
with the bigger minor number.
2.3.4 > 2.2.4 - If the patches are numerically different, then take the one with the
bigger patch number.
2.3.4 > 2.3.3 - If only one of them has a build number, then take the one with the
build number.
2.3.4-0 > 2.3.4 - If they both have build numbers, and the build numbers are numerically
different, then take the one with the bigger build number.
2.3.4-10 > 2.3.4-9 - If only one of them has a tag, then take the one without the tag.
2.3.4 > 2.3.4-beta - If they both have tags, then take the one with the lexicographically
larger tag.
2.3.4-beta > 2.3.4-alpha - At this point, they're equal.
Ranges
The following range styles are supported:
>1.2.3Greater than a specific version.<1.2.3Less than1.2.3 - 2.3.4:=>=1.2.3 <=2.3.4~1.2.3:=>=1.2.3 <1.3.0~1.2:=>=1.2.0 <2.0.0~1:=>=1.0.0 <2.0.01.2.x:=>=1.2.0 <1.3.01.x:=>=1.0.0 <2.0.0
Ranges can be joined with either a space (which implies "and") or a
|| (which implies "or").
Functions
$version->valid(): Return the parsed version, or null if it's not valid.$version->inc($type): Return the version incremented by the release type (major, minor, patch, or build), or null if an invalid release type is provided.
Comparison
version::gt($v1, $v2):v1 > v2version::gte($v1, $v2):v1 >= v2version::lt($v1, $v2):v1 < v2version::lte($v1, $v2):v1 <= v2version::eq($v1, $v2):v1 == v2This is true if they're logically equivalent, even if they're not the exact same string. You already know how to compare strings.version::neq($v1, $v2):v1 != v2The opposite of eq.version::cmp($v1, $comparator, $v2): Pass in a comparison string, and it'll call the corresponding function above."==="and"!=="do simple string comparison, but are included for completeness. Throws if an invalid comparison string is provided.version::compare($v1, $v2): Return 0 ifv1 == v2, 1 ifv1 > v2, or -1 ifv1 < v2. Sorts in ascending order if passed tousort()version::rcompare($v1, $v2): The reverse of compare. Sorts an array of versions in descending order when passed tousort().
Ranges
$expression->validRange(): Return the valid range ornullif it's not valid$version->satisfies($range): Returntrueif the version satisfies the range.$expression->maxSatisfying($versions): Return the highest version in the array that satisfies the range, ornullif none of them do.
Thanks to
All contributors (https://github.com/vierbergenlars/php-semver/graphs/contributors)
@isaacs and other contributors to node-semver
vierbergenlars/php-semver 适用场景与选型建议
vierbergenlars/php-semver 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.42M 次下载、GitHub Stars 达 115, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「semver」 「versioning」 「semantic」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 vierbergenlars/php-semver 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vierbergenlars/php-semver 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 vierbergenlars/php-semver 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Semantic Versioning Parser and Comparator
An extension to Semantic MediaWiki allowing to build breadcrumb links from an attributive property filter
Versioning behaviour for eloquent models
Minimal CSS Framework for semantic HTML.
Adds the EDTF data type to Wikibase
Converts SemVer version (like Composer packages) into integer version with operators. Helps managing versions: store, compare, sort and retrive by conditions.
统计信息
- 总下载量: 3.42M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 116
- 点击次数: 30
- 依赖项目数: 67
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04