1515
1616use Composer \Autoload \ClassLoader ;
1717use Ixnode \PhpContainer \File ;
18+ use Ixnode \PhpContainer \Json ;
19+ use Ixnode \PhpException \ArrayType \ArrayKeyNotFoundException ;
1820use Ixnode \PhpException \File \FileNotFoundException ;
21+ use Ixnode \PhpException \Function \FunctionJsonEncodeException ;
22+ use Ixnode \PhpException \Type \TypeInvalidException ;
23+ use JsonException ;
1924use 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}
0 commit comments