Skip to content

Instantly share code, notes, and snippets.

@simonrjones
Created April 16, 2021 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonrjones/2ea2709adb09721842d7305ff0c988e0 to your computer and use it in GitHub Desktop.
Save simonrjones/2ea2709adb09721842d7305ff0c988e0 to your computer and use it in GitHub Desktop.
Return package version dynamically from Composer 2
<?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