|
19 | 19 | */ |
20 | 20 | class ExecutableFinder |
21 | 21 | { |
22 | | - private $suffixes = ['.exe', '.bat', '.cmd', '.com']; |
| 22 | + private $suffixes = []; |
23 | 23 |
|
24 | 24 | /** |
25 | 25 | * Replaces default suffixes of executable. |
@@ -70,19 +70,38 @@ public function find($name, $default = null, array $extraDirs = []) |
70 | 70 | ); |
71 | 71 | } |
72 | 72 |
|
73 | | - $suffixes = ['']; |
| 73 | + $suffixes = []; |
74 | 74 | if ('\\' === \DIRECTORY_SEPARATOR) { |
75 | 75 | $pathExt = getenv('PATHEXT'); |
76 | | - $suffixes = array_merge($pathExt ? explode(\PATH_SEPARATOR, $pathExt) : $this->suffixes, $suffixes); |
| 76 | + $suffixes = $this->suffixes; |
| 77 | + $suffixes = array_merge($suffixes, $pathExt ? explode(\PATH_SEPARATOR, $pathExt) : ['.exe', '.bat', '.cmd', '.com']); |
77 | 78 | } |
| 79 | + $suffixes = '' !== pathinfo($name, PATHINFO_EXTENSION) ? array_merge([''], $suffixes) : array_merge($suffixes, ['']); |
78 | 80 | foreach ($suffixes as $suffix) { |
79 | 81 | foreach ($dirs as $dir) { |
| 82 | + if ('' === $dir) { |
| 83 | + $dir = '.'; |
| 84 | + } |
80 | 85 | if (@is_file($file = $dir.\DIRECTORY_SEPARATOR.$name.$suffix) && ('\\' === \DIRECTORY_SEPARATOR || @is_executable($file))) { |
81 | 86 | return $file; |
82 | 87 | } |
| 88 | + |
| 89 | + if (!@is_dir($dir) && basename($dir) === $name.$suffix && @is_executable($dir)) { |
| 90 | + return $dir; |
| 91 | + } |
83 | 92 | } |
84 | 93 | } |
85 | 94 |
|
| 95 | + if ('\\' === \DIRECTORY_SEPARATOR || !\function_exists('exec') || \strlen($name) !== strcspn($name, '/'.\DIRECTORY_SEPARATOR)) { |
| 96 | + return $default; |
| 97 | + } |
| 98 | + |
| 99 | + $execResult = exec('command -v -- '.escapeshellarg($name)); |
| 100 | + |
| 101 | + if (($executablePath = substr($execResult, 0, strpos($execResult, \PHP_EOL) ?: null)) && @is_executable($executablePath)) { |
| 102 | + return $executablePath; |
| 103 | + } |
| 104 | + |
86 | 105 | return $default; |
87 | 106 | } |
88 | 107 | } |
0 commit comments