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 *
1717use Ixnode \PhpContainer \File ;
1818use Ixnode \PhpContainer \Json ;
1919use Ixnode \PhpException \ArrayType \ArrayKeyNotFoundException ;
20+ use Ixnode \PhpException \Case \CaseInvalidException ;
2021use Ixnode \PhpException \File \FileNotFoundException ;
22+ use Ixnode \PhpException \File \FileNotReadableException ;
2123use Ixnode \PhpException \Function \FunctionJsonEncodeException ;
2224use Ixnode \PhpException \Type \TypeInvalidException ;
2325use 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}
0 commit comments