From 16d20fe3e7a92c6dc734b98d0faadb8e09e84a5f Mon Sep 17 00:00:00 2001 From: Siad Ardroumli Date: Fri, 24 Jan 2025 21:18:11 +0100 Subject: [PATCH] Added getPhingVersion() cache and cached getPhingShortVersion() Get rid of additional name stripping to get the short version of Phing. --- src/Phing/Phing.php | 72 ++++++++++++++++++++++++++++++++------------- 1 file changed, 51 insertions(+), 21 deletions(-) diff --git a/src/Phing/Phing.php b/src/Phing/Phing.php index a54614920..304659cc3 100644 --- a/src/Phing/Phing.php +++ b/src/Phing/Phing.php @@ -112,6 +112,14 @@ class Phing * Used by utility function getResourcePath(). */ private static $importPaths; + /** + * Cache of the Phing version information when it has been loaded. + */ + private static $phingVersion; + /** + * Cache of the short Phing version information when it has been loaded. + */ + private static $phingShortVersion; /** * System-wide static properties (moved from System). */ @@ -590,33 +598,56 @@ public static function printVersion() } /** - * Gets the current Phing version based on VERSION.TXT file. + * Gets the current Phing version based on VERSION.TXT file. Once the information + * has been loaded once, it's cached and returned from the cache on future + * calls. * * @throws ConfigurationException */ public static function getPhingVersion(): string { - $versionPath = self::getResourcePath('phing/etc/VERSION.TXT'); - if (null === $versionPath) { - $versionPath = self::getResourcePath('etc/VERSION.TXT'); - } - if (null === $versionPath) { - throw new ConfigurationException('No VERSION.TXT file found; try setting phing.home environment variable.'); - } + if (self::$phingVersion === null) { + $versionPath = self::getResourcePath('phing/etc/VERSION.TXT'); + if (null === $versionPath) { + $versionPath = self::getResourcePath('etc/VERSION.TXT'); + } + if (null === $versionPath) { + throw new ConfigurationException('No VERSION.TXT file found; try setting phing.home environment variable.'); + } + + try { // try to read file + $file = new File($versionPath); + $reader = new FileReader($file); + $phingVersion = trim($reader->read()); + } catch (IOException $iox) { + throw new ConfigurationException("Can't read version information file"); + } + + $basePath = dirname(__DIR__, 2); + + $version = new Version($phingVersion, $basePath); + self::$phingShortVersion = (method_exists($version, 'asString') ? $version->asString() : $version->getVersion()); - try { // try to read file - $file = new File($versionPath); - $reader = new FileReader($file); - $phingVersion = trim($reader->read()); - } catch (IOException $iox) { - throw new ConfigurationException("Can't read version information file"); + self::$phingVersion = 'Phing ' . self::$phingShortVersion; } + return self::$phingVersion; + } - $basePath = dirname(__DIR__, 2); - - $version = new Version($phingVersion, $basePath); - - return 'Phing ' . (method_exists($version, 'asString') ? $version->asString() : $version->getVersion()); + /** + * Returns the short Phing version information, if available. Once the information + * has been loaded once, it's cached and returned from the cache on future + * calls. + * + * @return the short Phing version information as a string + * + * @throws ConfigurationException + */ + public static function getPhingShortVersion(): string + { + if (self::$phingShortVersion === null) { + self::getPhingVersion(); + } + return self::$phingShortVersion; } /** @@ -1530,8 +1561,7 @@ private function addInputHandler(Project $project): void */ private function comparePhingVersion($version): void { - $current = strtolower(self::getPhingVersion()); - $current = trim(str_replace('phing', '', $current)); + $current = self::getPhingShortVersion(); // make sure that version checks are not applied to trunk if ('dev' === $current) {