Skip to content

Commit 347139d

Browse files
committed
Add php and composer version
1 parent 98f0a77 commit 347139d

File tree

5 files changed

+78
-16
lines changed

5 files changed

+78
-16
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
},
2424
"require": {
2525
"php": "^8.0",
26-
"ixnode/php-exception": "^0.1.7",
26+
"ixnode/php-exception": "^0.1.19",
2727
"ixnode/php-container": "^0.1.2"
2828
},
2929
"bin": [

composer.lock

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

src/BashVersionManager.php

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

3-
declare(strict_types=1);
4-
53
/*
64
* This file is part of the ixno/bash-version-manager project.
75
*
@@ -11,6 +9,8 @@
119
* file that was distributed with this source code.
1210
*/
1311

12+
declare(strict_types=1);
13+
1414
namespace Ixnode\BashVersionManager;
1515

1616
/**

src/Version.php

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

3-
declare(strict_types=1);
4-
53
/*
64
* This file is part of the ixno/bash-version-manager project.
75
*
@@ -11,6 +9,8 @@
119
* file that was distributed with this source code.
1210
*/
1311

12+
declare(strict_types=1);
13+
1414
namespace Ixnode\BashVersionManager;
1515

1616
use Composer\Autoload\ClassLoader;
@@ -27,7 +27,8 @@
2727
* Class Version
2828
*
2929
* @author Björn Hempel <bjoern@hempel.li>
30-
* @version 0.1.0 (2022-12-30)
30+
* @version 0.1.1 (2023-06-24)
31+
* @since 0.1.1 (2023-06-24) Refactoring. Add more versions.
3132
* @since 0.1.0 (2022-12-30) First version.
3233
*/
3334
class Version
@@ -54,6 +55,12 @@ class Version
5455

5556
public const INDEX_AUTHORS = 'authors';
5657

58+
public const INDEX_PHP = 'php-version';
59+
60+
public const INDEX_COMPOSER = 'composer-version';
61+
62+
protected const APP_COMPOSER = 'composer';
63+
5764
protected ?string $rootDir = null;
5865

5966
/**
@@ -140,11 +147,55 @@ public function getAuthors(): array
140147
return self::VALUE_AUTHORS;
141148
}
142149

150+
/**
151+
* Returns the composer version.
152+
*
153+
* @return string
154+
*/
155+
public function getVersionComposer(): string
156+
{
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));
178+
}
179+
180+
/**
181+
* Returns the php version of this application.
182+
*
183+
* @return string
184+
*/
185+
public function getVersionPhp(): string
186+
{
187+
return phpversion();
188+
}
189+
143190
/**
144191
* Returns all information.
145192
*
146193
* @return array{version: string, license: string, authors: array<int, string>}
194+
* @throws ArrayKeyNotFoundException
147195
* @throws FileNotFoundException
196+
* @throws FunctionJsonEncodeException
197+
* @throws JsonException
198+
* @throws TypeInvalidException
148199
*/
149200
public function getAll(): array
150201
{
@@ -155,6 +206,8 @@ public function getAll(): array
155206
self::INDEX_DATE => $this->getDate(),
156207
self::INDEX_LICENSE => $this->getLicense(),
157208
self::INDEX_AUTHORS => $this->getAuthors(),
209+
self::INDEX_PHP => $this->getVersionPhp(),
210+
self::INDEX_COMPOSER => $this->getVersionComposer(),
158211
];
159212
}
160213

tests/Unit/VersionTest.php

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

3-
declare(strict_types=1);
4-
53
/*
64
* This file is part of the ixno/bash-version-manager project.
75
*
@@ -11,11 +9,16 @@
119
* file that was distributed with this source code.
1210
*/
1311

12+
declare(strict_types=1);
13+
1414
namespace Ixnode\BashVersionManager\Tests\Unit;
1515

1616
use Ixnode\BashVersionManager\Version;
17-
use Ixnode\PhpContainer\File;
17+
use Ixnode\PhpException\ArrayType\ArrayKeyNotFoundException;
1818
use Ixnode\PhpException\File\FileNotFoundException;
19+
use Ixnode\PhpException\Function\FunctionJsonEncodeException;
20+
use Ixnode\PhpException\Type\TypeInvalidException;
21+
use JsonException;
1922
use PHPUnit\Framework\TestCase;
2023

2124
/**
@@ -34,6 +37,10 @@ final class VersionTest extends TestCase
3437
* @test
3538
*
3639
* @throws FileNotFoundException
40+
* @throws ArrayKeyNotFoundException
41+
* @throws FunctionJsonEncodeException
42+
* @throws TypeInvalidException
43+
* @throws JsonException
3744
*/
3845
public function wrapper(): void
3946
{
@@ -47,6 +54,8 @@ public function wrapper(): void
4754
Version::INDEX_DATE => $dateString,
4855
Version::INDEX_LICENSE => Version::VALUE_LICENSE,
4956
Version::INDEX_AUTHORS => Version::VALUE_AUTHORS,
57+
Version::INDEX_PHP => '8.2.7',
58+
Version::INDEX_COMPOSER => '2.5.1',
5059
];
5160

5261
/* Act */

0 commit comments

Comments
 (0)