Skip to content

Commit 75dbc22

Browse files
authored
Merge pull request #131 from godruoyi/godruoyi/move-getversion-method-to-class
chore: move the obtaining the version method to a separate class
2 parents 33f911a + 9c69fab commit 75dbc22

File tree

2 files changed

+72
-30
lines changed

2 files changed

+72
-30
lines changed

bin/pie

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ declare(strict_types=1);
55

66
namespace Php\Pie;
77

8-
use Composer\InstalledVersions;
98
use Php\Pie\Command\BuildCommand;
109
use Php\Pie\Command\DownloadCommand;
1110
use Php\Pie\Command\InfoCommand;
1211
use Php\Pie\Command\InstallCommand;
1312
use Php\Pie\Command\ShowCommand;
13+
use Php\Pie\Util\PieVersion;
1414
use Symfony\Component\Console\Application;
1515
use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
1616
use Symfony\Component\Console\Input\InputInterface;
@@ -21,36 +21,8 @@ include $_composer_autoload_path ?? __DIR__ . '/../vendor/autoload.php';
2121

2222
$container = Container::factory();
2323

24-
$application = new Application(
25-
'🥧 PHP Installer for Extensions (PIE)',
26-
(static function (): string {
27-
$pieVersion = '@pie_version@';
24+
$application = new Application('🥧 PHP Installer for Extensions (PIE)', PieVersion::get());
2825

29-
/**
30-
* @psalm-suppress RedundantCondition
31-
* @noinspection PhpConditionAlreadyCheckedInspection
32-
*/
33-
if ($pieVersion === '@pie_version'.'@') {
34-
if (!class_exists(InstalledVersions::class)) {
35-
/**
36-
* Note: magic constant that causes Symfony Console to not display a version
37-
* {@see Application::getLongVersion()}
38-
*/
39-
return 'UNKNOWN';
40-
}
41-
42-
$installedVersion = InstalledVersions::getVersion(InstalledVersions::getRootPackage()['name']);
43-
if ($installedVersion === null) {
44-
return 'UNKNOWN';
45-
}
46-
47-
return $installedVersion;
48-
}
49-
50-
/** @psalm-suppress NoValue */
51-
return $pieVersion;
52-
})()
53-
);
5426
$application->setCommandLoader(new ContainerCommandLoader(
5527
$container,
5628
[
@@ -61,4 +33,5 @@ $application->setCommandLoader(new ContainerCommandLoader(
6133
'show' => ShowCommand::class,
6234
]
6335
));
36+
6437
$application->run($container->get(InputInterface::class), $container->get(OutputInterface::class));

src/Util/PieVersion.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Php\Pie\Util;
6+
7+
use Composer\InstalledVersions;
8+
9+
use function class_exists;
10+
11+
/** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */
12+
final class PieVersion
13+
{
14+
/**
15+
* Note: magic constant that causes Symfony Console to not display a version
16+
* {@see Application::getLongVersion()}
17+
*/
18+
private const SYMFONY_MAGIC_CONST_UNKNOWN = 'UNKNOWN';
19+
20+
/**
21+
* A static method to try to find the version of PIE you are currently
22+
* running. If running in the PHAR built with Box, this should return a
23+
* realistic-looking version; usually either a tag (e.g. `2.0.0`), or a tag
24+
* and following commit short hash (e.g. `2.0.0@e558e33`). If not this will
25+
* fall back to some other techniques to try to determine a version.
26+
*/
27+
public static function get(): string
28+
{
29+
/**
30+
* This value is replaced dynamically by Box with the real version when
31+
* we build the PHAR. It is based on the Git tag and/or version
32+
*
33+
* It will be replaced with `2.0.0` on an exact tag match, or something
34+
* like `2.0.0@e558e33` on a commit following a tag.
35+
*
36+
* When running not in a PHAR, this will not be replaced, so this
37+
* method needs additional logic to determine the version.
38+
*
39+
* @link https://box-project.github.io/box/configuration/#pretty-git-tag-placeholder-git
40+
*/
41+
$pieVersion = '@pie_version@';
42+
43+
/**
44+
* @psalm-suppress RedundantCondition
45+
* @noinspection PhpConditionAlreadyCheckedInspection
46+
*/
47+
// phpcs:ignore Generic.Strings.UnnecessaryStringConcat.Found
48+
if ($pieVersion === '@pie_version' . '@') {
49+
if (! class_exists(InstalledVersions::class)) {
50+
return self::SYMFONY_MAGIC_CONST_UNKNOWN;
51+
}
52+
53+
/**
54+
* This tries to determine the version based on Composer; if we are
55+
* the root package (i.e. you're developing on it), this will most
56+
* likely be something like `dev-main` (branch name).
57+
*/
58+
$installedVersion = InstalledVersions::getVersion(InstalledVersions::getRootPackage()['name']);
59+
if ($installedVersion === null) {
60+
return self::SYMFONY_MAGIC_CONST_UNKNOWN;
61+
}
62+
63+
return $installedVersion;
64+
}
65+
66+
/** @psalm-suppress NoValue */
67+
return $pieVersion;
68+
}
69+
}

0 commit comments

Comments
 (0)