Use composer/semver to compare versions

Remove Utils::versionCompare method.
This commit is contained in:
printempw 2018-06-30 16:31:39 +08:00
parent 40485253ec
commit f2477f437b
4 changed files with 9 additions and 67 deletions

View File

@ -11,6 +11,7 @@ use Storage;
use Artisan;
use App\Models\User;
use Illuminate\Http\Request;
use Composer\Semver\Comparator;
use Illuminate\Validation\Validator;
use App\Exceptions\PrettyPageException;
@ -119,7 +120,7 @@ class SetupController extends Controller
public function update()
{
if (Utils::versionCompare(config('app.version'), option('version', ''), '<=')) {
if (Comparator::lessThanOrEqualTo(config('app.version'), option('version'))) {
// No updates available
return view('setup.locked');
}
@ -140,7 +141,7 @@ class SetupController extends Controller
// Skip if the file is not valid or expired
if (! isset($matches[2]) ||
Utils::versionCompare($matches[2], config('app.version'), '<')) {
Comparator::lessThan($matches[2], config('app.version'))) {
continue;
}

View File

@ -11,6 +11,7 @@ use Storage;
use ZipArchive;
use App\Services\OptionForm;
use Illuminate\Http\Request;
use Composer\Semver\Comparator;
class UpdateController extends Controller
{
@ -46,9 +47,9 @@ class UpdateController extends Controller
if ($this->getUpdateInfo()) {
$info['latest_version'] = $this->getUpdateInfo('latest_version');
$info['new_version_available'] = Utils::versionCompare(
$info['new_version_available'] = Comparator::greaterThan(
$info['latest_version'],
$info['current_version'], '>'
$info['current_version']
);
if ($detail = $this->getReleaseInfo($info['latest_version'])) {
@ -96,7 +97,7 @@ class UpdateController extends Controller
{
$latest = $this->getUpdateInfo('latest_version');
return Utils::versionCompare($latest, $this->currentVersion, '>') && $this->getReleaseInfo($latest);
return Comparator::greaterThan($latest, $this->currentVersion) && $this->getReleaseInfo($latest);
}
public function download(Request $request)

View File

@ -6,6 +6,7 @@ use DB;
use View;
use Utils;
use Illuminate\Http\Request;
use Composer\Semver\Comparator;
use Illuminate\Support\ServiceProvider;
use App\Exceptions\PrettyPageException;
use App\Http\Controllers\SetupController;
@ -80,7 +81,7 @@ class BootServiceProvider extends ServiceProvider
return redirect('/setup')->send();
}
if (Utils::versionCompare(config('app.version'), option('version', ''), '>')) {
if (Comparator::greaterThan(config('app.version'), option('version'))) {
return redirect('/setup/update')->send();
}

View File

@ -56,67 +56,6 @@ class Utils
return false;
}
/**
* Compares two "PHP-standardized" version number strings.
* Unlike version_compare(), this method will determine that versions with suffix are lower.
*
* e.g. 3.2-beta > 3.2-alpha
* 3.2 > 3.2-beta
* 3.2 > 3.2-pre-release
* 3.2 > 3.2-pr8
*
* @param string $version1
* @param string $version2
* @param string $operator
* @return mixed
*/
public static function versionCompare($version1, $version2, $operator = null)
{
$versions = [$version1, $version2];
// Pre-processing for version contains hyphen
foreach ([0, 1] as $offset) {
if (false !== ($result = self::parseVersionWithHyphen($versions[$offset]))) {
$versions[$offset] = $result;
} else {
$versions[$offset] = ['main' => $versions[$offset], 'sub' => ''];
}
}
if (version_compare($versions[0]['main'], $versions[1]['main'], '=')) {
$sub1 = $versions[0]['sub'];
$sub2 = $versions[1]['sub'];
// v3.2-pr < v3.2
if ($sub1 != "" || $sub2 != "") {
// If both of sub-versions are not empty
if ($sub1 != "" && $sub2 != "") {
return version_compare($sub1, $sub2, $operator);
} else {
$result = version_compare($sub1, $sub2, $operator);
// Reverse the result since version_compare() will determine that "beta" > ""
return ($operator == "=") ? $result : !$result;
}
}
}
return version_compare($versions[0]['main'], $versions[1]['main'], $operator);
}
public static function parseVersionWithHyphen($version)
{
preg_match('/([^-]*)-(.*)/', $version, $matches);
if (isset($matches[2])) {
return [
'main' => $matches[1],
'sub' => $matches[2]
];
}
return false;
}
public static function download($url, $path)
{
@set_time_limit(0);