From 2c29634d03659013b58697d55e64c4b3582aa579 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sat, 25 Jul 2020 19:17:33 +0100 Subject: [PATCH] Remove VERSION and GIT_COMMIT constants these are now lazily computed in VersionInfo as needed. --- build/make-release.php | 2 +- src/CrashDump.php | 7 ++-- src/PocketMine.php | 17 --------- src/Server.php | 4 +- src/VersionInfo.php | 37 +++++++++++++++++++ src/stats/SendUsageTask.php | 3 +- src/updater/AutoUpdater.php | 2 +- src/utils/Internet.php | 2 +- tests/phpstan/bootstrap.php | 2 - .../check-explicit-mixed-baseline.neon | 10 ++--- 10 files changed, 51 insertions(+), 35 deletions(-) diff --git a/build/make-release.php b/build/make-release.php index a89b22383..f79675ef5 100644 --- a/build/make-release.php +++ b/build/make-release.php @@ -61,7 +61,7 @@ function main(array $argv) : void{ if(isset($argv[1])){ $currentVer = new VersionString($argv[1]); }else{ - $currentVer = new VersionString(VersionInfo::BASE_VERSION); + $currentVer = VersionInfo::getVersionObj(); } $nextVer = new VersionString(sprintf( "%u.%u.%u", diff --git a/src/CrashDump.php b/src/CrashDump.php index adea192f9..8fb1f9624 100644 --- a/src/CrashDump.php +++ b/src/CrashDump.php @@ -30,7 +30,6 @@ use pocketmine\plugin\PluginBase; use pocketmine\plugin\PluginManager; use pocketmine\utils\Filesystem; use pocketmine\utils\Utils; -use pocketmine\utils\VersionString; use function base64_encode; use function date; use function error_get_last; @@ -308,14 +307,14 @@ class CrashDump{ } private function generalData() : void{ - $version = new VersionString(VersionInfo::BASE_VERSION, VersionInfo::IS_DEVELOPMENT_BUILD, VersionInfo::BUILD_NUMBER); + $version = VersionInfo::getVersionObj(); $this->data["general"] = []; $this->data["general"]["name"] = $this->server->getName(); $this->data["general"]["base_version"] = VersionInfo::BASE_VERSION; $this->data["general"]["build"] = VersionInfo::BUILD_NUMBER; $this->data["general"]["is_dev"] = VersionInfo::IS_DEVELOPMENT_BUILD; $this->data["general"]["protocol"] = ProtocolInfo::CURRENT_PROTOCOL; - $this->data["general"]["git"] = \pocketmine\GIT_COMMIT; + $this->data["general"]["git"] = VersionInfo::getGitHash(); $this->data["general"]["uname"] = php_uname("a"); $this->data["general"]["php"] = phpversion(); $this->data["general"]["zend"] = zend_version(); @@ -323,7 +322,7 @@ class CrashDump{ $this->data["general"]["os"] = Utils::getOS(); $this->data["general"]["composer_libraries"] = Versions::VERSIONS; $this->addLine($this->server->getName() . " version: " . $version->getFullVersion(true) . " [Protocol " . ProtocolInfo::CURRENT_PROTOCOL . "]"); - $this->addLine("Git commit: " . \pocketmine\GIT_COMMIT); + $this->addLine("Git commit: " . VersionInfo::getGitHash()); $this->addLine("uname -a: " . php_uname("a")); $this->addLine("PHP Version: " . phpversion()); $this->addLine("Zend version: " . zend_version()); diff --git a/src/PocketMine.php b/src/PocketMine.php index 78c3f6cda..d5b842d30 100644 --- a/src/PocketMine.php +++ b/src/PocketMine.php @@ -197,23 +197,6 @@ namespace pocketmine { ErrorToExceptionHandler::set(); - $version = new VersionString(VersionInfo::BASE_VERSION, VersionInfo::IS_DEVELOPMENT_BUILD, VersionInfo::BUILD_NUMBER); - define('pocketmine\VERSION', $version->getFullVersion(true)); - - $gitHash = str_repeat("00", 20); - - if(\Phar::running(true) === ""){ - $gitHash = Git::getRepositoryStatePretty(\pocketmine\PATH); - }else{ - $phar = new \Phar(\Phar::running(false)); - $meta = $phar->getMetadata(); - if(isset($meta["git"])){ - $gitHash = $meta["git"]; - } - } - - define('pocketmine\GIT_COMMIT', $gitHash); - $opts = getopt("", ["data:", "plugins:", "no-wizard", "enable-ansi", "disable-ansi"]); $dataPath = isset($opts["data"]) ? $opts["data"] . DIRECTORY_SEPARATOR : realpath(getcwd()) . DIRECTORY_SEPARATOR; diff --git a/src/Server.php b/src/Server.php index e4e025b9e..1a9dd31e9 100644 --- a/src/Server.php +++ b/src/Server.php @@ -288,7 +288,7 @@ class Server{ } public function getPocketMineVersion() : string{ - return \pocketmine\VERSION; + return VersionInfo::getVersionObj()->getFullVersion(true); } public function getVersion() : string{ @@ -1455,7 +1455,7 @@ class Server{ $report = false; } - if(strrpos(\pocketmine\GIT_COMMIT, "-dirty") !== false or \pocketmine\GIT_COMMIT === str_repeat("00", 20)){ + if(strrpos(VersionInfo::getGitHash(), "-dirty") !== false or VersionInfo::getGitHash() === str_repeat("00", 20)){ $this->logger->debug("Not sending crashdump due to locally modified"); $report = false; //Don't send crashdumps for locally modified builds } diff --git a/src/VersionInfo.php b/src/VersionInfo.php index 67ac227ac..e4daaf30f 100644 --- a/src/VersionInfo.php +++ b/src/VersionInfo.php @@ -23,6 +23,10 @@ declare(strict_types=1); namespace pocketmine; +use pocketmine\utils\Git; +use pocketmine\utils\VersionString; +use function str_repeat; + final class VersionInfo{ public const NAME = "PocketMine-MP"; public const BASE_VERSION = "4.0.0"; @@ -32,4 +36,37 @@ final class VersionInfo{ private function __construct(){ //NOOP } + + /** @var string|null */ + private static $gitHash = null; + + public static function getGitHash() : string{ + if(self::$gitHash === null){ + $gitHash = str_repeat("00", 20); + + if(\Phar::running(true) === ""){ + $gitHash = Git::getRepositoryStatePretty(\pocketmine\PATH); + }else{ + $phar = new \Phar(\Phar::running(false)); + $meta = $phar->getMetadata(); + if(isset($meta["git"])){ + $gitHash = $meta["git"]; + } + } + + self::$gitHash = $gitHash; + } + + return self::$gitHash; + } + + /** @var VersionString|null */ + private static $fullVersion = null; + + public static function getVersionObj() : VersionString{ + if(self::$fullVersion === null){ + self::$fullVersion = new VersionString(self::BASE_VERSION, self::IS_DEVELOPMENT_BUILD, self::BUILD_NUMBER); + } + return self::$fullVersion; + } } diff --git a/src/stats/SendUsageTask.php b/src/stats/SendUsageTask.php index 15753ff52..4fe3394df 100644 --- a/src/stats/SendUsageTask.php +++ b/src/stats/SendUsageTask.php @@ -31,7 +31,6 @@ use pocketmine\utils\AssumptionFailedError; use pocketmine\utils\Internet; use pocketmine\utils\Process; use pocketmine\utils\Utils; -use pocketmine\utils\VersionString; use pocketmine\uuid\UUID; use pocketmine\VersionInfo; use function array_map; @@ -72,7 +71,7 @@ class SendUsageTask extends AsyncTask{ case self::TYPE_OPEN: $data["event"] = "open"; - $version = new VersionString(VersionInfo::BASE_VERSION, VersionInfo::IS_DEVELOPMENT_BUILD, VersionInfo::BUILD_NUMBER); + $version = VersionInfo::getVersionObj(); $data["server"] = [ "port" => $server->getPort(), diff --git a/src/updater/AutoUpdater.php b/src/updater/AutoUpdater.php index 51497ee33..9ca12b85e 100644 --- a/src/updater/AutoUpdater.php +++ b/src/updater/AutoUpdater.php @@ -160,7 +160,7 @@ class AutoUpdater{ if($this->updateInfo === null){ return; } - $currentVersion = new VersionString(VersionInfo::BASE_VERSION, VersionInfo::IS_DEVELOPMENT_BUILD, VersionInfo::BUILD_NUMBER); + $currentVersion = VersionInfo::getVersionObj(); try{ $newVersion = new VersionString($this->updateInfo->base_version, $this->updateInfo->is_dev, $this->updateInfo->build); }catch(\InvalidArgumentException $e){ diff --git a/src/utils/Internet.php b/src/utils/Internet.php index de66a8a31..4b7ad926f 100644 --- a/src/utils/Internet.php +++ b/src/utils/Internet.php @@ -224,7 +224,7 @@ class Internet{ CURLOPT_RETURNTRANSFER => true, CURLOPT_CONNECTTIMEOUT_MS => (int) ($timeout * 1000), CURLOPT_TIMEOUT_MS => (int) ($timeout * 1000), - CURLOPT_HTTPHEADER => array_merge(["User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0 " . VersionInfo::NAME . "/" . \pocketmine\VERSION], $extraHeaders), + CURLOPT_HTTPHEADER => array_merge(["User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0 " . VersionInfo::NAME . "/" . VersionInfo::getVersionObj()->getFullVersion(true)], $extraHeaders), CURLOPT_HEADER => true ]); try{ diff --git a/tests/phpstan/bootstrap.php b/tests/phpstan/bootstrap.php index 9850a602f..21459e92c 100644 --- a/tests/phpstan/bootstrap.php +++ b/tests/phpstan/bootstrap.php @@ -25,6 +25,4 @@ define('pocketmine\_PHPSTAN_ANALYSIS', true); //TODO: these need to be defined properly or removed define('pocketmine\COMPOSER_AUTOLOADER_PATH', dirname(__DIR__, 2) . '/vendor/autoload.php'); -define('pocketmine\GIT_COMMIT', str_repeat('00', 20)); define('pocketmine\PLUGIN_PATH', ''); -define('pocketmine\VERSION', '9.9.9'); diff --git a/tests/phpstan/configs/check-explicit-mixed-baseline.neon b/tests/phpstan/configs/check-explicit-mixed-baseline.neon index 2d1b55268..856de86a7 100644 --- a/tests/phpstan/configs/check-explicit-mixed-baseline.neon +++ b/tests/phpstan/configs/check-explicit-mixed-baseline.neon @@ -30,11 +30,6 @@ parameters: count: 7 path: ../../../src/MemoryManager.php - - - message: "#^Cannot access offset 'git' on mixed\\.$#" - count: 2 - path: ../../../src/PocketMine.php - - message: "#^Cannot cast mixed to int\\.$#" count: 6 @@ -90,6 +85,11 @@ parameters: count: 1 path: ../../../src/ServerConfigGroup.php + - + message: "#^Cannot access offset 'git' on mixed\\.$#" + count: 2 + path: ../../../src/VersionInfo.php + - message: "#^Cannot cast mixed to string\\.$#" count: 1