-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 82e6234
Showing
8 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
vendor | ||
.phpunit.result.cache | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"name": "mazanax/composer-project-version", | ||
"description": "Small helper to get current composer project version", | ||
"type": "library", | ||
"version": "1.0.0", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Alexander Gorshkov", | ||
"email": "agorshkov@mznx.ru" | ||
} | ||
], | ||
"minimum-stability": "stable", | ||
"require": { | ||
"php": "^7.4", | ||
"ext-json": "*" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"MZNX\\ComposerPackageVersion\\": "src/" | ||
}, | ||
"files": ["src/helper.php"] | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"MZNX\\ComposerPackageVersion\\Tests\\": "tests/" | ||
} | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^9.5" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MZNX\ComposerPackageVersion; | ||
|
||
use InvalidArgumentException; | ||
use JsonException; | ||
|
||
class PackageVersion | ||
{ | ||
private string $pathToComposerJson; | ||
private ?string $version = null; | ||
|
||
private static ?PackageVersion $instance = null; | ||
|
||
private function __construct(string $pathToComposerJson) | ||
{ | ||
$this->pathToComposerJson = $pathToComposerJson; | ||
} | ||
|
||
private function __clone() | ||
{ | ||
} | ||
|
||
public static function init(string $pathToComposerJson): self | ||
{ | ||
if (self::$instance === null || self::$instance->pathToComposerJson !== $pathToComposerJson) { | ||
self::$instance = new self($pathToComposerJson); | ||
} | ||
|
||
return self::$instance; | ||
} | ||
|
||
public function getVersion(): string | ||
{ | ||
if (null === $this->version) { | ||
$this->version = $this->parseVersion(); | ||
} | ||
|
||
return $this->version; | ||
} | ||
|
||
private function parseVersion(): string | ||
{ | ||
if (!file_exists($this->pathToComposerJson)) { | ||
throw new InvalidArgumentException( | ||
sprintf('Cannot parse %s: file does not exist', $this->pathToComposerJson) | ||
); | ||
} | ||
|
||
try { | ||
$json = json_decode(file_get_contents($this->pathToComposerJson), true, 512, JSON_THROW_ON_ERROR); | ||
} catch (JsonException $e) { | ||
throw new InvalidArgumentException( | ||
sprintf('Cannot parse %s: %s', $this->pathToComposerJson, $e->getMessage()), 0, $e | ||
); | ||
} | ||
|
||
return trim($json['version'] ?? 'dev-master'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MZNX\ComposerPackageVersion; | ||
|
||
function Version(string $pathToComposerJson): string | ||
{ | ||
return PackageVersion::init($pathToComposerJson) | ||
->getVersion(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MZNX\ComposerPackageVersion\Tests; | ||
|
||
use InvalidArgumentException; | ||
use MZNX\ComposerPackageVersion\PackageVersion; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
use function MZNX\ComposerPackageVersion\Version; | ||
|
||
class PackageVersionTest extends TestCase | ||
{ | ||
public function test_get_version(): void | ||
{ | ||
$sut = PackageVersion::init(__DIR__ . '/fixtures/with_version.json'); | ||
|
||
self::assertEquals('1.2.3', $sut->getVersion()); | ||
} | ||
|
||
public function test_helper_function(): void | ||
{ | ||
static::assertEquals('1.2.3', Version(__DIR__ . '/fixtures/with_version.json')); | ||
} | ||
|
||
public function test_get_default_version(): void | ||
{ | ||
$sut = PackageVersion::init(__DIR__ . '/fixtures/without_version.json'); | ||
|
||
self::assertEquals('dev-master', $sut->getVersion()); | ||
} | ||
|
||
public function test_get_version_with_invalid_file_path(): void | ||
{ | ||
$sut = PackageVersion::init(__DIR__ . '/non-existing/composer/json/path'); | ||
|
||
$this->expectException(InvalidArgumentException::class); | ||
$sut->getVersion(); | ||
} | ||
|
||
public function test_get_version_with_broken_json_file(): void | ||
{ | ||
$sut = PackageVersion::init(__DIR__ . '/fixtures/broken_json_file.json'); | ||
|
||
$this->expectException(InvalidArgumentException::class); | ||
$sut->getVersion(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "test/package", | ||
"description": "Broken json file with specified version", | ||
"version": "1.2.3", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "test/package", | ||
"description": "Package with specified version", | ||
"version": "1.2.3" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "test/package", | ||
"description": "Package without specified version" | ||
} |