Skip to content

Commit 92901cd

Browse files
committed
1 parent b8648cf commit 92901cd

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

ExecutableFinder.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*/
2020
class ExecutableFinder
2121
{
22-
private $suffixes = ['.exe', '.bat', '.cmd', '.com'];
22+
private $suffixes = [];
2323

2424
/**
2525
* Replaces default suffixes of executable.
@@ -70,19 +70,38 @@ public function find($name, $default = null, array $extraDirs = [])
7070
);
7171
}
7272

73-
$suffixes = [''];
73+
$suffixes = [];
7474
if ('\\' === \DIRECTORY_SEPARATOR) {
7575
$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']);
7778
}
79+
$suffixes = '' !== pathinfo($name, PATHINFO_EXTENSION) ? array_merge([''], $suffixes) : array_merge($suffixes, ['']);
7880
foreach ($suffixes as $suffix) {
7981
foreach ($dirs as $dir) {
82+
if ('' === $dir) {
83+
$dir = '.';
84+
}
8085
if (@is_file($file = $dir.\DIRECTORY_SEPARATOR.$name.$suffix) && ('\\' === \DIRECTORY_SEPARATOR || @is_executable($file))) {
8186
return $file;
8287
}
88+
89+
if (!@is_dir($dir) && basename($dir) === $name.$suffix && @is_executable($dir)) {
90+
return $dir;
91+
}
8392
}
8493
}
8594

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+
86105
return $default;
87106
}
88107
}

PhpExecutableFinder.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ public function __construct()
3535
*/
3636
public function find($includeArgs = true)
3737
{
38+
if ($php = getenv('PHP_BINARY')) {
39+
if (!is_executable($php) && !$php = $this->executableFinder->find($php)) {
40+
return false;
41+
}
42+
43+
if (@is_dir($php)) {
44+
return false;
45+
}
46+
47+
return $php;
48+
}
49+
3850
$args = $this->findArguments();
3951
$args = $includeArgs && $args ? ' '.implode(' ', $args) : '';
4052

Process.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,6 +1664,15 @@ function ($m) use (&$env, &$varCache, &$varCount, $uid) {
16641664
);
16651665

16661666
$cmd = 'cmd /V:ON /E:ON /D /C ('.str_replace("\n", ' ', $cmd).')';
1667+
static $comSpec;
1668+
1669+
if (!$comSpec && $comSpec = (new ExecutableFinder())->find('cmd.exe')) {
1670+
// Escape according to CommandLineToArgvW rules
1671+
$comSpec = '"'.preg_replace('{(\\\\*+)"}', '$1$1\"', $comSpec) .'"';
1672+
}
1673+
1674+
$cmd = ($comSpec !== null ? $comSpec : 'cmd').' /V:ON /E:ON /D /C ('.str_replace("\n", ' ', $cmd).')';
1675+
16671676
foreach ($this->processPipes->getFiles() as $offset => $filename) {
16681677
$cmd .= ' '.$offset.'>"'.$filename.'"';
16691678
}

0 commit comments

Comments
 (0)