Skip to content

Commit 651830b

Browse files
[Process] Properly deal with not-found executables on Windows
1 parent 95f3f19 commit 651830b

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

ExecutableFinder.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,14 @@ public function find(string $name, ?string $default = null, array $extraDirs = [
7070
}
7171
}
7272

73-
$command = '\\' === \DIRECTORY_SEPARATOR ? 'where' : 'command -v --';
74-
if (\function_exists('exec') && ($executablePath = strtok(@exec($command.' '.escapeshellarg($name)), \PHP_EOL)) && @is_executable($executablePath)) {
73+
if (!\function_exists('exec') || \strlen($name) !== strcspn($name, '/'.\DIRECTORY_SEPARATOR)) {
74+
return $default;
75+
}
76+
77+
$command = '\\' === \DIRECTORY_SEPARATOR ? 'where %s 2> NUL' : 'command -v -- %s';
78+
$execResult = exec(\sprintf($command, escapeshellarg($name)));
79+
80+
if (($executablePath = substr($execResult, 0, strpos($execResult, \PHP_EOL) ?: null)) && @is_executable($executablePath)) {
7581
return $executablePath;
7682
}
7783

PhpExecutableFinder.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,16 @@ public function find(bool $includeArgs = true)
3535
{
3636
if ($php = getenv('PHP_BINARY')) {
3737
if (!is_executable($php)) {
38-
$command = '\\' === \DIRECTORY_SEPARATOR ? 'where' : 'command -v --';
39-
if (\function_exists('exec') && $php = strtok(exec($command.' '.escapeshellarg($php)), \PHP_EOL)) {
40-
if (!is_executable($php)) {
41-
return false;
42-
}
43-
} else {
38+
if (!\function_exists('exec') || \strlen($php) !== strcspn($php, '/'.\DIRECTORY_SEPARATOR)) {
39+
return false;
40+
}
41+
42+
$command = '\\' === \DIRECTORY_SEPARATOR ? 'where %s 2> NUL' : 'command -v -- %s';
43+
$execResult = exec(\sprintf($command, escapeshellarg($php)));
44+
if (!$php = substr($execResult, 0, strpos($execResult, \PHP_EOL) ?: null)) {
45+
return false;
46+
}
47+
if (!is_executable($php)) {
4448
return false;
4549
}
4650
}

0 commit comments

Comments
 (0)