1010use Magento \Framework \Console \Cli ;
1111use Magento \Framework \Escaper ;
1212use Magento \Framework \HTTP \ClientFactory ;
13+ use Magento \Framework \Shell ;
1314use OpenForgeProject \MageForge \Console \Command \AbstractCommand ;
1415use Symfony \Component \Console \Helper \TableSeparator ;
1516use Symfony \Component \Console \Input \InputInterface ;
@@ -25,12 +26,16 @@ class CheckCommand extends AbstractCommand
2526 /**
2627 * @param ProductMetadataInterface $productMetadata
2728 * @param Escaper $escaper
29+ * @param ResourceConnection $resourceConnection
30+ * @param ClientFactory $httpClientFactory
31+ * @param Shell $shell
2832 */
2933 public function __construct (
3034 private readonly ProductMetadataInterface $ productMetadata ,
3135 private readonly Escaper $ escaper ,
3236 private readonly ResourceConnection $ resourceConnection ,
33- private readonly ClientFactory $ httpClientFactory
37+ private readonly ClientFactory $ httpClientFactory ,
38+ private readonly Shell $ shell
3439 ) {
3540 parent ::__construct ();
3641 }
@@ -118,9 +123,13 @@ protected function executeCommand(InputInterface $input, OutputInterface $output
118123 */
119124 private function getNodeVersion (): string
120125 {
121- // phpcs:ignore Magento2.Security.InsecureFunction.Found -- exec required to check external tool version
122- exec ('node -v 2>/dev/null ' , $ output , $ returnCode );
123- return $ returnCode === 0 && !empty ($ output ) ? trim ($ output [0 ], 'v ' ) : 'Not installed ' ;
126+ try {
127+ $ output = trim ($ this ->shell ->execute ('node -v 2>/dev/null ' ));
128+ } catch (\Exception $ e ) {
129+ return 'Not installed ' ;
130+ }
131+
132+ return $ output !== '' ? ltrim ($ output , 'v ' ) : 'Not installed ' ;
124133 }
125134
126135 /**
@@ -202,12 +211,14 @@ private function getMysqlVersionViaMagento(): ?string
202211 */
203212 private function getMysqlVersionViaClient (): ?string
204213 {
205- // phpcs:ignore Magento2.Security.InsecureFunction.Found -- exec required to check external tool version
206- exec ( 'mysql --version 2>/dev/null ' , $ output , $ returnCode );
207- if ( $ returnCode === 0 && ! empty ( $ output ) ) {
208- $ versionString = $ output [ 0 ] ;
209- preg_match ( ' /Distrib ([0-9.]+)/ ' , $ versionString , $ matches );
214+ try {
215+ $ output = trim ( $ this -> shell -> execute ( 'mysql --version 2>/dev/null ' ) );
216+ } catch ( \ Exception $ e ) {
217+ return null ;
218+ }
210219
220+ if ($ output !== '' ) {
221+ preg_match ('/Distrib ([0-9.]+)/ ' , $ output , $ matches );
211222 return isset ($ matches [1 ]) ? $ matches [1 ] : null ;
212223 }
213224
@@ -301,13 +312,17 @@ private function getShortOsInfo(): string
301312 */
302313 private function getComposerVersion (): string
303314 {
304- // phpcs:ignore Magento2.Security.InsecureFunction.Found -- exec required to check external tool version
305- exec ('composer --version 2>/dev/null ' , $ output , $ returnCode );
306- if ($ returnCode !== 0 || empty ($ output )) {
315+ try {
316+ $ output = trim ($ this ->shell ->execute ('composer --version 2>/dev/null ' ));
317+ } catch (\Exception $ e ) {
318+ return 'Not installed ' ;
319+ }
320+
321+ if ($ output === '' ) {
307322 return 'Not installed ' ;
308323 }
309324
310- preg_match ('/Composer version ([^ ]+)/ ' , $ output[ 0 ] , $ matches );
325+ preg_match ('/Composer version ([^ ]+)/ ' , $ output , $ matches );
311326 return isset ($ matches [1 ]) ? $ matches [1 ] : 'Unknown ' ;
312327 }
313328
@@ -318,9 +333,13 @@ private function getComposerVersion(): string
318333 */
319334 private function getNpmVersion (): string
320335 {
321- // phpcs:ignore Magento2.Security.InsecureFunction.Found -- exec required to check external tool version
322- exec ('npm --version 2>/dev/null ' , $ output , $ returnCode );
323- return $ returnCode === 0 && !empty ($ output ) ? trim ($ output [0 ]) : 'Not installed ' ;
336+ try {
337+ $ output = trim ($ this ->shell ->execute ('npm --version 2>/dev/null ' ));
338+ } catch (\Exception $ e ) {
339+ return 'Not installed ' ;
340+ }
341+
342+ return $ output !== '' ? $ output : 'Not installed ' ;
324343 }
325344
326345 /**
@@ -330,13 +349,17 @@ private function getNpmVersion(): string
330349 */
331350 private function getGitVersion (): string
332351 {
333- // phpcs:ignore Magento2.Security.InsecureFunction.Found -- exec required to check external tool version
334- exec ('git --version 2>/dev/null ' , $ output , $ returnCode );
335- if ($ returnCode !== 0 || empty ($ output )) {
352+ try {
353+ $ output = trim ($ this ->shell ->execute ('git --version 2>/dev/null ' ));
354+ } catch (\Exception $ e ) {
355+ return 'Not installed ' ;
356+ }
357+
358+ if ($ output === '' ) {
336359 return 'Not installed ' ;
337360 }
338361
339- preg_match ('/git version (.+)/ ' , $ output[ 0 ] , $ matches );
362+ preg_match ('/git version (.+)/ ' , $ output , $ matches );
340363 return isset ($ matches [1 ]) ? $ matches [1 ] : 'Unknown ' ;
341364 }
342365
0 commit comments