Skip to content

Commit

Permalink
[jan] Add Components_Component#getPreviousVersion().
Browse files Browse the repository at this point in the history
  • Loading branch information
yunosh committed Apr 5, 2016
1 parent 3f8e4bd commit 0714fba
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
7 changes: 7 additions & 0 deletions components/lib/Components/Component.php
Expand Up @@ -55,6 +55,13 @@ public function getDescription();
*/
public function getVersion();

/**
* Returns the previous version of the component.
*
* @return string The previous component version.
*/
public function getPreviousVersion();

/**
* Return the last release date of the component.
*
Expand Down
32 changes: 32 additions & 0 deletions components/lib/Components/Component/Base.php
Expand Up @@ -98,6 +98,38 @@ public function getVersion()
return $this->getPackageXml()->getVersion();
}

/**
* Returns the previous version of the component.
*
* @return string The previous component version.
*/
public function getPreviousVersion()
{
$previousVersion = null;
$currentVersion = $this->getVersion();
$currentState = $this->getState();
$versions = $this->getPackageXml()->getVersions();
usort(
$versions,
function($a, $b) {
return version_compare($a['version'], $b['version']);
}
);
foreach ($versions as $version) {
// If this is a stable version we want the previous stable version,
// otherwise use any previous version.
if ($currentState == 'stable' &&
$version['stability'] != 'stable') {
continue;
}
if (version_compare($version['version'], $currentVersion, '>=')) {
return $previousVersion;
}
$previousVersion = $version['version'];
}
return $previousVersion;
}

/**
* Return the last release date of the component.
*
Expand Down
28 changes: 28 additions & 0 deletions components/lib/Components/Component/Remote.php
Expand Up @@ -138,6 +138,34 @@ public function getVersion()
return $this->_version;
}

/**
* Returns the previous version of the component.
*
* @return string The previous component version.
*/
public function getPreviousVersion()
{
$previousVersion = null;
$currentVersion = $this->getVersion();
$currentState = $this->getState();
$releases = $this->_remote->getReleases();
$versions = $releases->listReleases();
usort($versions, 'version_compare');
foreach ($versions as $version) {
// If this is a stable version we want the previous stable version,
// otherwise use any previous version.
if ($currentState == 'stable' &&
$releases->getReleaseStability($version) != 'stable') {
continue;
}
if (version_compare($version, $currentVersion, '>=')) {
return $previousVersion;
}
$previousVersion = $version;
}
return $previousVersion;
}

/**
* Return the channel of the component.
*
Expand Down
2 changes: 2 additions & 0 deletions components/package.xml
Expand Up @@ -21,6 +21,7 @@
</stability>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
* [jan] Add Components_Component_Base#getPreviousVersion().
* [jan] Bundle QC tools.
* [jan] Remove Freecode support.
* [jan] Update website versions after releasing a package.
Expand Down Expand Up @@ -4581,6 +4582,7 @@
<date>2016-01-13</date>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
* [jan] Add Components_Component_Base#getPreviousVersion().
* [jan] Bundle QC tools.
* [jan] Remove Freecode support.
* [jan] Update website versions after releasing a package.
Expand Down

0 comments on commit 0714fba

Please sign in to comment.