Created
April 16, 2021 19:05
-
-
Save simonrjones/2ea2709adb09721842d7305ff0c988e0 to your computer and use it in GitHub Desktop.
Return package version dynamically from Composer 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace MyNamespace; | |
use Composer\InstalledVersions; | |
/** | |
* Simple class to return package version from Composer 2 | |
* | |
* Replace PACKAGE constant with your Composer package name | |
*/ | |
final class Version | |
{ | |
const PACKAGE = 'your-organisation/package-name'; | |
/** | |
* Return current version of package | |
* | |
* Requires Composer 2, or returns null if not found | |
* | |
* @return string|null | |
*/ | |
public static function getVersion(): ?string | |
{ | |
if (class_exists('\Composer\InstalledVersions')) { | |
if (InstalledVersions::isInstalled(self::PACKAGE)) { | |
return InstalledVersions::getPrettyVersion(self::PACKAGE); | |
} | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment