Skip to content

Commit

Permalink
Instead of throwing an exception, we instead load out handler script …
Browse files Browse the repository at this point in the history
…'script_not_found.php' to correctly write a message to STDERR and throw an exit code of 127 (command not found). This works around PHP Bug https://bugs.php.net/bug.php?id=64882 whereby the 'Could not open input file' error message goes to STDOUT which triggers an exception in the STDIN stream handler
  • Loading branch information
lucasnetau committed Mar 8, 2022
1 parent 798150b commit b338d19
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
4 changes: 4 additions & 0 deletions bin/script_not_found.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php declare(strict_types=1);

fprintf(STDERR, 'Unable to run PHP script. Script not found');
exit(127);
7 changes: 4 additions & 3 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use function function_exists;
use function fwrite;
use function json_encode;
use function realpath;
use function register_shutdown_function;
use function set_exception_handler;
use const PHP_BINARY;
Expand Down Expand Up @@ -92,7 +93,7 @@ function checkpoint($checkpoint)
function php_cmd(string $filename): string
{
if (!file_exists($filename)) {
throw new ScriptNotFound("$filename not found");
$filename = realpath(__DIR__ . '/../bin/script_not_found.php');
}
return escapeshellarg(PHP_BINARY) . " -f " . escapeshellarg($filename) . " --";
}
Expand All @@ -102,9 +103,9 @@ function php_cmd(string $filename): string
function wrap_source_php_cmd(string $filename): string
{
if (!file_exists($filename)) {
throw new ScriptNotFound("$filename not found");
$filename = realpath(__DIR__ . '/../bin/script_not_found.php');
}
$prepend_file = __DIR__ . '/../bin/source_prepend.php';
$prepend_file = realpath(__DIR__ . '/../bin/source_prepend.php');
return escapeshellarg(PHP_BINARY) . " -d auto_prepend_file=" . escapeshellarg($prepend_file) . " -f " . escapeshellarg($filename) . " --";
}
}
Expand Down
30 changes: 30 additions & 0 deletions tests/process_failure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types=1);

use function EdgeTelemetrics\EventCorrelation\php_cmd;
use function EdgeTelemetrics\EventCorrelation\wrap_source_php_cmd;

include __DIR__ . '/../vendor/autoload.php';

$ping = new React\ChildProcess\Process(php_cmd('unknownfile.php'));
$ping->start();

$ping->on('exit', function($code, $term) {
if ($code === 127) {
echo "Command not found. Test Pass" . PHP_EOL;
}
if ($code !== 0) {
echo "Process Failure code $code" . PHP_EOL;
}
});

$ping->stderr->on('data', function ($chunk) {
error_log('stderr: '. $chunk);
});

$ping->stdout->on('data', function ($chunk) {
error_log('stdout:' . $chunk);
});

React\EventLoop\Loop::get()->run();

echo "Process Failure Test Completed" . PHP_EOL;

0 comments on commit b338d19

Please sign in to comment.