Skip to content

Commit 08416df

Browse files
committed
Use php instead of PHP_BINARY when automatic binary detection fails
1 parent fb0145d commit 08416df

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

src/Factory.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ private function openSocketIo($filename, $flags = null)
375375
private function which($bin)
376376
{
377377
foreach (\explode(\PATH_SEPARATOR, \getenv('PATH')) as $path) {
378-
if (\is_executable($path . \DIRECTORY_SEPARATOR . $bin)) {
378+
if (@\is_executable($path . \DIRECTORY_SEPARATOR . $bin)) {
379379
return $path . \DIRECTORY_SEPARATOR . $bin;
380380
}
381381
}
@@ -396,20 +396,26 @@ private function resolve($filename)
396396

397397
/**
398398
* @return string
399+
* @codeCoverageIgnore Covered by `/tests/FunctionalExampleTest.php` instead.
399400
*/
400401
private function php()
401402
{
402-
// if this is the php-cgi binary, check if we can execute the php binary instead
403-
$binary = \PHP_BINARY;
404-
$candidate = \str_replace('-cgi', '', $binary);
405-
if ($candidate !== $binary && \is_executable($candidate)) {
406-
$binary = $candidate; // @codeCoverageIgnore
403+
$binary = 'php';
404+
if (\PHP_SAPI === 'cli' || \PHP_SAPI === 'cli-server') {
405+
// use same PHP_BINARY in CLI mode, but do not use same binary for CGI/FPM
406+
$binary = \PHP_BINARY;
407+
} else {
408+
// if this is the php-cgi binary, check if we can execute the php binary instead
409+
$candidate = \str_replace('-cgi', '', \PHP_BINARY);
410+
if ($candidate !== \PHP_BINARY && @\is_executable($candidate)) {
411+
$binary = $candidate;
412+
}
407413
}
408414

409415
// if `php` is a symlink to the php binary, use the shorter `php` name
410416
// this is purely cosmetic feature for the process list
411-
if (\realpath($this->which('php')) === $binary) {
412-
$binary = 'php'; // @codeCoverageIgnore
417+
if ($binary !== 'php' && \realpath($this->which('php')) === $binary) {
418+
$binary = 'php';
413419
}
414420

415421
return $binary;

tests/FunctionalExampleTest.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ public function testQueryExampleReturnsCalculatedValueFromPlaceholderVariables()
2222

2323
public function testQueryExampleExecutedWithCgiReturnsDefaultValueAfterContentTypeHeader()
2424
{
25-
$code = 1;
26-
$null = DIRECTORY_SEPARATOR === '\\' ? 'NUL' : '/dev/null';
27-
system("php-cgi --version >$null 2>$null", $code);
28-
if ($code !== 0) {
25+
if (!$this->canExecute('php-cgi --version')) {
2926
$this->markTestSkipped('Unable to execute "php-cgi"');
3027
}
3128

@@ -34,6 +31,37 @@ public function testQueryExampleExecutedWithCgiReturnsDefaultValueAfterContentTy
3431
$this->assertStringEndsWith("\r\n\r\n" . 'value' . PHP_EOL . '42' . PHP_EOL, $output);
3532
}
3633

34+
public function testQueryExampleWithOpenBasedirRestrictedRunsDefaultPhpAndReturnsDefaultValue()
35+
{
36+
if (!$this->canExecute('php --version')) {
37+
$this->markTestSkipped('Unable to execute "php"');
38+
}
39+
40+
$output = $this->execExample(escapeshellarg(PHP_BINARY) . ' -dopen_basedir=' . escapeshellarg(dirname(__DIR__)) . ' query.php');
41+
42+
$this->assertEquals('value' . PHP_EOL . '42' . PHP_EOL, $output);
43+
}
44+
45+
public function testQueryExampleExecutedWithCgiAndOpenBasedirRestrictedRunsDefaultPhpAndReturnsDefaultValueAfterContentTypeHeader()
46+
{
47+
if (!$this->canExecute('php-cgi --version') || !$this->canExecute('php --version')) {
48+
$this->markTestSkipped('Unable to execute "php-cgi" or "php"');
49+
}
50+
51+
$output = $this->execExample('php-cgi -dopen_basedir=' . escapeshellarg(dirname(__DIR__)) . ' query.php');
52+
53+
$this->assertStringEndsWith("\r\n\r\n" . 'value' . PHP_EOL . '42' . PHP_EOL, $output);
54+
}
55+
56+
private function canExecute($command)
57+
{
58+
$code = 1;
59+
$null = DIRECTORY_SEPARATOR === '\\' ? 'NUL' : '/dev/null';
60+
system("$command >$null 2>$null", $code);
61+
62+
return $code === 0;
63+
}
64+
3765
private function execExample($command)
3866
{
3967
chdir(__DIR__ . '/../examples/');

0 commit comments

Comments
 (0)