Remove output buffering in child process; update require scoping

This commit is contained in:
Aaron Piotrowski 2018-10-15 00:15:56 -05:00
parent fa864692e7
commit bf08e2fdf4
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB

View File

@ -12,12 +12,6 @@ if (\function_exists("cli_set_process_title")) {
@\cli_set_process_title("amp-process");
}
// Redirect all output written using echo, print, printf, etc. to STDERR.
\ob_start(function ($data) {
\fwrite(\STDERR, $data);
return '';
}, 1, \PHP_OUTPUT_HANDLER_CLEANABLE | \PHP_OUTPUT_HANDLER_FLUSHABLE);
(function () {
$paths = [
\dirname(__DIR__, 5) . "/autoload.php",
@ -75,25 +69,23 @@ Loop::run(function () use ($argc, $argv) {
}
try {
if (!isset($argv[0])) {
throw new \Error("No script path given");
}
if (!\is_file($argv[0])) {
throw new \Error(\sprintf("No script found at '%s' (be sure to provide the full path to the script)", $argv[0]));
}
// Protect current scope by requiring script within another function.
$callable = (function () use ($argc, $argv): callable {
if (!isset($argv[0])) {
throw new \Error("No script path given");
}
if (!\is_file($argv[0])) {
throw new \Error(\sprintf("No script found at '%s' (be sure to provide the full path to the script)", $argv[0]));
}
$callable = require $argv[0];
if (!\is_callable($callable)) {
throw new \Error(\sprintf("Script '%s' did not return a callable function", $argv[0]));
}
return $callable;
$callable = (function () use ($argc, $argv) { // Using $argc so it is available to the required script.
return require $argv[0];
})();
if (!\is_callable($callable)) {
throw new \Error(\sprintf("Script '%s' did not return a callable function", $argv[0]));
}
$result = new Sync\ExitSuccess(yield call($callable, $channel));
} catch (Sync\ChannelException $exception) {
exit(1); // Parent context died, simply exit.