-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #131 from godruoyi/godruoyi/move-getversion-method…
…-to-class chore: move the obtaining the version method to a separate class
- Loading branch information
Showing
2 changed files
with
72 additions
and
30 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Php\Pie\Util; | ||
|
||
use Composer\InstalledVersions; | ||
|
||
use function class_exists; | ||
|
||
/** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */ | ||
final class PieVersion | ||
{ | ||
/** | ||
* Note: magic constant that causes Symfony Console to not display a version | ||
* {@see Application::getLongVersion()} | ||
*/ | ||
private const SYMFONY_MAGIC_CONST_UNKNOWN = 'UNKNOWN'; | ||
|
||
/** | ||
* A static method to try to find the version of PIE you are currently | ||
* running. If running in the PHAR built with Box, this should return a | ||
* realistic-looking version; usually either a tag (e.g. `2.0.0`), or a tag | ||
* and following commit short hash (e.g. `2.0.0@e558e33`). If not this will | ||
* fall back to some other techniques to try to determine a version. | ||
*/ | ||
public static function get(): string | ||
{ | ||
/** | ||
* This value is replaced dynamically by Box with the real version when | ||
* we build the PHAR. It is based on the Git tag and/or version | ||
* | ||
* It will be replaced with `2.0.0` on an exact tag match, or something | ||
* like `2.0.0@e558e33` on a commit following a tag. | ||
* | ||
* When running not in a PHAR, this will not be replaced, so this | ||
* method needs additional logic to determine the version. | ||
* | ||
* @link https://box-project.github.io/box/configuration/#pretty-git-tag-placeholder-git | ||
*/ | ||
$pieVersion = '@pie_version@'; | ||
|
||
/** | ||
* @psalm-suppress RedundantCondition | ||
* @noinspection PhpConditionAlreadyCheckedInspection | ||
*/ | ||
// phpcs:ignore Generic.Strings.UnnecessaryStringConcat.Found | ||
if ($pieVersion === '@pie_version' . '@') { | ||
if (! class_exists(InstalledVersions::class)) { | ||
return self::SYMFONY_MAGIC_CONST_UNKNOWN; | ||
} | ||
|
||
/** | ||
* This tries to determine the version based on Composer; if we are | ||
* the root package (i.e. you're developing on it), this will most | ||
* likely be something like `dev-main` (branch name). | ||
*/ | ||
$installedVersion = InstalledVersions::getVersion(InstalledVersions::getRootPackage()['name']); | ||
if ($installedVersion === null) { | ||
return self::SYMFONY_MAGIC_CONST_UNKNOWN; | ||
} | ||
|
||
return $installedVersion; | ||
} | ||
|
||
/** @psalm-suppress NoValue */ | ||
return $pieVersion; | ||
} | ||
} |