Skip to content

Commit

Permalink
Switched semver package, closes #37
Browse files Browse the repository at this point in the history
  • Loading branch information
thelfensdrfer committed Dec 29, 2017
1 parent e1b0a4b commit e0d60c7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 38 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
"minimum-stability": "stable",
"require": {
"desarrolla2/cache": "2.1.*",
"monolog/monolog": "1.22.*",
"monolog/monolog": "1.23.*",
"php": ">= 5.6.4",
"vierbergenlars/php-semver": "3.0.*"
"composer/semver": "1.4.*"
},
"require-dev": {
"phpunit/phpunit": "5.7.*"
"phpunit/phpunit": "6.5.*"
},
"autoload": {
"psr-4": {
Expand Down
64 changes: 29 additions & 35 deletions src/AutoUpdate.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<?php namespace VisualAppeal;

use \vierbergenlars\SemVer\version;
use \vierbergenlars\SemVer\expression;
use \vierbergenlars\SemVer\SemVerException;
use Composer\Semver\Comparator;
use Composer\Semver\Semver;

use \Desarrolla2\Cache\Cache;
use \Desarrolla2\Cache\Adapter\NotCache;
use Desarrolla2\Cache\Cache;
use Desarrolla2\Cache\Adapter\NotCache;

use \Monolog\Logger;
use \Monolog\Handler\NullHandler;
use Monolog\Logger;
use Monolog\Handler\NullHandler;

/**
* Auto update class.
Expand All @@ -18,7 +17,7 @@ class AutoUpdate
/**
* The latest version.
*
* @var vierbergenlars\SemVer\version
* @var string
*/
private $_latestVersion = null;

Expand Down Expand Up @@ -81,7 +80,7 @@ class AutoUpdate
/**
* Current version.
*
* @var vierbergenlars\SemVer\version
* @var string
*/
protected $_currentVersion = null;

Expand Down Expand Up @@ -184,8 +183,8 @@ public function __construct($tempDir = null, $installDir = null, $maxExecutionTi
$this->setTempDir(($tempDir !== null) ? $tempDir : __DIR__ . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR);
$this->setInstallDir(($installDir !== null) ? $installDir : __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR);

$this->_latestVersion = new version('0.0.0');
$this->_currentVersion = new version('0.0.0');
$this->_latestVersion = '0.0.0';
$this->_currentVersion = '0.0.0';

// Init cache
$this->_cache = new Cache(new NotCache());
Expand Down Expand Up @@ -306,14 +305,7 @@ public function setCache($adapter, $ttl = 3600)
*/
public function setCurrentVersion($currentVersion)
{
$version = new version($currentVersion);
if ($version->valid() === null) {
$this->_log->addError(sprintf('Invalid current version "%s"', $currentVersion));

return false;
}

$this->_currentVersion = $version;
$this->_currentVersion = $currentVersion;

return $this;
}
Expand Down Expand Up @@ -362,7 +354,7 @@ public function addLogHandler(\Monolog\Handler\HandlerInterface $handler)
/**
* Get the name of the latest version.
*
* @return vierbergenlars\SemVer\version
* @return string
*/
public function getLatestVersion()
{
Expand All @@ -376,9 +368,13 @@ public function getLatestVersion()
*/
public function getVersionsToUpdate()
{
return array_map(function ($update) {
return $update['version'];
}, $this->_updates);
if (count($this->_updates) > 0) {
return array_map(function ($update) {
return $update['version'];
}, $this->_updates);
}

return [];
}

/**
Expand Down Expand Up @@ -432,7 +428,7 @@ public function checkUpdate()
$this->_log->addNotice('Checking for a new update...');

// Reset previous updates
$this->_latestVersion = new version('0.0.0');
$this->_latestVersion = '0.0.0';
$this->_updates = [];

$versions = $this->_cache->get('update-versions');
Expand Down Expand Up @@ -497,15 +493,9 @@ public function checkUpdate()
}

// Check for latest version
foreach ($versions as $versionRaw => $updateUrl) {
$version = new version($versionRaw);
if ($version->valid() === null) {
$this->_log->addInfo(sprintf('Could not parse version "%s" from update server "%s"', $versionRaw, $updateFile));
continue;
}

if (version::gt($version, $this->_currentVersion)) {
if (version::gt($version, $this->_latestVersion))
foreach ($versions as $version => $updateUrl) {
if (Comparator::greaterThan($version, $this->_currentVersion)) {
if (Comparator::greaterThan($version, $this->_latestVersion))
$this->_latestVersion = $version;

$this->_updates[] = [
Expand All @@ -517,7 +507,11 @@ public function checkUpdate()

// Sort versions to install
usort($this->_updates, function ($a, $b) {
return version::compare($a['version'], $b['version']);
if (Comparator::equalTo($a['version'], $b['version'])) {
return 0;
}

return Comparator::lessThan($a['version'], $b['version']) ? -1 : 1;
});

if ($this->newVersionAvailable()) {
Expand All @@ -538,7 +532,7 @@ public function checkUpdate()
*/
public function newVersionAvailable()
{
return version::gt($this->_latestVersion, $this->_currentVersion);
return Comparator::greaterThan($this->_latestVersion, $this->_currentVersion);
}

/**
Expand Down

0 comments on commit e0d60c7

Please sign in to comment.