kherge/semver
Composer 安装命令:
composer require kherge/semver
包简介
Manages semantic version numbers and compares them.
关键字:
README 文档
README
Version
A library for parsing and comparing semantic version numbers.
Usage
Please note that full qualified names are not used in the examples below. All
references to interfaces, classes, and functions can be found in the namespace
KHerGe\Version. Also note that the examples below do not demonstrate the
complete abilities of the library. Please refer to the source files for more
information.
Simple
Parsing
For very simple use cases, you only need the parse() function.
use function KHerGe\Version\parse;
With this function, you can create a value object that represents an individual semantic version number. The value object is immutable, but convenience methods are available so that you can alter values and receive new value objects.
// Create a new value object. $version = parse('1.2.3-alpha.1+20161004'); // Bump the patch number: 1.0.1 $patched = $version->incrementPatch(); // The original value object is unchanged. echo $version; // 1.2.3-alpha.1+20161004 // But the patched version number has the change. echo $patched; // 1.2.4
Comparing
Simple comparisons can also be performed directly on the value objects.
if ($patched->isGreaterThan($version)) { // $patched is greater than $version }
Validating
While, the parse() function throws a InvalidStringRepresentationException
for invalid string representations of a semantic version number, you can still
check yourself by using the is_valid() function.
use function KHerGe\Version\is_valid; $version = '1.2.3-alpha.1+20161004'; if (is_valid($version)) { // $version is valid }
Complex
Implementations
The library will work on any implementation of VersionInterface but provides
a Version implementation that includes a lot of extra methods for convenience.
$version = new Version( // major 1, // minor 2, // patch 3, // pre-release ['a', 'b', 'c'], // build ['x', 'y', 'z'] );
Parsing
If you need to use your own implementation of VersionInterface, the library
provides a function to parse the components of a string representation so that
you won't have to.
use function KHerGe\Version\parse_components; $components = parse_components('1.2.3-alpha.1+20161004');
The result of parse_components() can be used to create a new instance that
implements VersionInterface. This function performs its own validation, so
checking with is_valid() will be redundant.
$components = [ 'major' => 1, 'minor' => 2, 'patch' => 3, 'pre-release' => ['alpha', '1'], 'build' => ['20161004'] ];
Comparing
The library contains a set of pre-made constraints, all of which implement
ConstraintInterface. These constraints can be mixed and matched in order to
perform far more complex comparison operations than by using the constraints
on their own.
use KHerGe\Version\Compare\Constraint\AndX; use KHerGe\Version\Compare\Constraint\EqualTo; use KHerGe\Version\Compare\Constraint\GreaterThan; use KHerGe\Version\Compare\Constraint\LessThan; use KHerGe\Version\Compare\Constraint\OrX; use function KHerGe\Version\parse; // Match any of the following constraints. $constraint = new OrX( [ // Match all of the following constraints. new AndX( [ // Must be greater than "0.2.3". new GreaterThan(parse('0.2.3')), // Must be less than "0.4.4". new LessThan(parse('0.4.4')), ] ), // Match exactly "0.4.5". new EqualTo(parse('0.4.5')) ] ); // Verify that the version meets the constraints. $version = parse('0.4.0'); if ($constraint->allows($version)) { // $version is allowed }
Requires
- PHP 7.0 or greater
Install
To install, you will need to use Composer.
composer require kherge/semver
License
Released under both MIT and Apache 2.0.
See LICENSE.
kherge/semver 适用场景与选型建议
kherge/semver 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.31k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2016 年 10 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「semver」 「versioning」 「semantic」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kherge/semver 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kherge/semver 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kherge/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.
统计信息
- 总下载量: 4.31k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-10-05