Skip to content

Commit dce72f7

Browse files
sidzexussum12
andauthored
Get application version from composer.lock (povils#176)
Co-authored-by: Scott Dutton <[email protected]>
1 parent 17d4327 commit dce72f7

File tree

6 files changed

+37
-17
lines changed

6 files changed

+37
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
55

66
## [v3.5.0]
77
- Support `phpunit/php-timer` 7.0
8+
- Add `composer-runtime-api:"^2.0"` dependency to be able to get application version automatically
89

910
## [v3.4.1]
1011
- Add test to verify that `Application::VERSION` match the latest git tag

composer.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
},
1515
"require": {
1616
"php": "^7.4 || ^8.0",
17-
"symfony/console": "^4.4 || ^5.0 || ^6.0 || ^7.0",
18-
"symfony/finder": "^4.4 || ^5.0 || ^6.0 || ^7.0",
17+
"composer-runtime-api": "^2.0",
1918
"nikic/php-parser": "^4.18 || ^5.0",
2019
"php-parallel-lint/php-console-highlighter": "^1.0",
21-
"phpunit/php-timer": "^2.0 || ^3.0 || ^4.0 || ^5.0 || ^6.0 || ^7.0"
20+
"phpunit/php-timer": "^2.0 || ^3.0 || ^4.0 || ^5.0 || ^6.0 || ^7.0",
21+
"symfony/console": "^4.4 || ^5.0 || ^6.0 || ^7.0",
22+
"symfony/finder": "^4.4 || ^5.0 || ^6.0 || ^7.0"
2223
},
2324
"require-dev": {
2425
"phpunit/phpunit": "^9.6",
@@ -34,6 +35,9 @@
3435
"Povils\\PHPMND\\Tests\\": "tests/"
3536
}
3637
},
38+
"config": {
39+
"sort-packages": true
40+
},
3741
"bin": [
3842
"bin/phpmnd"
3943
],

src/Console/Application.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Povils\PHPMND\Console;
66

7+
use Composer\InstalledVersions;
8+
use OutOfBoundsException;
79
use Povils\PHPMND\Command\RunCommand;
810
use Povils\PHPMND\Container;
911
use Symfony\Component\Console\Application as BaseApplication;
@@ -14,14 +16,15 @@
1416

1517
class Application extends BaseApplication
1618
{
17-
public const VERSION = '3.5.0';
19+
public const PACKAGE_NAME = 'povils/phpmnd';
20+
1821
private const NAME = 'phpmnd';
1922

2023
private Container $container;
2124

2225
public function __construct(Container $container)
2326
{
24-
parent::__construct(self::NAME, self::VERSION);
27+
parent::__construct(self::NAME, self::getPrettyVersion());
2528

2629
$this->setDefaultCommand('run', true);
2730

@@ -62,4 +65,23 @@ protected function getDefaultCommands(): array
6265
{
6366
return [new HelpCommand(), new RunCommand()];
6467
}
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+
}
6587
}

src/Container.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,8 @@ private function __construct(array $values)
3131
public static function create(): self
3232
{
3333
return new self([
34-
Parser::class => static function (self $container): Parser {
35-
return (new ParserFactory())->createForHostVersion();
36-
},
37-
FileParser::class => static function (self $container): FileParser {
38-
return new FileParser($container->getParser());
39-
},
34+
Parser::class => static fn (): Parser => (new ParserFactory())->createForHostVersion(),
35+
FileParser::class => static fn (self $container): FileParser => new FileParser($container->getParser()),
4036
]);
4137
}
4238

@@ -57,10 +53,7 @@ private function offsetSet(string $id, Closure $value): void
5753
unset($this->values[$id]);
5854
}
5955

60-
/**
61-
* @return object
62-
*/
63-
private function get(string $id)
56+
private function get(string $id): object
6457
{
6558
if (!isset($this->keys[$id])) {
6659
throw new InvalidArgumentException(sprintf('Unknown service "%s"', $id));

src/Printer/Xml.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function printData(OutputInterface $output, HintList $hintList, array $de
2929
$output->writeln('Generate XML output...');
3030
$dom = new DOMDocument();
3131
$rootNode = $dom->createElement('phpmnd');
32-
$rootNode->setAttribute('version', Application::VERSION);
32+
$rootNode->setAttribute('version', Application::getPrettyVersion());
3333
$rootNode->setAttribute('fileCount', (string) count($groupedList));
3434

3535
$filesNode = $dom->createElement('files');

tests/Printer/XmlTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function testPrintData() : void
7979

8080
private function assertXml(string $expected, string $actualFile) : void
8181
{
82-
$expectedXml = str_replace('%%PHPMND_VERSION%%', Application::VERSION, $expected);
82+
$expectedXml = str_replace('%%PHPMND_VERSION%%', Application::getPrettyVersion(), $expected);
8383
$this->assertXmlStringEqualsXmlString($expectedXml, file_get_contents($actualFile));
8484
}
8585
}

0 commit comments

Comments
 (0)