|
4 | 4 |
|
5 | 5 | namespace Povils\PHPMND\Console;
|
6 | 6 |
|
| 7 | +use Composer\InstalledVersions; |
| 8 | +use OutOfBoundsException; |
7 | 9 | use Povils\PHPMND\Command\RunCommand;
|
8 | 10 | use Povils\PHPMND\Container;
|
9 | 11 | use Symfony\Component\Console\Application as BaseApplication;
|
|
14 | 16 |
|
15 | 17 | class Application extends BaseApplication
|
16 | 18 | {
|
17 |
| - public const VERSION = '3.5.0'; |
| 19 | + public const PACKAGE_NAME = 'povils/phpmnd'; |
| 20 | + |
18 | 21 | private const NAME = 'phpmnd';
|
19 | 22 |
|
20 | 23 | private Container $container;
|
21 | 24 |
|
22 | 25 | public function __construct(Container $container)
|
23 | 26 | {
|
24 |
| - parent::__construct(self::NAME, self::VERSION); |
| 27 | + parent::__construct(self::NAME, self::getPrettyVersion()); |
25 | 28 |
|
26 | 29 | $this->setDefaultCommand('run', true);
|
27 | 30 |
|
@@ -62,4 +65,23 @@ protected function getDefaultCommands(): array
|
62 | 65 | {
|
63 | 66 | return [new HelpCommand(), new RunCommand()];
|
64 | 67 | }
|
| 68 | + |
| 69 | + public static function getPrettyVersion(): string |
| 70 | + { |
| 71 | + // Pre 2.0 Composer runtime didn't have this class. |
| 72 | + if (!class_exists(InstalledVersions::class)) { |
| 73 | + return 'unknown'; |
| 74 | + } |
| 75 | + |
| 76 | + try { |
| 77 | + return (string) InstalledVersions::getPrettyVersion(self::PACKAGE_NAME); |
| 78 | + } catch (OutOfBoundsException $e) { |
| 79 | + if (preg_match('#package .*' . preg_quote(self::PACKAGE_NAME, '#') . '.* not installed#i', $e->getMessage()) === 0) { |
| 80 | + throw $e; |
| 81 | + } |
| 82 | + |
| 83 | + // We have a bogus exception: how can PHPMND be not installed if we're here? |
| 84 | + return 'not-installed'; |
| 85 | + } |
| 86 | + } |
65 | 87 | }
|
0 commit comments