1919 */
2020class ExecutableFinder
2121{
22- private array $ 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 array $ suffixes = [];
31+
3132 /**
3233 * Replaces default suffixes of executable.
3334 *
@@ -67,11 +68,13 @@ public function find(string $name, ?string $default = null, array $extraDirs = [
6768 $ extraDirs
6869 );
6970
70- $ suffixes = ['' ];
71+ $ suffixes = [];
7172 if ('\\' === \DIRECTORY_SEPARATOR ) {
7273 $ pathExt = getenv ('PATHEXT ' );
73- $ suffixes = array_merge ($ pathExt ? explode (\PATH_SEPARATOR , $ pathExt ) : $ this ->suffixes , $ suffixes );
74+ $ suffixes = $ this ->suffixes ;
75+ $ suffixes = array_merge ($ suffixes , $ pathExt ? explode (\PATH_SEPARATOR , $ pathExt ) : ['.exe ' , '.bat ' , '.cmd ' , '.com ' ]);
7476 }
77+ $ suffixes = '' !== pathinfo ($ name , PATHINFO_EXTENSION ) ? array_merge (['' ], $ suffixes ) : array_merge ($ suffixes , ['' ]);
7578 foreach ($ suffixes as $ suffix ) {
7679 foreach ($ dirs as $ dir ) {
7780 if ('' === $ dir ) {
@@ -87,12 +90,11 @@ public function find(string $name, ?string $default = null, array $extraDirs = [
8790 }
8891 }
8992
90- if (!\function_exists ('exec ' ) || \strlen ($ name ) !== strcspn ($ name , '/ ' .\DIRECTORY_SEPARATOR )) {
93+ if ('\\' === \ DIRECTORY_SEPARATOR || !\function_exists ('exec ' ) || \strlen ($ name ) !== strcspn ($ name , '/ ' .\DIRECTORY_SEPARATOR )) {
9194 return $ default ;
9295 }
9396
94- $ command = '\\' === \DIRECTORY_SEPARATOR ? 'where %s 2> NUL ' : 'command -v -- %s ' ;
95- $ execResult = exec (\sprintf ($ command , escapeshellarg ($ name )));
97+ $ execResult = exec ('command -v -- ' .escapeshellarg ($ name ));
9698
9799 if (($ executablePath = substr ($ execResult , 0 , strpos ($ execResult , \PHP_EOL ) ?: null )) && @is_executable ($ executablePath )) {
98100 return $ executablePath ;
0 commit comments