From 9b0b3c1d83519185ed52a5c79b18dc885797e581 Mon Sep 17 00:00:00 2001 From: MARiA so cute <33935209+NathanFreeman@users.noreply.github.com> Date: Fri, 31 Jul 2026 13:58:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=9C=A8RINIT=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96$=5FSERVER=E5=9C=A8-02=E7=9A=84=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E7=AD=89=E7=BA=A7=E4=B8=8B=E4=BC=9A=E5=87=BA=E7=8E=B0?= =?UTF-8?q?=E5=86=85=E5=AD=98=E9=94=99=E8=AF=AF=E5=92=8C=E6=82=AC=E7=A9=BA?= =?UTF-8?q?=E6=8C=87=E9=92=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Translator.php | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/Translator.php b/src/Translator.php index 6aa5a7b8..acf82083 100644 --- a/src/Translator.php +++ b/src/Translator.php @@ -1048,7 +1048,6 @@ CODE; // FunctionDef::sourceFile comes from loadFile()'s realpath(), so the // CLI script fields always identify main()'s canonical absolute file. $entryFile = $entryFunction->sourceFile; - $code .= $this->registerServerEnvironment($entryFile); $entryFileArg = $this->genCharPtr($entryFile, true); $entryPrefix = str_repeat("\n", max(0, $entryFunction->startLine - 1)); if (count($entryFunction->argInfoList) == 2) { @@ -3216,6 +3215,10 @@ CODE; $methodArgs = implode(', ', array_merge(['this_'], $implicitMethodArgs)); $callParams = $functionDef->argInfoList ? $methodArgs . ', ' . rtrim($callParams, ',') : $methodArgs; } else { + if ($this->isBuildModeBin() && $functionDef->name === self::ENTRY_FUNCTION) { + // $_SERVER 的初始化必须置于 main 入口函数内,以确保在其被访问前,运行环境及超全局上下文已完全就绪。 + $cppCode .= $this->registerServerEnvironment($functionDef->sourceFile); + } $callParams = $functionDef->argInfoList ? rtrim($callParams, ',') : ''; } @@ -3235,19 +3238,17 @@ CODE; private function registerServerEnvironment(string $entryFile): string { - $cppCode = 'php::Var &_SERVER = ' . $this->escapeGlobalVar('_SERVER') . ';' . PHP_EOL; - $cppCode .= 'php::Str php_self = "PHP_SELF";' . PHP_EOL; - $cppCode .= 'php::Str script_name = "SCRIPT_NAME";' . PHP_EOL; - $cppCode .= 'php::Str script_filename = "SCRIPT_FILENAME";' . PHP_EOL; - $cppCode .= 'php::Str path_translated = "PATH_TRANSLATED";' . PHP_EOL; - $cppCode .= 'php::Str document_root = "DOCUMENT_ROOT";' . PHP_EOL; - $cppCode .= 'php::Str value = ' . $this->genCharPtr($entryFile, true) . ';' . PHP_EOL; - - $cppCode .= '_SERVER.item(php_self, true) = value;' . PHP_EOL; - $cppCode .= '_SERVER.item(script_name, true) = value;' . PHP_EOL; - $cppCode .= '_SERVER.item(script_filename, true) = value;' . PHP_EOL; - $cppCode .= '_SERVER.item(path_translated, true) = value;' . PHP_EOL; - $cppCode .= '_SERVER.item(document_root, true) = "";' . PHP_EOL; + /** + * 对于常驻内存型应用,执行完当前逻辑后,会立即进入长时间的事件循环等待。 + * 因此,这些变量仅作为临时用途,用完后应即刻销毁,无需长期持有。 + */ + $cppCode = "const char *value = " . $this->genCharPtr($entryFile, true) . ';' . PHP_EOL; + $cppCode .= 'php::Var &_SERVER = ' . $this->escapeGlobalVar('_SERVER') . ';' . PHP_EOL; + $cppCode .= '_SERVER.item("PHP_SELF", true) = value;'. PHP_EOL; + $cppCode .= '_SERVER.item("SCRIPT_NAME", true) = value;'. PHP_EOL; + $cppCode .= '_SERVER.item("SCRIPT_FILENAME", true) = value;'. PHP_EOL; + $cppCode .= '_SERVER.item("PATH_TRANSLATED", true) = value;'. PHP_EOL; + $cppCode .= '_SERVER.item("DOCUMENT_ROOT", true) = "";' . PHP_EOL; return $cppCode . PHP_EOL; }