Skip to content

📦 依赖管理

本页讲解 pkg/composer 中与项目依赖安装、更新、自动加载相关的方法,定义在 dependencies.goadditional_methods.go。它们对应 composer 的 installupdatedump-autoloadsuggestsfundaudit 等子命令。

🎯 何时使用

  • 📥 克隆项目后第一次安装依赖 → Install
  • 🔄 把依赖升级到最新版本 → Update
  • ⚡ 重新生成自动加载器(新增类后) → DumpAutoload
  • 🧪 想预演一次安装/更新而不真正改动 → InstallDryRun / UpdateDryRun
  • 🏭 生产部署需要极致优化 → InstallWithClassmapAuthoritative / InstallWithAPCu
  • 🔒 只修复 composer.lock 哈希而不动包版本 → UpdateWithLock

📋 方法总览

下表列出依赖管理相关的全部方法。带 WithOptions 的变体接受 map[string]string 自定义 composer 长选项;带 DryRun 的变体只模拟不真正执行。

方法签名概要等价命令
📥 InstallInstall(noDev bool, optimize bool) errorcomposer install [--no-dev] [--optimize-autoloader]
InstallWithOptionsInstallWithOptions(options map[string]string) errorcomposer install [options]
InstallWithPreferSourceInstallWithPreferSource() errorcomposer install --prefer-source
InstallWithPreferDistInstallWithPreferDist() errorcomposer install --prefer-dist
InstallNoScriptsInstallNoScripts() errorcomposer install --no-scripts
InstallWithClassmapAuthoritativeInstallWithClassmapAuthoritative() errorcomposer install --classmap-authoritative
InstallWithAPCuInstallWithAPCu() errorcomposer install --apcu-autoloader
InstallNoDevInstallNoDev() errorcomposer install --no-dev
InstallWithWorkingDirInstallWithWorkingDir(workingDir string, noDev bool, optimize bool) error在指定目录执行 install
🧪 InstallDryRunInstallDryRun() (string, error)composer install --dry-run
🔄 UpdateUpdate(packages []string, noDev bool) errorcomposer update [--no-dev] [packages...]
UpdateWithOptionsUpdateWithOptions(packages []string, options map[string]string) errorcomposer update [options] [packages...]
UpdateWithPreferSourceUpdateWithPreferSource(packages []string) errorcomposer update --prefer-source [packages...]
UpdateWithPreferDistUpdateWithPreferDist(packages []string) errorcomposer update --prefer-dist [packages...]
UpdateNoScriptsUpdateNoScripts(packages []string) errorcomposer update --no-scripts [packages...]
UpdateNoDevUpdateNoDev(packages []string) errorcomposer update --no-dev [packages...]
UpdateWithDependenciesUpdateWithDependencies(packages []string) errorcomposer update --with-dependencies [packages...]
UpdateWithAllDependenciesUpdateWithAllDependencies(packages []string) errorcomposer update --with-all-dependencies [packages...]
UpdateWithLockUpdateWithLock() errorcomposer update --lock
🧪 UpdateDryRunUpdateDryRun(packages []string) (string, error)composer update --dry-run [packages...]
DumpAutoloadDumpAutoload(optimize bool) errorcomposer dump-autoload [--optimize]
DumpAutoloadWithOptionsDumpAutoloadWithOptions(options map[string]string) errorcomposer dump-autoload [options]
🔍 CheckDependenciesCheckDependencies() (string, error)composer check
💡 SuggestsSuggests() errorcomposer suggests
💰 FundPackagesFundPackages() (string, error)composer fund
🔒 RunAuditRunAudit() (string, error)composer audit

选项 map 的约定

*WithOptions 接收的 map[string]string,键是 composer 长选项名(不含 --),值若为空串 "" 则视为纯开关选项(只加 --key),否则生成 --key=value。例如 {"no-dev": "", "prefer-dist": ""} 会拼成 --no-dev --prefer-dist


📥 Install

安装项目的所有依赖,基于 composer.json。这是最常用的方法。

签名

go
func (c *Composer) Install(noDev bool, optimize bool) error

参数

参数类型说明
noDevbooltrue 则加 --no-dev,不安装 require-dev 中的开发依赖
optimizebooltrue 则加 --optimize-autoloader,优化自动加载器

返回值

类型说明
error失败时返回包裹了 ErrInstallFailed 的错误

示例

go
// 安装所有依赖(包括开发依赖)
err := comp.Install(false, false)
if err != nil {
    log.Fatalf("安装依赖失败: %v", err)
}

// 只安装生产依赖并优化自动加载(生产部署常用)
err = comp.Install(true, true)

进阶

  • 需要更多选项时用 InstallWithOptions
  • 想预演而不真正安装用 InstallDryRun

⚙️ InstallWithOptions

使用自定义选项安装依赖,支持任意 composer 长选项组合。

签名

go
func (c *Composer) InstallWithOptions(options map[string]string) error

参数

参数类型说明
optionsmap[string]string安装选项映射,键为选项名,值为选项值(空串表示开关)

示例

go
options := map[string]string{
    "no-dev":            "",
    "optimize-autoloader": "",
    "prefer-dist":       "",
    "no-progress":       "",
}
err := comp.InstallWithOptions(options)
if err != nil {
    log.Fatalf("安装依赖失败: %v", err)
}

🔄 Update

更新项目依赖到最新版本,可指定包名或更新全部。

签名

go
func (c *Composer) Update(packages []string, noDev bool) error

参数

参数类型说明
packages[]string要更新的包名列表,为空切片则更新所有包
noDevbooltrue 则加 --no-dev

示例

go
// 更新所有依赖(包括开发依赖)
err := comp.Update([]string{}, false)

// 只更新指定的包
err = comp.Update([]string{"symfony/console", "symfony/process"}, false)

// 只更新生产依赖
err = comp.Update([]string{}, true)

进阶

  • 想连带更新间接依赖用 UpdateWithDependenciesUpdateWithAllDependencies
  • 只刷新 lock 哈希用 UpdateWithLock

⚙️ UpdateWithOptions

使用自定义选项更新依赖。

签名

go
func (c *Composer) UpdateWithOptions(packages []string, options map[string]string) error

参数

参数类型说明
packages[]string要更新的包名列表
optionsmap[string]string更新选项映射

示例

go
options := map[string]string{
    "no-dev":            "",
    "prefer-dist":       "",
    "with-dependencies": "",
    "no-progress":       "",
}
err := comp.UpdateWithOptions([]string{"symfony/console"}, options)

DumpAutoload

生成 Composer 的自动加载文件,可选择是否优化。

签名

go
func (c *Composer) DumpAutoload(optimize bool) error

参数

参数类型说明
optimizebooltrue 则加 --optimize,生成类映射(生产环境推荐)

示例

go
// 生成标准自动加载文件(开发环境)
err := comp.DumpAutoload(false)

// 生成优化的自动加载文件(生产部署)
err = comp.DumpAutoload(true)

进阶

  • 需要更多选项(如 --classmap-authoritative--apcu--no-dev)用 DumpAutoloadWithOptions

⚙️ DumpAutoloadWithOptions

使用自定义选项生成自动加载文件。

签名

go
func (c *Composer) DumpAutoloadWithOptions(options map[string]string) error

示例

go
options := map[string]string{
    "optimize":             "",
    "classmap-authoritative": "",
    "apcu":                 "",
    "no-dev":               "",
}
err := comp.DumpAutoloadWithOptions(options)

🔒 UpdateWithLock

仅更新 composer.lock 文件的哈希值,不实际更新任何包版本。当 composer.lock 哈希与 composer.json 不同步时用来修复。

签名

go
func (c *Composer) UpdateWithLock() error

示例

go
err := comp.UpdateWithLock()
if err != nil {
    log.Fatalf("更新 lock 文件失败: %v", err)
}

🧪 InstallDryRun / UpdateDryRun

模拟执行安装/更新,不真正改动文件系统,常用于 CI 预演或检查会变更哪些包。

签名

go
func (c *Composer) InstallDryRun() (string, error)
func (c *Composer) UpdateDryRun(packages []string) (string, error)

参数(UpdateDryRun

参数类型说明
packages[]string要模拟更新的包名列表,为空则模拟更新所有包

返回值

类型说明
string模拟执行的输出结果
error执行错误

示例

go
output, err := comp.InstallDryRun()
if err != nil {
    log.Fatalf("模拟安装失败: %v", err)
}
fmt.Println(output)

output, err = comp.UpdateDryRun([]string{"symfony/console"})

🏭 单选项快捷方法

以下方法各自只附加一个固定选项,签名简单、语义清晰:

方法等价命令说明
InstallWithPreferSource() errorinstall --prefer-source强制从源码(Git)安装,便于调试/改源码
InstallWithPreferDist() errorinstall --prefer-dist强制从分发包(zip)安装,速度快,适合生产
InstallNoScripts() errorinstall --no-scripts跳过脚本执行,CI/CD 常用
InstallWithClassmapAuthoritative() errorinstall --classmap-authoritative权威类映射,仅从类映射加载,提升生产性能
InstallWithAPCu() errorinstall --apcu-autoloader启用 APCu 缓存自动加载,需 PHP APCu 扩展
InstallNoDev() errorinstall --no-dev不安装开发依赖
UpdateWithPreferSource(packages []string) errorupdate --prefer-source [packages...]从源码更新
UpdateWithPreferDist(packages []string) errorupdate --prefer-dist [packages...]从分发包更新
UpdateNoScripts(packages []string) errorupdate --no-scripts [packages...]跳过脚本更新
UpdateNoDev(packages []string) errorupdate --no-dev [packages...]不更新开发依赖
UpdateWithDependencies(packages []string) errorupdate --with-dependencies [packages...]连带更新依赖
UpdateWithAllDependencies(packages []string) errorupdate --with-all-dependencies [packages...]递归更新所有依赖

示例

go
// 生产部署:分发包 + 不跑脚本 + 权威类映射
_ = comp.InstallWithPreferDist()
_ = comp.InstallNoScripts()
_ = comp.InstallWithClassmapAuthoritative()

// 连带依赖更新指定包
_ = comp.UpdateWithDependencies([]string{"symfony/console"})
_ = comp.UpdateWithAllDependencies([]string{"symfony/console"})

APCu 前置条件

InstallWithAPCu 需要 PHP 安装并启用 APCu 扩展,否则 composer 会报错。


📁 InstallWithWorkingDir

在指定工作目录执行 install,执行完毕后恢复原工作目录。适合一次性操作别的项目而不影响实例默认目录。

签名

go
func (c *Composer) InstallWithWorkingDir(workingDir string, noDev bool, optimize bool) error

参数

参数类型说明
workingDirstring工作目录路径
noDevbool是否跳过开发依赖
optimizebool是否优化自动加载器

示例

go
err := comp.InstallWithWorkingDir("/srv/other-app", true, true)
if err != nil {
    log.Fatalf("安装失败: %v", err)
}

进阶

  • 实现上临时修改 c.workingDir,用 defer 恢复原值;若执行过程中发生 panic 也能恢复。
  • 更通用的"在别的目录跑任意命令"可用 ExecWithWorkingDir(见 exec)。

🔍 CheckDependencies

检查 composer.jsoncomposer.lock 是否同步、依赖是否有冲突。

签名

go
func (c *Composer) CheckDependencies() (string, error)

示例

go
output, err := comp.CheckDependencies()
if err != nil {
    log.Fatalf("检查依赖失败: %v", err)
}
fmt.Println("依赖检查结果:", output)

💡 Suggests

查看并安装建议的软件包(composer suggests)。

签名

go
func (c *Composer) Suggests() error

进阶

  • 想带选项用 SuggestsWithOptions(options map[string]string) (string, error)
  • 想查某个特定包的建议用 SuggestsForPackage(packageName string) (string, error)

💰 FundPackages / 🔒 RunAudit

方法签名等价命令
FundPackagesFundPackages() (string, error)composer fund — 列出可捐赠的包
RunAuditRunAudit() (string, error)composer audit — 查找已知安全漏洞
go
fundOutput, _ := comp.FundPackages()
auditOutput, _ := comp.RunAudit()

更强的审计能力

RunAudit 返回原始文本。想要结构化漏洞列表、按严重级别筛选,请用 安全审计 模块的 GetAuditInfo()HasVulnerabilities()GetHighSeverityVulnerabilities()


🧭 下一步

  • 🔍 包操作RequirePackage / Remove / Show / Search / Outdated
  • 🛠️ 核心运行Run / RunWithContext / SetWorkingDir
  • 🔒 安全审计 — 结构化漏洞扫描与废弃包检测

基于 MIT 许可证发布