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

通用安装与自动发现链

1 2026-11-25 23:02:34

装一个 wise-* 包只需两条命令:composer require 装代码并触发自动发现,wise:package:install 做有状态的初始化(建表、种子、菜单、资源)。迁移和种子不会自动跑,这是刻意的。

标准安装

composer require wiseadmin/wise-auth
php think wise:package:install wiseadmin/wise-auth

第一条把包装进 vendor/wiseadmin/wise-auth/ 并跑自动发现;第二条才真正建表。漏掉第二条的症状是"代码在、页面报表不存在"。

wiseadmin/framework 是 type: project,只用于 composer create-project,不要 composer require wiseadmin/framework。

extra.think:包的自我声明

包通过 composer.json 的 extra.think 告诉框架自己怎么接入:

键类型作用消费命令
servicesstring[]ServiceProvider 自动发现service:discover → vendor/services.php
config{name: path}默认配置发布vendor:publish
publish{源: 目标} 或 string[]包内静态资源 → 宿主 public/wise:vendor:publish
menusstring指向 menu.php / menu.json宿主菜单注册器
identstring一般不用写,缺省取包名短名PackageRegistry::identFor()

以 wise-agent 为例:

"extra": {
  "think": {
    "services": ["wise\\agent\\AgentServiceProvider"],
    "config":  { "wise-agent": "config/wise-agent.php" },
    "publish": { "public": "public/statics/plugins/wise-agent" }
  }
}

两个"写错也不报错"的地方:没有 extra.think.migrate-path(迁移目录由 ServiceProvider 里 RegistersMigrations 声明),不要写包内 composer.json 的 scripts(Composer 只执行根包的 scripts,写了等于没写)。

post-autoload-dump:每次 composer 变更都跑

install / update / require / remove / dump-autoload 结束时触发,顺序不可调换:

"post-autoload-dump": [
    "@php think service:discover",
    "@php think plugin:migrate:register",
    "@php think vendor:publish",
    "@php think wise:vendor:publish",
    "@php think menu:sync --prune-orphans --silent",
    "@php think wise:package:cleanup --reconcile --apply --silent"
]
命令作用出问题时的表现
service:discover扫 extra.think.services,生成 vendor/services.php包的服务不启动
plugin:migrate:register重新登记多路径迁移命令(幂等)migrate:run 只扫单路径,包的表不建
vendor:publish按 extra.think.config 发布默认配置到 config/读不到配置默认值
wise:vendor:publish发布包内 public/ 到宿主 public/;增量复制,默认跳过已存在文件静态资源 404
menu:sync --prune-orphans同步扩展点声明的菜单,软删孤儿菜单残留或缺失
wise:package:cleanup --reconcile --apply清理"登记过但已不在安装集合"的包卸载后残留菜单/权限/表

plugin:migrate:register 必须紧跟在 service:discover 之后:后者会重写 vendor/services.php,而多路径迁移能力正是靠这个文件里的注册项;放在 publish 之后不会报错,但会让 vendor:publish / wise:vendor:publish 先跑一遍未注册态,往 stderr 刷一条 [wise] 警告:多路径迁移命令未注册。检测用 plugin:migrate:register --check(0 已注册 / 1 未注册 / 2 前置缺失);plugin:migrate:register 自身启动时那一条提示属预期,可忽略。

迁移与种子不在这条链里:它们有状态、不可回滚,必须显式 wise:package:install。

wise:package:install 做了什么

php think wise:package:install wiseadmin/wise-auth
php think wise:package:install --all
php think wise:package:install --all --dry-run
php think wise:package:install wiseadmin/wise-auth --skip-migrate --skip-seed

内部步骤(每步幂等):

service:discover → plugin:migrate:register → migrate:run → seeds
→ PackageInstallable::install() → menu:sync
→ wise:vendor:publish → registry.record

--all 处理的是全部"接入型"包,不是字面上的所有 vendor 包。判据:extra.think.services/config/publish/menus 满足任一,或包内有 database/migrations;唯一例外是 topthink/* 自身。

卸载是两步,顺序不能反

第一步:卸载包自己登记的东西(此时包文件还要能加载,所以必须在 composer remove 之前):

php think wise:package:uninstall wiseadmin/wise-auth

第二步:移除代码,Composer 钩子会自动接后续清理:

composer remove <pkg>
 ├─ pre-package-uninstall   → 写现场快照
 ├─ post-package-uninstall  → wise:package:cleanup <pkg> --apply --silent
 └─ dump-autoload           → menu:sync --prune-orphans + cleanup --reconcile

手工补救(比如直接删了 vendor/ 目录):

php think wise:package:cleanup wiseadmin/wise-auth          # 默认只预览
php think wise:package:cleanup wiseadmin/wise-auth --apply  # 真正清理
php think wise:package:cleanup --reconcile --apply          # 手删 vendor 后的兜底

清理依据是 .wise/package_registry.json(刻意不放在 runtime/,因为 php think clear 会清空它);审计日志在 runtime/package_cleanup.log。

包与插件的边界

  • 包永远在 vendor/,不复制、不软链进 plugins/(composer_bridge 已移除);
  • 插件是 zip → plugins/{ident}/ + manifest.json,走 PluginManager 生命周期;
  • 包内 PHP 不引用 app\... 命名空间,否则无法独立安装;
  • 插件要用包能力,在 manifest 的 packages 段声明依赖。

三个容易搞错的认知

一、"composer require 就装好了"。 代码装好了,表还没建,要跑 wise:package:install。
二、"--all 会把 vendor 里所有包都初始化"。 只处理"接入型"包。
三、"先 composer remove 再清理也一样"。 顺序反了包已经加载不了,uninstall() 跑不起来,只能靠 cleanup --reconcile 兜底。

自查顺序:composer show wiseadmin/wise-auth 有版本 → Test-Path config/wise-auth.php 为 True → wise:package:install --dry-run 列出步骤 → 正式安装后表已建。

这篇文档对您有帮助吗?

相关文档