Skip to content

Commit

Permalink
Added getPhingVersion() cache and cached getPhingShortVersion()
Browse files Browse the repository at this point in the history
Get rid of additional name stripping to get the short version of Phing.
  • Loading branch information
siad007 authored Jan 24, 2025
1 parent e731640 commit 16d20fe
Showing 1 changed file with 51 additions and 21 deletions.
72 changes: 51 additions & 21 deletions src/Phing/Phing.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).
*/
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 16d20fe

Please sign in to comment.