Skip to content

Commit 9318e05

Browse files
committed
Add composer package version parser
1 parent cd2e4fb commit 9318e05

File tree

5 files changed

+142
-45
lines changed

5 files changed

+142
-45
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"require": {
2525
"php": "^8.0",
2626
"ixnode/php-exception": "^0.1.19",
27-
"ixnode/php-container": "^0.1.2"
27+
"ixnode/php-container": "^0.1.8",
28+
"ixnode/php-checker": "^0.1.9"
2829
},
2930
"bin": [
3031
"bin/version-manager"

composer.lock

Lines changed: 77 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/BashVersionManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/*
4-
* This file is part of the ixno/bash-version-manager project.
4+
* This file is part of the ixnode/bash-version-manager project.
55
*
66
* (c) Björn Hempel <https://www.hempel.li/>
77
*

src/Version.php

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/*
4-
* This file is part of the ixno/bash-version-manager project.
4+
* This file is part of the ixnode/bash-version-manager project.
55
*
66
* (c) Björn Hempel <https://www.hempel.li/>
77
*
@@ -17,7 +17,9 @@
1717
use Ixnode\PhpContainer\File;
1818
use Ixnode\PhpContainer\Json;
1919
use Ixnode\PhpException\ArrayType\ArrayKeyNotFoundException;
20+
use Ixnode\PhpException\Case\CaseInvalidException;
2021
use Ixnode\PhpException\File\FileNotFoundException;
22+
use Ixnode\PhpException\File\FileNotReadableException;
2123
use Ixnode\PhpException\Function\FunctionJsonEncodeException;
2224
use Ixnode\PhpException\Type\TypeInvalidException;
2325
use JsonException;
@@ -59,6 +61,8 @@ class Version
5961

6062
public const INDEX_COMPOSER = 'composer-version';
6163

64+
public const INDEX_PHP_EXCEPTION = 'php-exception';
65+
6266
protected const APP_COMPOSER = 'composer';
6367

6468
protected ?string $rootDir = null;
@@ -93,6 +97,9 @@ public function getVersion(): string
9397
*
9498
* @return string
9599
* @throws ArrayKeyNotFoundException
100+
* @throws CaseInvalidException
101+
* @throws FileNotFoundException
102+
* @throws FileNotReadableException
96103
* @throws FunctionJsonEncodeException
97104
* @throws JsonException
98105
* @throws TypeInvalidException
@@ -107,6 +114,9 @@ public function getName(): string
107114
*
108115
* @return string
109116
* @throws ArrayKeyNotFoundException
117+
* @throws CaseInvalidException
118+
* @throws FileNotFoundException
119+
* @throws FileNotReadableException
110120
* @throws FunctionJsonEncodeException
111121
* @throws JsonException
112122
* @throws TypeInvalidException
@@ -154,27 +164,7 @@ public function getAuthors(): array
154164
*/
155165
public function getVersionComposer(): string
156166
{
157-
$output = [];
158-
159-
$returnValue = null;
160-
161-
exec(sprintf('%s -V', self::APP_COMPOSER), $output, $returnValue);
162-
163-
if ($returnValue !== 0) {
164-
return sprintf('%s is not available', self::APP_COMPOSER);
165-
}
166-
167-
$string = implode("\n", $output);
168-
169-
$matches = [];
170-
171-
$result = preg_match('~[0-9]+\.[0-9]+\.[0-9]~', $string, $matches);
172-
173-
if ($result !== 1) {
174-
return sprintf('Unable to get %s version.', self::APP_COMPOSER);
175-
}
176-
177-
return strval(current($matches));
167+
return $this->executeComposerCommand(sprintf('%s -V', self::APP_COMPOSER));
178168
}
179169

180170
/**
@@ -187,6 +177,17 @@ public function getVersionPhp(): string
187177
return phpversion();
188178
}
189179

180+
/**
181+
* Shows the composer package version.
182+
*
183+
* @param string $package
184+
* @return string
185+
*/
186+
public function getVersionComposerPackage(string $package): string
187+
{
188+
return $this->executeComposerCommand(sprintf('%s show | grep "%s"', self::APP_COMPOSER, $package));
189+
}
190+
190191
/**
191192
* Returns all information.
192193
*
@@ -208,6 +209,7 @@ public function getAll(): array
208209
self::INDEX_AUTHORS => $this->getAuthors(),
209210
self::INDEX_PHP => $this->getVersionPhp(),
210211
self::INDEX_COMPOSER => $this->getVersionComposer(),
212+
self::INDEX_PHP_EXCEPTION => $this->getVersionComposerPackage('ixnode/php-exception'),
211213
];
212214
}
213215

@@ -236,15 +238,49 @@ public function getComposerFile(): File
236238
*
237239
* @param string|string[] $keys
238240
* @return string
241+
* @throws ArrayKeyNotFoundException
242+
* @throws FileNotFoundException
239243
* @throws FunctionJsonEncodeException
240-
* @throws TypeInvalidException
241244
* @throws JsonException
242-
* @throws ArrayKeyNotFoundException
245+
* @throws TypeInvalidException
246+
* @throws CaseInvalidException
247+
* @throws FileNotReadableException
243248
*/
244249
public function getComposerKey(string|array $keys): string
245250
{
246251
$json = new Json($this->getComposerFile());
247252

248253
return $json->getKeyString($keys);
249254
}
255+
256+
/**
257+
* Executes the composer command and returns the version.
258+
*
259+
* @param string $command
260+
* @return string
261+
*/
262+
private function executeComposerCommand(string $command): string
263+
{
264+
$output = [];
265+
266+
$returnValue = null;
267+
268+
exec($command, $output, $returnValue);
269+
270+
if ($returnValue !== 0) {
271+
return sprintf('%s is not available', self::APP_COMPOSER);
272+
}
273+
274+
$string = implode("\n", $output);
275+
276+
$matches = [];
277+
278+
$result = preg_match('~[0-9]+\.[0-9]+\.[0-9]+~', $string, $matches);
279+
280+
if ($result !== 1) {
281+
return sprintf('Unable to get %s version.', self::APP_COMPOSER);
282+
}
283+
284+
return strval(current($matches));
285+
}
250286
}

tests/Unit/VersionTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/*
4-
* This file is part of the ixno/bash-version-manager project.
4+
* This file is part of the ixnode/bash-version-manager project.
55
*
66
* (c) Björn Hempel <https://www.hempel.li/>
77
*
@@ -56,6 +56,7 @@ public function wrapper(): void
5656
Version::INDEX_AUTHORS => Version::VALUE_AUTHORS,
5757
Version::INDEX_PHP => '8.2.7',
5858
Version::INDEX_COMPOSER => '2.5.1',
59+
Version::INDEX_PHP_EXCEPTION => '0.1.19',
5960
];
6061

6162
/* Act */

0 commit comments

Comments
 (0)