composer/semver 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

composer/semver

最新稳定版本:3.4.4

Composer 安装命令:

composer require composer/semver

包简介

Version comparison library that offers utilities, version constraint parsing and validation.

README 文档

README

Version comparison library that offers utilities, version constraint parsing and validation.

It follows semver (semantic versioning) where possible but is also constrained by version_compare and backwards compatibility and as such cannot implement semver strictly.

Originally written as part of composer/composer, now extracted and made available as a stand-alone library.

Continuous Integration PHP Lint PHPStan

Installation

Install the latest version with:

composer require composer/semver

Requirements

  • PHP 5.3.2 is required but using the latest version of PHP is highly recommended.

Version Comparison

For details on how versions are compared, refer to the Versions article in the documentation section of the getcomposer.org website.

Basic usage

Validation / Normalization

The Composer\Semver\VersionParser class provides the following methods for parsing, normalizing and validating versions and constraints.

Numeric versions are normalized to a 4 component versions (e.g. 1.2.3 is normalized to 1.2.3.0) for internal consistency and compatibility with version_compare. Normalized versions are used for constraints internally but should not be shown to end users.

For versions:

  • isValid($version)
  • normalize($version, $fullVersion = null)
  • normalizeBranch($name)
  • normalizeDefaultBranch($name)

For constraints:

  • parseConstraints($constraints)

For stabilities:

  • parseStability($version)
  • normalizeStability($stability)

Comparison

The Composer\Semver\Comparator class provides the following methods for comparing versions:

  • greaterThan($v1, $v2)
  • greaterThanOrEqualTo($v1, $v2)
  • lessThan($v1, $v2)
  • lessThanOrEqualTo($v1, $v2)
  • equalTo($v1, $v2)
  • notEqualTo($v1, $v2)

Each function takes two version strings as arguments and returns a boolean. For example:

use Composer\Semver\Comparator;

Comparator::greaterThan('1.25.0', '1.24.0'); // 1.25.0 > 1.24.0

Semver

The Composer\Semver\Semver class provides the following methods:

  • satisfies($version, $constraints)
  • satisfiedBy(array $versions, $constraint)
  • sort($versions)
  • rsort($versions)

Intervals

The Composer\Semver\Intervals static class provides a few utilities to work with complex constraints or read version intervals from a constraint:

use Composer\Semver\Intervals;

// Checks whether $candidate is a subset of $constraint
Intervals::isSubsetOf(ConstraintInterface $candidate, ConstraintInterface $constraint);

// Checks whether $a and $b have any intersection, equivalent to $a->matches($b)
Intervals::haveIntersections(ConstraintInterface $a, ConstraintInterface $b);

// Optimizes a complex multi constraint by merging all intervals down to the smallest
// possible multi constraint. The drawbacks are this is not very fast, and the resulting
// multi constraint will have no human readable prettyConstraint configured on it
Intervals::compactConstraint(ConstraintInterface $constraint);

// Creates an array of numeric intervals and branch constraints representing a given constraint
Intervals::get(ConstraintInterface $constraint);

// Clears the memoization cache when you are done processing constraints
Intervals::clear()

See the class docblocks for more details.

License

composer/semver is licensed under the MIT License, see the LICENSE file for details.

统计信息

  • 总下载量: 464.33M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3295
  • 点击次数: 1
  • 依赖项目数: 634
  • 推荐数: 8

GitHub 信息

  • Stars: 3272
  • Watchers: 13
  • Forks: 73
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04