19
19
*/
20
20
class ExecutableFinder
21
21
{
22
- private $ 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 $ suffixes = [];
31
+
31
32
/**
32
33
* Replaces default suffixes of executable.
33
34
*/
@@ -65,11 +66,13 @@ public function find(string $name, ?string $default = null, array $extraDirs = [
65
66
$ extraDirs
66
67
);
67
68
68
- $ suffixes = ['' ];
69
+ $ suffixes = [];
69
70
if ('\\' === \DIRECTORY_SEPARATOR ) {
70
71
$ 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 ' ]);
72
74
}
75
+ $ suffixes = '' !== pathinfo ($ name , PATHINFO_EXTENSION ) ? array_merge (['' ], $ suffixes ) : array_merge ($ suffixes , ['' ]);
73
76
foreach ($ suffixes as $ suffix ) {
74
77
foreach ($ dirs as $ dir ) {
75
78
if ('' === $ dir ) {
@@ -85,12 +88,11 @@ public function find(string $name, ?string $default = null, array $extraDirs = [
85
88
}
86
89
}
87
90
88
- if (!\function_exists ('exec ' ) || \strlen ($ name ) !== strcspn ($ name , '/ ' .\DIRECTORY_SEPARATOR )) {
91
+ if ('\\' === \ DIRECTORY_SEPARATOR || !\function_exists ('exec ' ) || \strlen ($ name ) !== strcspn ($ name , '/ ' .\DIRECTORY_SEPARATOR )) {
89
92
return $ default ;
90
93
}
91
94
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 ));
94
96
95
97
if (($ executablePath = substr ($ execResult , 0 , strpos ($ execResult , \PHP_EOL ) ?: null )) && @is_executable ($ executablePath )) {
96
98
return $ executablePath ;
0 commit comments