Skip to content

Commit 4df8a74

Browse files
committed
Add name and description to version information.
1 parent f83aebe commit 4df8a74

File tree

4 files changed

+84
-24
lines changed

4 files changed

+84
-24
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"require": {
2525
"php": "^8.0",
2626
"ixnode/php-exception": "^0.1.7",
27-
"ixnode/php-container": "^0.1.0"
27+
"ixnode/php-container": "^0.1.2"
2828
},
2929
"bin": [
3030
"bin/version-manager"

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/Version.php

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515

1616
use Composer\Autoload\ClassLoader;
1717
use Ixnode\PhpContainer\File;
18+
use Ixnode\PhpContainer\Json;
19+
use Ixnode\PhpException\ArrayType\ArrayKeyNotFoundException;
1820
use Ixnode\PhpException\File\FileNotFoundException;
21+
use Ixnode\PhpException\Function\FunctionJsonEncodeException;
22+
use Ixnode\PhpException\Type\TypeInvalidException;
23+
use JsonException;
1924
use ReflectionClass;
2025

2126
/**
@@ -35,8 +40,14 @@ class Version
3540

3641
public const PATH_VERSION = 'VERSION';
3742

43+
public const PATH_COMPOSER_JSON = 'composer.json';
44+
3845
public const INDEX_VERSION = 'version';
3946

47+
public const INDEX_NAME = 'name';
48+
49+
public const INDEX_DESCRIPTION = 'description';
50+
4051
public const INDEX_DATE = 'date';
4152

4253
public const INDEX_LICENSE = 'license';
@@ -67,9 +78,35 @@ public function __construct(?string $rootDir = null)
6778
*/
6879
public function getVersion(): string
6980
{
70-
$versionFile = $this->getVersionFile();
81+
return $this->getVersionFile()->getContentAsTextTrim();
82+
}
7183

72-
return (new File($versionFile))->getContentAsTextTrim();
84+
/**
85+
* Returns the name of this application.
86+
*
87+
* @return string
88+
* @throws ArrayKeyNotFoundException
89+
* @throws FunctionJsonEncodeException
90+
* @throws JsonException
91+
* @throws TypeInvalidException
92+
*/
93+
public function getName(): string
94+
{
95+
return $this->getComposerKey(self::INDEX_NAME);
96+
}
97+
98+
/**
99+
* Returns the description of this application.
100+
*
101+
* @return string
102+
* @throws ArrayKeyNotFoundException
103+
* @throws FunctionJsonEncodeException
104+
* @throws JsonException
105+
* @throws TypeInvalidException
106+
*/
107+
public function getDescription(): string
108+
{
109+
return $this->getComposerKey(self::INDEX_DESCRIPTION);
73110
}
74111

75112
/**
@@ -80,15 +117,7 @@ public function getVersion(): string
80117
*/
81118
public function getDate(): string
82119
{
83-
$versionFile = $this->getVersionFile();
84-
85-
$mtime = filemtime($versionFile);
86-
87-
if ($mtime === false) {
88-
throw new FileNotFoundException($versionFile);
89-
}
90-
91-
return date ('l, F d, Y - H:i:s', $mtime);
120+
return $this->getVersionFile()->getDate();
92121
}
93122

94123
/**
@@ -120,6 +149,8 @@ public function getAuthors(): array
120149
public function getAll(): array
121150
{
122151
return [
152+
self::INDEX_NAME => $this->getName(),
153+
self::INDEX_DESCRIPTION => $this->getDescription(),
123154
self::INDEX_VERSION => $this->getVersion(),
124155
self::INDEX_DATE => $this->getDate(),
125156
self::INDEX_LICENSE => $this->getLicense(),
@@ -130,10 +161,37 @@ public function getAll(): array
130161
/**
131162
* Returns the version file.
132163
*
164+
* @return File
165+
*/
166+
public function getVersionFile(): File
167+
{
168+
return new File(sprintf('%s/%s', $this->rootDir, self::PATH_VERSION));
169+
}
170+
171+
/**
172+
* Returns the composer json file.
173+
*
174+
* @return File
175+
*/
176+
public function getComposerFile(): File
177+
{
178+
return new File(sprintf('%s/%s', $this->rootDir, self::PATH_COMPOSER_JSON));
179+
}
180+
181+
/**
182+
* Returns a value from composer.json given by key name.
183+
*
184+
* @param string|string[] $keys
133185
* @return string
186+
* @throws FunctionJsonEncodeException
187+
* @throws TypeInvalidException
188+
* @throws JsonException
189+
* @throws ArrayKeyNotFoundException
134190
*/
135-
public function getVersionFile(): string
191+
public function getComposerKey(string|array $keys): string
136192
{
137-
return sprintf('%s/%s', $this->rootDir, self::PATH_VERSION);
193+
$json = new Json($this->getComposerFile());
194+
195+
return $json->getKeyString($keys);
138196
}
139197
}

tests/Unit/VersionTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public function wrapper(): void
4141
$versionString = $this->getVersionString();
4242
$dateString = $this->getDateString();
4343
$versionArray = [
44+
Version::INDEX_NAME => 'ixnode/bash-version-manager',
45+
Version::INDEX_DESCRIPTION => 'Bash Version Manager',
4446
Version::INDEX_VERSION => $versionString,
4547
Version::INDEX_DATE => $dateString,
4648
Version::INDEX_LICENSE => Version::VALUE_LICENSE,
@@ -64,7 +66,7 @@ protected function getVersionString(): string
6466
{
6567
$versionFile = (new Version())->getVersionFile();
6668

67-
return (new File($versionFile))->getContentAsTextTrim();
69+
return $versionFile->getContentAsTextTrim();
6870
}
6971

7072
/**
@@ -77,10 +79,10 @@ protected function getDateString(): string
7779
{
7880
$versionFile = (new Version())->getVersionFile();
7981

80-
$mtime = filemtime($versionFile);
82+
$mtime = filemtime($versionFile->getPathReal());
8183

8284
if ($mtime === false) {
83-
throw new FileNotFoundException($versionFile);
85+
throw new FileNotFoundException($versionFile->getPath());
8486
}
8587

8688
return date ('l, F d, Y - H:i:s', $mtime);

0 commit comments

Comments
 (0)