From 2cd45acbdf46a5040ad7da6ff1d8f3ac68ee5877 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Fri, 19 Jan 2018 18:44:17 -0500 Subject: [PATCH] Show if there's a new version available on the admin dashboard #143 --- app/Facades/Utils.php | 28 ++++++++++ .../Controllers/Admin/DashboardController.php | 26 ++++++++- composer.json | 3 +- composer.lock | 54 ++++++++++++++++++- config/phpvms.php | 5 ++ config/version.yml | 2 +- 6 files changed, 114 insertions(+), 4 deletions(-) diff --git a/app/Facades/Utils.php b/app/Facades/Utils.php index b4e99e94..84de8487 100644 --- a/app/Facades/Utils.php +++ b/app/Facades/Utils.php @@ -2,6 +2,7 @@ namespace App\Facades; +use GuzzleHttp\Client; use \Illuminate\Support\Facades\Facade; class Utils extends Facade @@ -11,6 +12,33 @@ class Utils extends Facade return 'utils'; } + /** + * Download a URI. If a file is given, it will save the downloaded + * content into that file + * @param $uri + * @param null $file + * @return string + * @throws \RuntimeException + */ + public static function downloadUrl($uri, $file=null) + { + $opts = []; + if($file !== null) { + $opts['sink'] = $file; + } + + $client = new Client(); + $response = $client->request('GET', $uri, $opts); + + $body = $response->getBody()->getContents(); + if($response->getHeader('content-type') === 'application/json') { + $body = \GuzzleHttp\json_decode($body); + } + + return $body; + } + + /** * Returns a 40 character API key that a user can use * @return string diff --git a/app/Http/Controllers/Admin/DashboardController.php b/app/Http/Controllers/Admin/DashboardController.php index a28206c2..0bddca83 100644 --- a/app/Http/Controllers/Admin/DashboardController.php +++ b/app/Http/Controllers/Admin/DashboardController.php @@ -3,12 +3,17 @@ namespace App\Http\Controllers\Admin; use Auth; +use Flash; +use Version; use Illuminate\Http\Request; +use App\Facades\Utils; use App\Repositories\NewsRepository; use App\Repositories\PirepRepository; use App\Repositories\UserRepository; +use vierbergenlars\SemVer\version as semver; + class DashboardController extends BaseController { private $newsRepo, $pirepRepo, $userRepo; @@ -24,10 +29,29 @@ class DashboardController extends BaseController } /** - * Display a listing of the Airlines. + * Check if a new version is available by checking the VERSION file from + * S3 and then using the semver library to do the comparison. Just show + * a session flash file on this page that'll get cleared right away + * @throws \RuntimeException + */ + protected function checkNewVersion() + { + $current_version = new semver(Version::compact()); + $latest_version = new semver(Utils::downloadUrl(config('phpvms.version_file'))); + + if(semver::gt($latest_version, $current_version)) { + Flash::warning('New version '.$latest_version.' is available!'); + } + } + + /** + * Show the admin dashboard + * @throws \RuntimeException */ public function index(Request $request) { + $this->checkNewVersion(); + return view('admin.dashboard.index', [ 'news' => $this->newsRepo->getLatest(), 'pending_pireps' => $this->pirepRepo->getPendingCount(), diff --git a/composer.json b/composer.json index 98b74503..daa0890e 100755 --- a/composer.json +++ b/composer.json @@ -49,7 +49,8 @@ "league/iso3166": "^2.1", "google/apiclient": "^2.0", "theiconic/php-ga-measurement-protocol": "^2.0", - "irazasyed/laravel-gamp": "^1.3" + "irazasyed/laravel-gamp": "^1.3", + "vierbergenlars/php-semver": "^3.0" }, "require-dev": { "phpunit/phpunit": "6.4.0", diff --git a/composer.lock b/composer.lock index e62be230..4f12c43f 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "6778fdaa04275c7824a22188522f844c", + "content-hash": "9fc46733ea4ac9fb3c249612e2e302cf", "packages": [ { "name": "arrilot/laravel-widgets", @@ -5130,6 +5130,58 @@ ], "time": "2015-02-23T12:40:40+00:00" }, + { + "name": "vierbergenlars/php-semver", + "version": "3.0.2", + "source": { + "type": "git", + "url": "https://github.com/vierbergenlars/php-semver.git", + "reference": "be22b86be4c1133acc42fd1685276792024af5f9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/vierbergenlars/php-semver/zipball/be22b86be4c1133acc42fd1685276792024af5f9", + "reference": "be22b86be4c1133acc42fd1685276792024af5f9", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "require-dev": { + "phpunit/phpunit": "~4" + }, + "bin": [ + "bin/semver", + "bin/update-versions" + ], + "type": "library", + "autoload": { + "psr-0": { + "vierbergenlars\\SemVer\\": "src/", + "vierbergenlars\\LibJs\\": "src/" + }, + "classmap": [ + "src/vierbergenlars/SemVer/internal.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Lars Vierbergen", + "email": "vierbergenlars@gmail.com" + } + ], + "description": "The Semantic Versioner for PHP", + "keywords": [ + "semantic", + "semver", + "versioning" + ], + "time": "2017-07-11T09:53:59+00:00" + }, { "name": "vlucas/phpdotenv", "version": "v2.4.0", diff --git a/config/phpvms.php b/config/phpvms.php index a44fa8be..087d1fa2 100644 --- a/config/phpvms.php +++ b/config/phpvms.php @@ -41,4 +41,9 @@ return [ * Misc Settings */ 'news_feed_url' => 'http://forum.phpvms.net/rss/1-announcements-feed.xml/?', + + /** + * URL to the latest version file + */ + 'version_file' => 'http://downloads.phpvms.net/VERSION', ]; diff --git a/config/version.yml b/config/version.yml index 5070bcc2..cf33299e 100644 --- a/config/version.yml +++ b/config/version.yml @@ -9,7 +9,7 @@ build: mode: number length: 6 increment_by: 1 - number: 180119-6e12c0 + number: 180119-a1b989 git_absorb: git-local git: git-local: 'git rev-parse --verify HEAD'