跳转到正文
wise-* 包教程 20 个可独立安装的 Composer 能力包:选包、装包、调用、扩展

公共基座 wise-support

1 2026-11-25 23:02:34

它是全体 wise-* 包的底层:扩展点注册中心、请求上下文、门面、锁与熔断。不提供业务功能,但几乎所有包都依赖它,所以版本固定 1.0.2(其它包 1.0.1)。

安装:没有配置文件,也不需要

composer require wiseadmin/wise-support

无 config/、无迁移、无需发布配置——由 extra.think.services 自动注册 wise\support\SupportServiceProvider,PSR-4 为 wise\support\ → src/,同时加载 src/helper.php。

依赖只有 PHP >= 8.1、ext-openssl、ThinkPHP ^8.0。装完能用的判据是 vendor/services.php 里出现 wise\support\SupportServiceProvider。

四组入口

ExtPoint——扩展注册中心。 统一"能力注册 → 发现 → 调度",三种策略见跨包扩展与协作。常用:definePoint()、register()、all()、active()、resolve()、setActive()、persistOverrides()、resetRequestState()。

Auth 门面。 wise\support\facade\Auth → AuthManager:user(?string $guard = null)、id()、check()、guest()、authenticate()、setDefaultGuard()、registerGuard()。

Context 门面。 wise\support\facade\Context → RequestContext:set()、get()、setRequestId()、getUserId()、setTenantId()、setLocale()、logContext()、clear()。

这是跨 FPM / Swoole 安全存放请求级数据的唯一推荐位置:协程内按协程 ID 分桶 + Coroutine::defer 回收,非协程回退单桶。所以"当前用户、租户、request_id"放这里,不要放类属性或 static。

JWT 门面是代理,不是实现。

$token   = \wise\support\facade\JWT::issue(42, ['role' => 'admin']);
$payload = \wise\support\facade\JWT::parse($token);

它经 container('jwt') → JwtManager → ExtPoint('jwt.provider') 找当前引擎(默认由 wise-jwt 注册)。引擎未就绪时降级为未登录——所以"明明配了 JWT 却拿不到用户"要先看引擎有没有注册成功,而不是查 token。

还有这些基础件

  • 分布式锁、断路器 CircuitBreaker、退避策略 BackoffStrategy、限流器 RateLimiter;
  • ConnectionManager、MessageRouter、ConfigCheckCollector;
  • WebSocket 处理器与心跳(ws.handler、ws.heartbeat);
  • Pipeline 引擎、统一异常与错误码体系;
  • RequestStateRegistry + HttpEnd 事件做请求级清理。

这些是"包里重复出现的基础需求"的收口处。自己写业务时如果发现要造锁/重试/熔断,先来这里找。

最小可用示例

use wise\support\ExtPoint;
use wise\support\facade\Context;

ExtPoint::definePoint('example.single', ExtPoint::POLICY_SINGLE, SomeDriverInterface::class);
ExtPoint::register('example.single', 'alpha', AlphaDriver::class);
$active = ExtPoint::active('example.single');

Context::setRequestId('req-123');
Context::set('locale', 'zh-CN');
$locale = Context::get('locale');
Context::clear();

三个容易搞错的认知

一、"它是个可选工具包"。 别的包都 require 它,卸了等于全站不可用。
二、"JWT 门面自己会签令牌"。 它只是代理,实现在 wise-jwt;引擎停用时表现为未登录。
三、"Context 和 $request 等价"。 $request 是 HTTP 请求对象,Context 是跨层可用的请求级存储(CLI 下也能用,此时是单桶)。

自查:composer show wiseadmin/wise-support 应显示 1.0.2;vendor/services.php 含 SupportServiceProvider;\wise\support\ExtPoints::class 可解析。

多运行时约束(哪些能放静态、哪些不能)见多运行时兼容。

这篇文档对您有帮助吗?

相关文档