From 70a3c68f730927ef176a6cdf78c436a51a049e60 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 18 Apr 2012 01:17:32 +0200 Subject: [PATCH] Add package stability --- src/Composer/Package/AliasPackage.php | 12 ++++++++- src/Composer/Package/MemoryPackage.php | 11 +++++++- src/Composer/Package/PackageInterface.php | 7 +++++ .../Package/Version/VersionParser.php | 27 ++++++++++++++----- .../Package/Version/VersionParserTest.php | 22 ++++++++------- 5 files changed, 61 insertions(+), 18 deletions(-) diff --git a/src/Composer/Package/AliasPackage.php b/src/Composer/Package/AliasPackage.php index 6670b93e5..6835226d8 100644 --- a/src/Composer/Package/AliasPackage.php +++ b/src/Composer/Package/AliasPackage.php @@ -28,6 +28,7 @@ class AliasPackage extends BasePackage protected $dev; protected $aliasOf; protected $rootPackageAlias = false; + protected $stability; protected $requires; protected $conflicts; @@ -50,7 +51,8 @@ class AliasPackage extends BasePackage $this->version = $version; $this->prettyVersion = $prettyVersion; $this->aliasOf = $aliasOf; - $this->dev = VersionParser::isDev($version); + $this->stability = VersionParser::parseStability($version); + $this->dev = $this->stability === 'dev'; // replace self.version dependencies foreach (array('requires', 'devRequires') as $type) { @@ -91,6 +93,14 @@ class AliasPackage extends BasePackage return $this->version; } + /** + * {@inheritDoc} + */ + public function getStability() + { + return $this->stability; + } + /** * {@inheritDoc} */ diff --git a/src/Composer/Package/MemoryPackage.php b/src/Composer/Package/MemoryPackage.php index 147f94ed7..cc8f9ff1c 100644 --- a/src/Composer/Package/MemoryPackage.php +++ b/src/Composer/Package/MemoryPackage.php @@ -71,7 +71,8 @@ class MemoryPackage extends BasePackage $this->version = $version; $this->prettyVersion = $prettyVersion; - $this->dev = VersionParser::isDev($version); + $this->stability = VersionParser::parseStability($version); + $this->dev = $this->stability === 'dev'; } /** @@ -98,6 +99,14 @@ class MemoryPackage extends BasePackage return $this->type ?: 'library'; } + /** + * {@inheritDoc} + */ + public function getStability() + { + return $this->stability; + } + /** * @param string $targetDir */ diff --git a/src/Composer/Package/PackageInterface.php b/src/Composer/Package/PackageInterface.php index 5b8d2ddb2..43a07d28b 100644 --- a/src/Composer/Package/PackageInterface.php +++ b/src/Composer/Package/PackageInterface.php @@ -180,6 +180,13 @@ interface PackageInterface */ function getPrettyVersion(); + /** + * Returns the stability of this package: one of (dev, alpha, beta, RC, stable) + * + * @return string + */ + function getStability(); + /** * Returns the package license, e.g. MIT, BSD, GPL * diff --git a/src/Composer/Package/Version/VersionParser.php b/src/Composer/Package/Version/VersionParser.php index 57d0596cc..374497ad4 100644 --- a/src/Composer/Package/Version/VersionParser.php +++ b/src/Composer/Package/Version/VersionParser.php @@ -22,17 +22,30 @@ use Composer\Package\LinkConstraint\VersionConstraint; */ class VersionParser { - private $modifierRegex = '[.-]?(?:(beta|RC|alpha|patch|pl|p)(?:[.-]?(\d+))?)?([.-]?dev)?'; + private static $modifierRegex = '[.-]?(?:(beta|RC|alpha|patch|pl|p)(?:[.-]?(\d+))?)?([.-]?dev)?'; /** - * Checks if a version is dev or not + * Returns the stability of a version * * @param string $version - * @return Boolean + * @return string */ - static public function isDev($version) + static public function parseStability($version) { - return 'dev-' === substr($version, 0, 4) || '-dev' === substr($version, -4); + if ('dev-' === substr($version, 0, 4) || '-dev' === substr($version, -4)) { + return 'dev'; + } + + preg_match('{'.self::$modifierRegex.'$}', $version, $match); + if (!empty($match[3])) { + return 'dev'; + } + + if (!empty($match[1]) && ($match[1] === 'beta' || $match[1] === 'alpha' || $match[1] === 'RC')) { + return $match[1]; + } + + return 'stable'; } /** @@ -60,13 +73,13 @@ class VersionParser } // match classical versioning - if (preg_match('{^v?(\d{1,3})(\.\d+)?(\.\d+)?(\.\d+)?'.$this->modifierRegex.'$}i', $version, $matches)) { + if (preg_match('{^v?(\d{1,3})(\.\d+)?(\.\d+)?(\.\d+)?'.self::$modifierRegex.'$}i', $version, $matches)) { $version = $matches[1] .(!empty($matches[2]) ? $matches[2] : '.0') .(!empty($matches[3]) ? $matches[3] : '.0') .(!empty($matches[4]) ? $matches[4] : '.0'); $index = 5; - } elseif (preg_match('{^v?(\d{4}(?:[.:-]?\d{2}){1,6}(?:[.:-]?\d{1,3})?)'.$this->modifierRegex.'$}i', $version, $matches)) { // match date-based versioning + } elseif (preg_match('{^v?(\d{4}(?:[.:-]?\d{2}){1,6}(?:[.:-]?\d{1,3})?)'.self::$modifierRegex.'$}i', $version, $matches)) { // match date-based versioning $version = preg_replace('{\D}', '-', $matches[1]); $index = 2; } diff --git a/tests/Composer/Test/Package/Version/VersionParserTest.php b/tests/Composer/Test/Package/Version/VersionParserTest.php index fa123e7dc..c0eefb14c 100644 --- a/tests/Composer/Test/Package/Version/VersionParserTest.php +++ b/tests/Composer/Test/Package/Version/VersionParserTest.php @@ -195,21 +195,25 @@ class VersionParserTest extends \PHPUnit_Framework_TestCase } /** - * @dataProvider dataIsDev + * @dataProvider stabilityProvider */ - public function testIsDev($expected, $version) + public function testParseStability($expected, $version) { - $this->assertSame($expected, VersionParser::isDev($version)); + $this->assertSame($expected, VersionParser::parseStability($version)); } - public function dataIsDev() + public function stabilityProvider() { return array( - array(false, '1.0'), - array(false, 'v2.0.*'), - array(false, '3.0dev'), - array(true, 'dev-master'), - array(true, '3.1.2-dev'), + array('stable', '1.0'), + array('dev', 'v2.0.x-dev'), + array('RC', '3.0-RC2'), + array('dev', 'dev-master'), + array('dev', '3.1.2-dev'), + array('stable', '3.1.2-pl2'), + array('stable', '3.1.2-patch'), + array('alpha', '3.1.2-alpha5'), + array('beta', '3.1.2-beta'), ); } }