MadelineProto/src/danog/MadelineProto/Ipc/Runner/ProcessRunner.php
2020-07-12 01:12:20 +02:00

75 lines
1.9 KiB
PHP

<?php
namespace danog\MadelineProto\Ipc\Runner;
use Amp\Process\Process as BaseProcess;
use Amp\Promise;
use danog\MadelineProto\Magic;
final class ProcessRunner extends RunnerAbstract
{
/** @var string|null Cached path to located PHP binary. */
private static $binaryPath;
/**
* Runner.
*
* @param string $session Session path
*/
public static function start(string $session): void
{
if (\PHP_SAPI === "cli") {
$binary = \PHP_BINARY;
} else {
$binary = self::$binaryPath ?? self::locateBinary();
}
$options = [
"html_errors" => "0",
"display_errors" => "0",
"log_errors" => "1",
];
$runner = self::getScriptPath();
$command = \implode(" ", [
'nohup',
\escapeshellarg($binary),
self::formatOptions($options),
$runner,
'madeline-ipc',
\escapeshellarg($session),
'&>/dev/null &'
]);
shell_exec($command);
}
private static function locateBinary(): string
{
$executable = \strncasecmp(\PHP_OS, "WIN", 3) === 0 ? "php.exe" : "php";
$paths = \array_filter(\explode(\PATH_SEPARATOR, \getenv("PATH")));
$paths[] = \PHP_BINDIR;
$paths = \array_unique($paths);
foreach ($paths as $path) {
$path .= \DIRECTORY_SEPARATOR.$executable;
if (\is_executable($path)) {
return self::$binaryPath = $path;
}
}
throw new \Error("Could not locate PHP executable binary");
}
private static function formatOptions(array $options): string
{
$result = [];
foreach ($options as $option => $value) {
$result[] = \sprintf("-d%s=%s", $option, $value);
}
return \implode(" ", $result);
}
}