1919 */
2020class ExecutableFinder
2121{
22- private $ suffixes = ['.exe ' , '.bat ' , '.cmd ' , '.com ' ];
2322 private const CMD_BUILTINS = [
2423 'assoc ' , 'break ' , 'call ' , 'cd ' , 'chdir ' , 'cls ' , 'color ' , 'copy ' , 'date ' ,
2524 'del ' , 'dir ' , 'echo ' , 'endlocal ' , 'erase ' , 'exit ' , 'for ' , 'ftype ' , 'goto ' ,
@@ -28,6 +27,8 @@ class ExecutableFinder
2827 'setlocal ' , 'shift ' , 'start ' , 'time ' , 'title ' , 'type ' , 'ver ' , 'vol ' ,
2928 ];
3029
30+ private $ suffixes = [];
31+
3132 /**
3233 * Replaces default suffixes of executable.
3334 */
@@ -65,11 +66,13 @@ public function find(string $name, ?string $default = null, array $extraDirs = [
6566 $ extraDirs
6667 );
6768
68- $ suffixes = ['' ];
69+ $ suffixes = [];
6970 if ('\\' === \DIRECTORY_SEPARATOR ) {
7071 $ pathExt = getenv ('PATHEXT ' );
71- $ suffixes = array_merge ($ pathExt ? explode (\PATH_SEPARATOR , $ pathExt ) : $ this ->suffixes , $ suffixes );
72+ $ suffixes = $ this ->suffixes ;
73+ $ suffixes = array_merge ($ suffixes , $ pathExt ? explode (\PATH_SEPARATOR , $ pathExt ) : ['.exe ' , '.bat ' , '.cmd ' , '.com ' ]);
7274 }
75+ $ suffixes = '' !== pathinfo ($ name , PATHINFO_EXTENSION ) ? array_merge (['' ], $ suffixes ) : array_merge ($ suffixes , ['' ]);
7376 foreach ($ suffixes as $ suffix ) {
7477 foreach ($ dirs as $ dir ) {
7578 if ('' === $ dir ) {
@@ -85,12 +88,11 @@ public function find(string $name, ?string $default = null, array $extraDirs = [
8588 }
8689 }
8790
88- if (!\function_exists ('exec ' ) || \strlen ($ name ) !== strcspn ($ name , '/ ' .\DIRECTORY_SEPARATOR )) {
91+ if ('\\' === \ DIRECTORY_SEPARATOR || !\function_exists ('exec ' ) || \strlen ($ name ) !== strcspn ($ name , '/ ' .\DIRECTORY_SEPARATOR )) {
8992 return $ default ;
9093 }
9194
92- $ command = '\\' === \DIRECTORY_SEPARATOR ? 'where %s 2> NUL ' : 'command -v -- %s ' ;
93- $ execResult = exec (\sprintf ($ command , escapeshellarg ($ name )));
95+ $ execResult = exec ('command -v -- ' .escapeshellarg ($ name ));
9496
9597 if (($ executablePath = substr ($ execResult , 0 , strpos ($ execResult , \PHP_EOL ) ?: null )) && @is_executable ($ executablePath )) {
9698 return $ executablePath ;
0 commit comments