一句话原则:只写一份代码,不写 FPM/Swoole 分支。优先级是 ThinkPHP 原生 API >
wise-support收口 > 裸 Swoole(禁止)。这篇只讲插件侧的落地约束,运行时机制见多运行时兼容。
红线清单
| 需求 | 用 | 不要用 |
|---|---|---|
| 输入 | $request->param()/file()/server()/header() | $_GET $_POST $_FILES $_SERVER |
| 请求级数据 | $request->xxx、RequestContext | 插件类属性、static $var 存用户/租户 |
| 探测环境 | class_exists() 守卫 | 散写 extension_loaded('Swoole') |
| 安装期动作 | CLI(plugin:install / 迁移 / 种子) | 从 HTTP 路径触发 exec/shell_exec/sleep |
| 配置 | 请求级传参 | Config::set() 改全局后依赖它 |
| Redis | persistent => false | 开 pconnect |
| 响应 | 返回 Response / 统一 JSON | header() / setcookie() / exit / die |
"用 $request->server()、不用 $_SERVER"不矛盾:前者是框架 API,后者是超全局。
陷阱一:boot 链会重复执行
PluginLoader(HTTP 首请求)与 PluginServiceProvider(CLI 命令)都可能触发 initServiceProvider()。基类做了进程级去重:
// WiseBootServiceProvider.php
private static array $booted = [];
// boot() 开头
if (isset(self::$booted[$pluginIdent])) { return; }但基类只对 11 步 boot 链自身去重。你自己在 boot() 里追加的注册动作必须自行幂等:
private static ?\WeakMap $assembled = null;
public function boot(): void
{
if ($this->assembled()) { return; }
// 注册事件监听 / describer
}不幂等的后果很具体:Event::listen() 重复追加,一次能力启停触发多次审计写库。
配套的两条硬要求:
common.php 里所有函数必须 function_exists() 包裹(脚手架 stubs/plugin/common.stub 已内置):
if (!function_exists('acme_ticket_config')) {
function acme_ticket_config(?string $key = null, $default = null)
{
$config = config('acme_ticket');
return $key === null ? $config : ($config[$key] ?? $default);
}
}event.php 同理:include 后执行的是 Event::bind/listen/subscribe,重复 include 就重复绑定。
还有一条容易被忘:CLI 命令只能在 register() 里注册,不能放 boot()。Service::commands() 走 Console::starting 回调,控制台构造完成后再注册就失效了。
陷阱二:请求级静态状态 = 串号
协程共享进程,static 属性放当前用户必然串号:
// 反例:下一个请求读到上一个用户
private static ?int $currentUserId = null;
// 正例:请求级数据走 RequestContext
$userId = \wise\support\context\RequestContext::getUserId();
$tenantId = \wise\support\context\RequestContext::getTenantId();
$rid = \wise\support\context\RequestContext::getRequestId(); // 未设置时是 "unknown"
$keyword = $request->param('keyword');RequestContext 按协程 ID 分桶,协程结束由 Runtime::defer() 回收;非协程回退 'global' 单桶,并在 setRequestId() 检测到 ID 变化时 unset(global),避免上一请求的身份串过来。clear() 只清当前桶。
Once::once() 也要留意:FPM 下静态属性随进程回收,常驻下语义退化成"同一 worker 只执行一次",身份/权限值会跨请求串号,闭包引用还会无界增长。宿主由 SupportStateResetter 在请求边界 flush()。
视图 scope 必须写在请求对象上:PluginDispatch 用 $this->request->view_scope = $ident。不要改成 Config::set(['view_path' => ...], 'view')——那是全局污染,常驻下会串到别的插件。
陷阱三:HTTP 路径禁阻塞
exec / shell_exec / system / proc_open / sleep 只在 CLI(安装、迁移、队列 worker)里允许。
协程里直接 usleep() 会阻塞整个 worker:一个请求的重试等待会把同进程其它请求一起卡住。要延时用 Runtime::sleep(),要异步投 think-queue。
两个进程级去重
HTTP 侧 PluginLoader 用 protected static bool $booted = false;,只在首次请求初始化插件。运行期间安装/启用新插件后,需要重启 FPM 或调 resetBootState()(会联动 WiseBootServiceProvider::resetBootState())。
CLI 侧 PluginServiceProvider 用 \think\Console::starting() 延迟到 Console::start() 才初始化——因为 register() / boot() 阶段 ORM 还没有数据库连接,会报 Unable to resolve NULL driver for [think\Cache] 或 Call to a member function connect() on null。
一个历史雷区别再踩:不要在插件 boot 链内部解析 Console facade,否则 startCallbacks 递归触发,最终 Allowed memory size exhausted。
三个容易搞错的认知
一、"基类去重了就够"。 只去重 11 步链,你自己的注册动作要自己幂等。
二、"FPM 下跑通就没问题"。 静态属性、Once、阻塞调用这三类问题只在常驻下暴露。
三、"改完代码行为没变,是缓存"。 常驻模式下 worker 不重启就不重载代码,先确认进程重启了。
自查:
\wise\plugin\WiseBootServiceProvider::resetBootState();
\app\common\middleware\PluginLoader::resetBootState();
dump(\wise\support\ExtPoint::getUndefinedRegistrations()); // 应为空
dump(\wise\support\ExtPoint::count()); // 重复 boot 后不应增长
dump(\wise\support\context\RequestContext::logContext()); // 两个 request_id 后不应残留旧身份本机没有 ext-swoole 时(Windows 就不支持),验证常驻要走 WSL2 或 phpswoole/swoole 镜像;否则保持 FPM——think-swoole 只是 require-dev / suggest,--no-dev 部署不会安装它。