debver - Debian/Ubuntu packager version helper
Version 1.2
, (*1)
Simple PHP helper class for working with Debian/Ubuntu package version strings., (*2)
Installing
The easiest way to install Debver is to use Composer, the awesome dependency manager for PHP. Once Composer is installed, run composer.phar require valorin/debver:1.*
and composer will do all the hard work for you., (*3)
Usage
If you are using the autoloader in Composer (or your framework ties into it), then all you need to do is add a use Debver\Version;
statement at the top of each file you wish to use Debver in and us e it like a normal class:, (*4)
<?php
namespace YourApplication;
use Debver\Version;
$version1 = "5.1.2-1ubuntu3.9";
$version2 = "3.9.4-4ubuntu0.2";
$compare = Version::compare($version1, $version2);
Compare two version strings:, (*5)
$result = Version::compare($version1, $version2);
if ($result == -1) {
echo "{$version1} < {$version2}";
} elseif ($result == 0) {
echo "{$version1} == {$version2}";
} elseif ($result == 1) {
echo "{$version1} > {$version2}";
}
Extract version string components:, (*6)
The three components to a version string can be extracted easily: Epoch, Upstream Version, and Debian Revision:, (*7)
$version = new Version($version1);
$epoch = $version->getEpoch();
$upstream = $version->getUpstream();
$revision = $version->getRevision();
Ubuntu manual: deb-version, (*8)
Compare two version strings using dpkg:, (*9)
If you are running on a Ubuntu/Debian box, you can use dpkg
directly to compare two packages (100% accuracy for all of the really wacky version strings).
This option was added for testing the custom functions, and I decided to leave it in just in case.
Internally it uses dpkg --compare-versions {$version1} lt {$version2}
via a system()
call., (*10)
$result = Version::compareWithDpkg($version1, $version2);
if ($result == -1) {
echo "{$version1} < {$version2}";
} elseif ($result == 0) {
echo "{$version1} == {$version2}";
} elseif ($result == 1) {
echo "{$version1} > {$version2}";
}
Retrieve a "compare string" for storing in a database, (*11)
Occasionally you need to store a large amount of version numbers in a database and then compare them in bulk using the database itself, rather than extracting the data and manipulating it in code. A "compare string" is a verbose version of the version string that can be compared using basic string comparison functions (>
<
==
), making it perfect for use in a database.., (*12)
$string = Version::getCompareString($version);