19
19
*/
20
20
class ExecutableFinder
21
21
{
22
- private array $ suffixes = ['.exe ' , '.bat ' , '.cmd ' , '.com ' ];
23
22
private const CMD_BUILTINS = [
24
23
'assoc ' , 'break ' , 'call ' , 'cd ' , 'chdir ' , 'cls ' , 'color ' , 'copy ' , 'date ' ,
25
24
'del ' , 'dir ' , 'echo ' , 'endlocal ' , 'erase ' , 'exit ' , 'for ' , 'ftype ' , 'goto ' ,
@@ -28,6 +27,8 @@ class ExecutableFinder
28
27
'setlocal ' , 'shift ' , 'start ' , 'time ' , 'title ' , 'type ' , 'ver ' , 'vol ' ,
29
28
];
30
29
30
+ private array $ suffixes = [];
31
+
31
32
/**
32
33
* Replaces default suffixes of executable.
33
34
*
@@ -67,11 +68,13 @@ public function find(string $name, ?string $default = null, array $extraDirs = [
67
68
$ extraDirs
68
69
);
69
70
70
- $ suffixes = ['' ];
71
+ $ suffixes = [];
71
72
if ('\\' === \DIRECTORY_SEPARATOR ) {
72
73
$ 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 ' ]);
74
76
}
77
+ $ suffixes = '' !== pathinfo ($ name , PATHINFO_EXTENSION ) ? array_merge (['' ], $ suffixes ) : array_merge ($ suffixes , ['' ]);
75
78
foreach ($ suffixes as $ suffix ) {
76
79
foreach ($ dirs as $ dir ) {
77
80
if ('' === $ dir ) {
@@ -87,12 +90,11 @@ public function find(string $name, ?string $default = null, array $extraDirs = [
87
90
}
88
91
}
89
92
90
- if (!\function_exists ('exec ' ) || \strlen ($ name ) !== strcspn ($ name , '/ ' .\DIRECTORY_SEPARATOR )) {
93
+ if ('\\' === \ DIRECTORY_SEPARATOR || !\function_exists ('exec ' ) || \strlen ($ name ) !== strcspn ($ name , '/ ' .\DIRECTORY_SEPARATOR )) {
91
94
return $ default ;
92
95
}
93
96
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 ));
96
98
97
99
if (($ executablePath = substr ($ execResult , 0 , strpos ($ execResult , \PHP_EOL ) ?: null )) && @is_executable ($ executablePath )) {
98
100
return $ executablePath ;
0 commit comments