Skip to content

Commit

Permalink
[Phing] Cache Phing::phingVersion() and add cached Phing::phingShortV…
Browse files Browse the repository at this point in the history
…ersion() method (#1900)
  • Loading branch information
siad007 authored Feb 2, 2025
1 parent e731640 commit e7c1fab
Showing 1 changed file with 50 additions and 20 deletions.
70 changes: 50 additions & 20 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");
}
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);
$basePath = dirname(__DIR__, 2);

$version = new Version($phingVersion, $basePath);
$version = new Version($phingVersion, $basePath);
self::$phingShortVersion = (method_exists($version, 'asString') ? $version->asString() : $version->getVersion());

return 'Phing ' . (method_exists($version, 'asString') ? $version->asString() : $version->getVersion());
self::$phingVersion = 'Phing ' . self::$phingShortVersion;
}
return self::$phingVersion;
}

/**
* 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 e7c1fab

Please sign in to comment.