Skip to content

Commit

Permalink
Merge branch '6.4' into 7.1
Browse files Browse the repository at this point in the history
* 6.4:
  [Process] Properly deal with not-found executables on Windows
  [Process] Fix handling empty path found in the PATH env var with ExecutableFinder
  • Loading branch information
nicolas-grekas committed Oct 31, 2024
2 parents 6aaa189 + 593ecb8 commit 1aa37ba
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
13 changes: 11 additions & 2 deletions ExecutableFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public function find(string $name, ?string $default = null, array $extraDirs = [
}
foreach ($suffixes as $suffix) {
foreach ($dirs as $dir) {
if ('' === $dir) {
$dir = '.';
}
if (@is_file($file = $dir.\DIRECTORY_SEPARATOR.$name.$suffix) && ('\\' === \DIRECTORY_SEPARATOR || @is_executable($file))) {
return $file;
}
Expand All @@ -68,8 +71,14 @@ public function find(string $name, ?string $default = null, array $extraDirs = [
}
}

$command = '\\' === \DIRECTORY_SEPARATOR ? 'where' : 'command -v --';
if (\function_exists('exec') && ($executablePath = strtok(@exec($command.' '.escapeshellarg($name)), \PHP_EOL)) && @is_executable($executablePath)) {
if (!\function_exists('exec') || \strlen($name) !== strcspn($name, '/'.\DIRECTORY_SEPARATOR)) {
return $default;
}

$command = '\\' === \DIRECTORY_SEPARATOR ? 'where %s 2> NUL' : 'command -v -- %s';
$execResult = exec(\sprintf($command, escapeshellarg($name)));

if (($executablePath = substr($execResult, 0, strpos($execResult, \PHP_EOL) ?: null)) && @is_executable($executablePath)) {
return $executablePath;
}

Expand Down
16 changes: 10 additions & 6 deletions PhpExecutableFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,16 @@ public function find(bool $includeArgs = true): string|false
{
if ($php = getenv('PHP_BINARY')) {
if (!is_executable($php)) {
$command = '\\' === \DIRECTORY_SEPARATOR ? 'where' : 'command -v --';
if (\function_exists('exec') && $php = strtok(exec($command.' '.escapeshellarg($php)), \PHP_EOL)) {
if (!is_executable($php)) {
return false;
}
} else {
if (!\function_exists('exec') || \strlen($php) !== strcspn($php, '/'.\DIRECTORY_SEPARATOR)) {
return false;
}

$command = '\\' === \DIRECTORY_SEPARATOR ? 'where %s 2> NUL' : 'command -v -- %s';
$execResult = exec(\sprintf($command, escapeshellarg($php)));
if (!$php = substr($execResult, 0, strpos($execResult, \PHP_EOL) ?: null)) {
return false;
}
if (!is_executable($php)) {
return false;
}
}
Expand Down
21 changes: 21 additions & 0 deletions Tests/ExecutableFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ public function testFindWithOpenBaseDir()
}
}

/**
* @runInSeparateProcess
*/
public function testFindBatchExecutableOnWindows()
{
if (\ini_get('open_basedir')) {
Expand Down Expand Up @@ -138,6 +141,24 @@ public function testFindBatchExecutableOnWindows()
$this->assertSamePath($target.'.BAT', $result);
}

/**
* @runInSeparateProcess
*/
public function testEmptyDirInPath()
{
putenv(sprintf('PATH=%s:', \dirname(\PHP_BINARY)));

touch('executable');
chmod('executable', 0700);

$finder = new ExecutableFinder();
$result = $finder->find('executable');

$this->assertSame('./executable', $result);

unlink('executable');
}

private function assertSamePath($expected, $tested)
{
if ('\\' === \DIRECTORY_SEPARATOR) {
Expand Down

0 comments on commit 1aa37ba

Please sign in to comment.