diff --git a/composer.json b/composer.json index 4a62900a..0ddc3bf3 100644 --- a/composer.json +++ b/composer.json @@ -70,6 +70,7 @@ "files": [ "src/BigIntegor.php", "src/YieldReturnValue.php", + "src/danog/MadelineProto/Ipc/Runner/entry.php", "src/polyfill.php" ] }, diff --git a/src/danog/MadelineProto/API.php b/src/danog/MadelineProto/API.php index c0b7a8f5..10590f70 100644 --- a/src/danog/MadelineProto/API.php +++ b/src/danog/MadelineProto/API.php @@ -188,7 +188,7 @@ class API extends InternalDoc { $this->init(); if (!$this->oldInstance) { - $this->logger->logger('Shutting down MadelineProto ('.\explode('\\', \get_class($this))[2].')'); + $this->logger->logger('Shutting down MadelineProto ('.static::class.')'); if ($this->API) { $this->API->destructing = true; $this->API->unreference(); diff --git a/src/danog/MadelineProto/Ipc/Runner/WebRunner.php b/src/danog/MadelineProto/Ipc/Runner/WebRunner.php index 355e7a80..233ff6fb 100644 --- a/src/danog/MadelineProto/Ipc/Runner/WebRunner.php +++ b/src/danog/MadelineProto/Ipc/Runner/WebRunner.php @@ -10,13 +10,11 @@ final class WebRunner extends RunnerAbstract { /** @var string|null Cached path to the runner script. */ private static $runPath; - /** - * Initialization payload. - * - * @var array - */ - private $params; + /** + * Resources + */ + private static array $resources = []; /** * Socket. * @@ -46,7 +44,7 @@ final class WebRunner extends RunnerAbstract if (!$rootDir) { throw new ContextException('Could not get entry file!'); } - $rootDir = \dirname($rootDir); + $rootDir = \dirname($rootDir).DIRECTORY_SEPARATOR; $uriDir = \dirname($uri); if (\substr($rootDir, -\strlen($uriDir)) !== $uriDir) { @@ -78,12 +76,12 @@ final class WebRunner extends RunnerAbstract self::$runPath = \str_replace('//', '/', self::$runPath); } - $this->params = [ - 'argv' => ['pony', 'madeline-ipc', $session], + $params = [ + 'argv' => ['madeline-ipc', $session], 'cwd' => Magic::getcwd() ]; - $params = \http_build_query($this->params); + $params = \http_build_query($params); $address = ($_SERVER['HTTPS'] ?? false ? 'tls' : 'tcp').'://'.$_SERVER['SERVER_NAME']; $port = $_SERVER['SERVER_PORT']; @@ -95,6 +93,6 @@ final class WebRunner extends RunnerAbstract // We don't care for results or timeouts here, PHP doesn't count IOwait time as execution time anyway // Technically should use amphp/socket, but I guess it's OK to not introduce another dependency just for a socket that will be used once. \fwrite($res = \fsockopen($address, $port), $payload); - \fclose($res); + self::$resources []= $res; } } diff --git a/src/danog/MadelineProto/Ipc/Runner/entry.php b/src/danog/MadelineProto/Ipc/Runner/entry.php index 2e87d0ef..8a54511c 100644 --- a/src/danog/MadelineProto/Ipc/Runner/entry.php +++ b/src/danog/MadelineProto/Ipc/Runner/entry.php @@ -24,23 +24,31 @@ use danog\MadelineProto\Magic; use danog\MadelineProto\SessionPaths; use danog\MadelineProto\Tools; -(static function () use (&$argv): void { - $ipcPath = null; - if (\defined(\MADELINE_WORKER_START::class)) { - $ipcPath = \MADELINE_WORKER_START; - } elseif (\count(\debug_backtrace(0)) === 1) { +(static function (): void { + if (\defined(\MADELINE_ENTRY::class)) { + // Already called + return; + } + \define(\MADELINE_ENTRY::class, 1); + if (!\defined(\MADELINE_WORKER_TYPE::class)) { + if (\count(\debug_backtrace(0)) !== 1) { + // We're not being included directly + return; + } + $arguments = []; if (isset($GLOBALS['argv']) && !empty($GLOBALS['argv'])) { - $arguments = $GLOBALS['argv']; + $arguments = \array_slice($GLOBALS['argv'], 1); } elseif (isset($_GET['argv']) && !empty($_GET['argv'])) { $arguments = $_GET['argv']; - } else { - return; } - if (isset($arguments[1]) && $arguments[1] === 'madeline-ipc') { - $ipcPath = $arguments[2]; - } else { - return; + if (\count($arguments) < 2) { + \trigger_error("Not enough arguments!", E_USER_ERROR); + exit(1); } + \define(\MADELINE_WORKER_TYPE::class, \array_shift($arguments)); + \define(\MADELINE_WORKER_ARGS::class, $arguments); + } + if (!\class_exists(API::class)) { $paths = [ \dirname(__DIR__, 7)."/autoload.php", \dirname(__DIR__, 5)."/vendor/autoload.php", @@ -60,8 +68,8 @@ use danog\MadelineProto\Tools; include $autoloadPath; } - - if ($ipcPath) { + if (\MADELINE_WORKER_TYPE === 'madeline-ipc') { + $ipcPath = \MADELINE_WORKER_ARGS[0]; if (!\file_exists($ipcPath)) { \trigger_error("IPC session $ipcPath does not exist!", E_USER_ERROR); exit(1); diff --git a/src/danog/MadelineProto/Wrappers/Loop.php b/src/danog/MadelineProto/Wrappers/Loop.php index df4e9ce7..45ee5508 100644 --- a/src/danog/MadelineProto/Wrappers/Loop.php +++ b/src/danog/MadelineProto/Wrappers/Loop.php @@ -56,6 +56,7 @@ trait Loop $this->logger->logger($needs_restart ? 'Will self-restart' : 'Will not self-restart'); if ($needs_restart) { $this->logger->logger("Adding restart callback!"); + $logger = $this->logger; $id = Shutdown::addCallback(static function () use (&$logger) { $address = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? 'tls' : 'tcp').'://'.$_SERVER['SERVER_NAME']; $port = $_SERVER['SERVER_PORT']; diff --git a/tools/phar.php b/tools/phar.php index 3a2ca171..e8c1d099 100644 --- a/tools/phar.php +++ b/tools/phar.php @@ -6,16 +6,17 @@ function ___install_madeline() { if (\count(\debug_backtrace(0)) === 1) { if (isset($GLOBALS['argv']) && !empty($GLOBALS['argv'])) { - $arguments = $GLOBALS['argv']; + $arguments = array_slice($GLOBALS['argv'], 1); } elseif (isset($_GET['argv']) && !empty($_GET['argv'])) { $arguments = $_GET['argv']; } else { $arguments = []; } - if (isset($arguments[1]) && $arguments[1] === 'madeline-ipc') { - \define(\MADELINE_WORKER_START::class, $arguments[2]); + if (count($arguments) >= 2) { + \define(\MADELINE_WORKER_TYPE::class, array_shift($arguments)); + \define(\MADELINE_WORKER_ARGS::class, $arguments); } else { - die('You must include this file in another PHP script'.PHP_EOL); + die('MadelineProto loader: you must include this file in another PHP script, see https://docs.madelineproto.xyz for more info.'.PHP_EOL); } } $old = false;