[Cs-versionparse-commits] SF.net SVN: cs-versionparse:[12] trunk/0.1/cs_version.abstract.class.php
Status: Inactive
Brought to you by:
crazedsanity
From: <cra...@us...> - 2009-01-26 17:26:29
|
Revision: 12 http://cs-versionparse.svn.sourceforge.net/cs-versionparse/?rev=12&view=rev Author: crazedsanity Date: 2009-01-26 17:26:22 +0000 (Mon, 26 Jan 2009) Log Message: ----------- Enhancements for determining higher versions & parsing suffixes. /cs_version.abstract.class.php: * MAIN::: -- new private var, fullVersionString: caches the derived full version string (may be used later...) -- new private var, suffixList: contains all valid suffixes & notes about their intended meanings, which explains why they're orderd as such. * get_version(): -- calls parse_version_string() to get information as an array. -- calls build_full_version_string() to build the version string. * parse_version_string() [NEW]: -- parses full version string into it's respective bits (major, minor, maintenance, and suffix). * build_full_version_string() [NEW]: -- uses data from parse_version_string() to build the full version string. * is_higher_version() [NEW]: -- checks if a given version is higher than another given version. -- NOTE: this logic was pulled from the upgradeClass from cs-project, in hopes that eventually this will deprecate that code. * parse_suffix() [NEW]: -- parses the suffix to help determine if one suffix is higher or lower than another (for is_higher_version()). Modified Paths: -------------- trunk/0.1/cs_version.abstract.class.php Modified: trunk/0.1/cs_version.abstract.class.php =================================================================== --- trunk/0.1/cs_version.abstract.class.php 2009-01-26 05:43:34 UTC (rev 11) +++ trunk/0.1/cs_version.abstract.class.php 2009-01-26 17:26:22 UTC (rev 12) @@ -13,8 +13,19 @@ abstract class cs_versionAbstract { public $isTest = FALSE; + + + private $versionFileLocation=null; + private $fullVersionString; + private $suffixList = array( + 'ALPHA', //very unstable + 'BETA', //kinda unstable, but probably useable + 'RC' //all known bugs fixed, searching for unknown ones + ); + + abstract public function __construct(); @@ -34,43 +45,16 @@ if($findIt == 1 && count($matches) == 2) { $fullVersionString = $matches[1]; - $pieces = explode('.', $fullVersionString); - $retval = array( - 'version_major' => $pieces[0], - 'version_minor' => $pieces[1], - 'version_maintenance' => $pieces[2] - ); - if(!strlen($retval['version_maintenance'])) { - $retval['version_maintenance'] = 0; - } + $versionInfo = $this->parse_version_string($fullVersionString); + $this->fullVersionString = $this->build_full_version_string($versionInfo); - if(preg_match('/-/', $retval['version_maintenance'])) { - $bits = explode('-', $retval['version_maintenance']); - $retval['version_maintenance'] = $bits[0]; - $suffix = $bits[1]; - } - else { - $suffix = ""; - } - $fullVersionString = ""; - foreach(array_values($retval) as $chunk) { - if(strlen($fullVersionString)) { - $fullVersionString .= '.'; - } - $fullVersionString .= $chunk; - } - if(strlen($suffix)) { - $fullVersionString .= '-'. $suffix; - } - - if($asArray) { - $retval['version_suffix'] = $suffix; - $retval['version_string'] = $fullVersionString; + $retval = $versionInfo; + $retval['version_string'] = $this->fullVersionString; } else { - $retval = $fullVersionString; + $retval = $this->build_full_version_string($versionInfo); } } else { @@ -160,5 +144,247 @@ //========================================================================= + + //========================================================================= + /** + * + * TODO: add logic to split apart the suffix (i.e. "-ALPHA5" broken into "ALPHA" and "5"). + */ + public function parse_version_string($version) { + if(is_string($version) && strlen($version) && preg_match('/\./', $version)) { + $version = preg_replace('/ /', '', $version); + + $pieces = explode('.', $version); + $retval = array( + 'version_major' => $pieces[0], + 'version_minor' => $pieces[1], + 'version_maintenance' => $pieces[2] + ); + if(!strlen($retval['version_maintenance'])) { + $retval['version_maintenance'] = 0; + } + + if(preg_match('/-/', $retval['version_maintenance'])) { + $bits = explode('-', $retval['version_maintenance']); + $retval['version_maintenance'] = $bits[0]; + $suffix = $bits[1]; + } + elseif(preg_match('/-/', $retval['version_minor'])) { + $bits = explode('-', $retval['version_minor']); + $retval['version_minor'] = $bits[0]; + $suffix = $bits[1]; + } + else { + $suffix = ""; + } + $retval['version_suffix'] = $suffix; + } + else { + throw new exception(__METHOD__ .": invalid version string passed (". $version .")"); + } + + return($retval); + }//end parse_version_string() + //========================================================================= + + + + //========================================================================= + public function build_full_version_string(array $versionInfo) { + $requiredIndexes = array( + 'version_major', 'version_minor', 'version_maintenance', 'version_suffix' + ); + + $missing=""; + $count=0; + foreach($requiredIndexes as $indexName) { + if(isset($versionInfo[$indexName])) { + $count++; + } + else { + if(strlen($missing)) { + $missing .= ", ". $indexName; + } + else { + $missing = $indexName; + } + } + } + + if($count == count($requiredIndexes) && !strlen($missing)) { + $suffix = $versionInfo['version_suffix']; + unset($versionInfo['version_suffix']); + + $retval = ""; + $gf = new cs_globalFunctions; + foreach($versionInfo as $name=>$value) { + if(strlen($retval)) { + $retval .= ".". $value; + } + else { + $retval = $value; + } + } + if(strlen($suffix)) { + $retval .= "-". $suffix; + } + } + else { + cs_debug_backtrace(1); + throw new exception(__METHOD__ .": missing indexes in given array (". $missing .")"); + } + + return($retval); + + }//end build_full_version_string() + //========================================================================= + + + + //========================================================================= + public function is_higher_version($version, $checkIfHigher) { + $retval = FALSE; + $this->gfObj = new cs_globalFunctions; + if(!is_string($version) || !is_string($checkIfHigher)) { + throw new exception(__METHOD__ .": no valid version strings, version=(". $version ."), checkIfHigher=(". $checkIfHigher .")"); + } + elseif($version == $checkIfHigher) { + $retval = FALSE; + } + else { + $curVersionArr = $this->parse_version_string($version); + $checkVersionArr = $this->parse_version_string($checkIfHigher); + + unset($curVersionArr['version_string'], $checkVersionArr['version_string']); + + + $curVersionSuffix = $curVersionArr['version_suffix']; + $checkVersionSuffix = $checkVersionArr['version_suffix']; + + + unset($curVersionArr['version_suffix']); + + foreach($curVersionArr as $index=>$versionNumber) { + $checkThis = $checkVersionArr[$index]; + + if(is_numeric($checkThis) && is_numeric($versionNumber)) { + //set them as integers. + settype($versionNumber, 'int'); + settype($checkThis, 'int'); + + if($checkThis > $versionNumber) { + $retval = TRUE; + break; + } + elseif($checkThis == $versionNumber) { + //they're equal... + } + else { + //TODO: should there maybe be an option to throw an exception (freak out) here? + } + } + else { + throw new exception(__METHOD__ .": ". $index ." is not numeric in one of the strings " . + "(versionNumber=". $versionNumber .", checkThis=". $checkThis .")"); + } + } + + //now deal with those damnable suffixes, but only if the versions are so far identical: if + // the "$checkIfHigher" is actually higher, don't bother (i.e. suffixes don't matter when + // we already know there's a major, minor, or maintenance version that's also higher. + if($retval === FALSE) { + //EXAMPLE: $version="1.0.0-BETA3", $checkIfHigher="1.1.0" + // Moving from a non-suffixed version to a suffixed version isn't supported, but the inverse is: + // i.e. (1.0.0-BETA3 to 1.0.0) is okay, but (1.0.0 to 1.0.0-BETA3) is NOT. + // Also: (1.0.0-BETA3 to 1.0.0-BETA4) is okay, but (1.0.0-BETA4 to 1.0.0-BETA3) is NOT. + if(strlen($curVersionSuffix) && strlen($checkVersionSuffix) && $curVersionSuffix == $checkVersionSuffix) { + //matching suffixes. + } + elseif(strlen($curVersionSuffix) || strlen($checkVersionSuffix)) { + //we know the suffixes are there and DO match. + if(strlen($curVersionSuffix) && strlen($checkVersionSuffix)) { + //okay, here's where we do some crazy things... + $curVersionData = $this->parse_suffix($curVersionSuffix); + $checkVersionData = $this->parse_suffix($checkVersionSuffix); + + if($curVersionData['type'] == $checkVersionData['type']) { + //got the same suffix type (like "BETA"), check the number. + if($checkVersionData['number'] > $curVersionData['number']) { + //new version's suffix number higher than current... + $retval = TRUE; + } + elseif($checkVersionData['number'] == $curVersionData['number']) { + //new version's suffix number is EQUAL TO current... + $retval = FALSE; + } + else { + //new version's suffix number is LESS THAN current... + $retval = FALSE; + } + } + else { + //not the same suffix... see if the new one is higher. + $suffixValues = array_flip($this->suffixList); + if($suffixValues[$checkVersionData['type']] > $suffixValues[$curVersionData['type']]) { + $retval = TRUE; + } + else { + //current suffix type is higher... + } + } + + } + elseif(strlen($curVersionSuffix) && !strlen($checkVersionSuffix)) { + //i.e. "1.0.0-BETA1" to "1.0.0" --->>> OKAY! + $retval = TRUE; + } + elseif(!strlen($curVersionSuffix) && strlen($checkVersionSuffix)) { + //i.e. "1.0.0" to "1.0.0-BETA1" --->>> NOT ACCEPTABLE! + } + } + else { + //no suffix to care about + } + } + } + + return($retval); + + }//end is_higher_version() + //========================================================================= + + + + //========================================================================= + protected function parse_suffix($suffix) { + $retval = NULL; + if(strlen($suffix)) { + //determine what kind it is. + foreach($this->suffixList as $type) { + if(preg_match('/^'. $type .'/', $suffix)) { + $checkThis = preg_replace('/^'. $type .'/', '', $suffix); + if(strlen($checkThis) && is_numeric($checkThis)) { + //oooh... it's something like "BETA3" + $retval = array( + 'type' => $type, + 'number' => $checkThis + ); + } + else { + throw new exception(__METHOD__ .": invalid suffix (". $suffix .")"); + } + break; + } + } + } + else { + throw new exception(__METHOD__ .": invalid suffix (". $suffix .")"); + } + + return($retval); + }//end parse_suffix() + //========================================================================= + + } ?> \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |