Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Phing] Cache Phing::phingVersion() and add cached Phing::phingShortVersion() method #1900

Merged
merged 2 commits into from
Feb 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading