pretty-package-versions

repository·2.x·Indexed 23 days ago

https://github.com/jean85/pretty-package-versions

A PHP wrapper for Composer dependencies that provides human-readable ('pretty') version strings, abstracting differences between Composer 1 and 2. It provides the Jean85\PrettyVersions class for retrieving package and root project versions, and the Jean85\Version value object for accessing formatted version strings, short versions, and commit references.

Tokens
1.2K
Snippets
2
Records
9
Agent score
78%

What's inside pretty-package-versions

  1. Install pretty-package-versions via Composer

    2.x

    To install this package, use Composer. If you are developing a library, it is recommended to use the constraint ^1.5 || ^2.0 to ensure compatibility with both Composer 1.x and 2.x without forcing end users to upgrade immediately.

    composer require 'jean85/pretty-package-versions:^1.5 || ^2.0'
  2. Get the root package name and version

    2.x

    Since version 1.5, Jean85\PrettyVersions provides methods to interact with the current (root) project defined in your composer.json:

    • PrettyVersions::getRootPackageName(): Returns the name of the root package.
    • PrettyVersions::getRootPackageVersion(): A shortcut to get the version of the root package.
  3. Use Jean85\PrettyVersions to get package versions

    2.x

    The primary entry point is the Jean85\PrettyVersions class. Use the static method getVersion(string $packageName) to retrieve a Jean85\Version object for a specific dependency.

    use Jean85\PrettyVersions;
    
    $version = PrettyVersions::getVersion('phpunit/phpunit');
    $version->getPrettyVersion(); // '6.0.0'
    $version->getShortVersion(); // '6.0.0'
    $version->getVersionWithShortReference(); // '6.0.0@fa5711'
  4. Methods available on the Jean85\Version class

    2.x

    The Jean85\Version object returned by PrettyVersions::getVersion() provides several ways to access version and reference data:

    • getPrettyVersion(): string: Returns the short version if the package is tagged; otherwise, returns the version with the short reference.
    • getShortVersion(): string: Returns just the version string (e.g., 6.0.0, v.1.7.0, 99999-dev).
    • getReference(): string: Returns the full reference (usually the full Git commit hash).
    • getShortReference(): string: Returns the shortened version of the reference (e.g., fa5711).
    • getVersionWithShortReference(): string: Returns the version followed by the short reference (e.g., 6.0.0@fa5711).
    • getPackageName(): string: Returns the original package name.
    • getFullVersion(): string: Returns the same value as PackageVersions\Versions::getVersion().
    • __toString(): string: Casts the object to a string, returning the same result as getPrettyVersion().
  5. Migration from version < 2.0 (Reference methods)

    2.x

    In version 2.0, methods related to 'commit' were renamed to 'reference' to better align with the Composer 2 API. The old methods are still available but are deprecated:

    New methodOld, deprecated method
    Version::getReference()Version::getCommitHash()
    Version::getShortReference()Version::getShortCommitHash()
    Version::getVersionWithShortReference()Version::getVersionWithShortCommit()
  6. Retrieve formatted version strings from Version

    2.x

    The Version class offers several ways to format the version information depending on whether you need the full reference or a shortened version:

    • getPrettyVersion(): Returns the version string. If the version is detected as a tagged version (e.g., 1.0.0 or v1.0.0), it returns the prettyVersion. Otherwise, it returns the version combined with a short reference.
    • getFullVersion(): Returns the prettyVersion concatenated with the full reference using an @ symbol (e.g., 1.2.3@abc123456789).
    • getVersionWithShortReference(): Returns the prettyVersion concatenated with a truncated version of the reference (e.g., 1.2.3@abc1234).
    • getShortReference(): Returns the first 7 characters of the reference, or {no reference} if no reference exists.
    • __toString(): Casting the object to a string returns the result of getPrettyVersion().
  7. Use the Version class to represent package versions

    2.x

    The Jean85\Version class is a value object used to represent a package version, its associated reference (like a commit hash), and its package name. It provides methods to retrieve formatted version strings, such as 'pretty' versions or versions combined with short references.

    Key constants:

    • Version::NO_VERSION_TEXT: Returns {no version} if no version is provided.
    • Version::NO_REFERENCE_TEXT: Returns {no reference} if no reference is provided.
  8. Access package metadata from Version

    2.x

    You can retrieve the underlying metadata used to construct the Version object using these methods:

    • getPackageName(): Returns the package name string.
    • getShortVersion(): Returns the prettyVersion string.
    • getReference(): Returns the full reference string (e.g., a full commit hash).
    • getPrettyVersion(): Returns the formatted version string (as described in the formatting section).
  9. Retrieve pretty version strings with PrettyVersions::getVersion()

    2.x

    Use PrettyVersions::getVersion(string $packageName) to obtain a Version object containing the pretty version string and the reference for a specific installed Composer package.

    Exceptions:

    • Throws ProvidedPackageException if the package is marked as 'provided' in Composer's installed data.
    • Throws ReplacedPackageException if the package is marked as 'replaced' in Composer's installed data.
    • Both exceptions implement VersionMissingExceptionInterface.