AutoUpdater: use JsonMapper to handle API response information

This commit is contained in:
Dylan K. Taylor 2020-06-18 19:03:11 +01:00
parent 2687e63645
commit 98f0447912
5 changed files with 128 additions and 59 deletions

View File

@ -41,10 +41,7 @@ class AutoUpdater{
protected $server;
/** @var string */
protected $endpoint;
/**
* @var mixed[]|null
* @phpstan-var array<string, mixed>|null
*/
/** @var UpdateInfo|null */
protected $updateInfo = null;
/** @var VersionString|null */
protected $newVersion;
@ -69,10 +66,9 @@ class AutoUpdater{
/**
* Callback used at the end of the update checking task
*
* @param mixed[] $updateInfo
* @phpstan-param array<string, mixed> $updateInfo
* @param UpdateInfo $updateInfo
*/
public function checkUpdateCallback(array $updateInfo) : void{
public function checkUpdateCallback(UpdateInfo $updateInfo) : void{
$this->updateInfo = $updateInfo;
$this->checkUpdate();
if($this->hasUpdate()){
@ -101,12 +97,12 @@ class AutoUpdater{
*/
public function showConsoleUpdate() : void{
$messages = [
"Your version of " . $this->server->getName() . " is out of date. Version " . $this->newVersion->getFullVersion(true) . " was released on " . date("D M j h:i:s Y", $this->updateInfo["date"])
"Your version of " . $this->server->getName() . " is out of date. Version " . $this->newVersion->getFullVersion(true) . " was released on " . date("D M j h:i:s Y", $this->updateInfo->date)
];
if($this->updateInfo["details_url"] !== null){
$messages[] = "Details: " . $this->updateInfo["details_url"];
if($this->updateInfo->details_url !== null){
$messages[] = "Details: " . $this->updateInfo->details_url;
}
$messages[] = "Download: " . $this->updateInfo["download_url"];
$messages[] = "Download: " . $this->updateInfo->download_url;
$this->printConsoleMessage($messages, \LogLevel::WARNING);
}
@ -148,10 +144,9 @@ class AutoUpdater{
/**
* Returns the last retrieved update data.
*
* @return mixed[]|null
* @phpstan-return array<string, mixed>|null
* @return UpdateInfo|null
*/
public function getUpdateInfo() : ?array{
public function getUpdateInfo() : ?UpdateInfo{
return $this->updateInfo;
}
@ -171,7 +166,7 @@ class AutoUpdater{
}
$currentVersion = new VersionString(\pocketmine\BASE_VERSION, \pocketmine\IS_DEVELOPMENT_BUILD, \pocketmine\BUILD_NUMBER);
try{
$newVersion = new VersionString($this->updateInfo["base_version"], $this->updateInfo["is_dev"], $this->updateInfo["build"]);
$newVersion = new VersionString($this->updateInfo->base_version, $this->updateInfo->is_dev, $this->updateInfo->build);
}catch(\InvalidArgumentException $e){
//Invalid version returned from API, assume there's no update
$this->logger->debug("Assuming no update because \"" . $e->getMessage() . "\"");

View File

@ -26,6 +26,7 @@ namespace pocketmine\updater;
use pocketmine\scheduler\AsyncTask;
use pocketmine\utils\Internet;
use function is_array;
use function is_string;
use function json_decode;
class UpdateCheckTask extends AsyncTask{
@ -52,19 +53,19 @@ class UpdateCheckTask extends AsyncTask{
if($response !== false){
$response = json_decode($response, true);
if(is_array($response)){
if(
isset($response["base_version"]) and
isset($response["is_dev"]) and
isset($response["build"]) and
isset($response["date"]) and
isset($response["download_url"])
){
$response["details_url"] = $response["details_url"] ?? null;
$this->setResult($response);
}elseif(isset($response["error"])){
if(isset($response["error"]) and is_string($response["error"])){
$this->error = $response["error"];
}else{
$this->error = "Invalid response data";
$mapper = new \JsonMapper();
$mapper->bExceptionOnMissingData = true;
$mapper->bEnforceMapType = false;
try{
/** @var UpdateInfo $responseObj */
$responseObj = $mapper->map($response, new UpdateInfo());
$this->setResult($responseObj);
}catch(\JsonMapper_Exception $e){
$this->error = "Invalid JSON response data: " . $e->getMessage();
}
}
}else{
$this->error = "Invalid response data";
@ -76,7 +77,9 @@ class UpdateCheckTask extends AsyncTask{
/** @var AutoUpdater $updater */
$updater = $this->fetchLocal(self::TLS_KEY_UPDATER);
if($this->hasResult()){
$updater->checkUpdateCallback($this->getResult());
/** @var UpdateInfo $response */
$response = $this->getResult();
$updater->checkUpdateCallback($response);
}else{
$updater->checkUpdateError($this->error);
}

View File

@ -0,0 +1,96 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\updater;
/**
* Model class for JsonMapper to represent the information returned from the updater API.
* @link https://update.pmmp.io/api
*/
final class UpdateInfo{
/**
* @var string
* @required
*/
public $job;
/**
* @var string
* @required
*/
public $php_version;
/**
* @var string
* @required
*/
public $base_version;
/**
* @var int
* @required
*/
public $build_number;
/**
* @var bool
* @required
*/
public $is_dev;
/**
* @var string
* @required
*/
public $branch;
/**
* @var string
* @required
*/
public $git_commit;
/**
* @var string
* @required
*/
public $mcpe_version;
/**
* @var string
* @required
*/
public $phar_name;
/**
* @var int
* @required
*/
public $build;
/**
* @var int
* @required
*/
public $date;
/**
* @var string
* @required
*/
public $details_url;
/**
* @var string
* @required
*/
public $download_url;
}

View File

@ -310,36 +310,11 @@ parameters:
count: 1
path: ../../../src/timings/TimingsHandler.php
-
message: "#^Parameter \\#2 \\$timestamp of function date expects int, mixed given\\.$#"
count: 1
path: ../../../src/updater/AutoUpdater.php
-
message: "#^Parameter \\#1 \\$baseVersion of class pocketmine\\\\utils\\\\VersionString constructor expects string, mixed given\\.$#"
count: 1
path: ../../../src/updater/AutoUpdater.php
-
message: "#^Parameter \\#2 \\$isDevBuild of class pocketmine\\\\utils\\\\VersionString constructor expects bool, mixed given\\.$#"
count: 1
path: ../../../src/updater/AutoUpdater.php
-
message: "#^Parameter \\#3 \\$buildNumber of class pocketmine\\\\utils\\\\VersionString constructor expects int, mixed given\\.$#"
count: 1
path: ../../../src/updater/AutoUpdater.php
-
message: "#^Parameter \\#1 \\$str of function strtolower expects string, mixed given\\.$#"
count: 1
path: ../../../src/updater/AutoUpdater.php
-
message: "#^Parameter \\#1 \\$updateInfo of method pocketmine\\\\updater\\\\AutoUpdater\\:\\:checkUpdateCallback\\(\\) expects array\\<string, mixed\\>, mixed given\\.$#"
count: 1
path: ../../../src/updater/UpdateCheckTask.php
-
message: "#^Parameter \\#3 \\$length of function substr expects int, mixed given\\.$#"
count: 1

View File

@ -800,23 +800,23 @@ parameters:
count: 1
path: ../../../src/thread/Worker.php
-
message: "#^Cannot access property \\$date on pocketmine\\\\updater\\\\UpdateInfo\\|null\\.$#"
count: 1
path: ../../../src/updater/AutoUpdater.php
-
message: "#^Cannot call method getFullVersion\\(\\) on pocketmine\\\\utils\\\\VersionString\\|null\\.$#"
count: 1
path: ../../../src/updater/AutoUpdater.php
-
message: "#^Offset 'date' does not exist on array\\<string, mixed\\>\\|null\\.$#"
count: 1
path: ../../../src/updater/AutoUpdater.php
-
message: "#^Offset 'details_url' does not exist on array\\<string, mixed\\>\\|null\\.$#"
message: "#^Cannot access property \\$details_url on pocketmine\\\\updater\\\\UpdateInfo\\|null\\.$#"
count: 2
path: ../../../src/updater/AutoUpdater.php
-
message: "#^Offset 'download_url' does not exist on array\\<string, mixed\\>\\|null\\.$#"
message: "#^Cannot access property \\$download_url on pocketmine\\\\updater\\\\UpdateInfo\\|null\\.$#"
count: 1
path: ../../../src/updater/AutoUpdater.php