🧩 DetectVerbose
检测 Composer 可执行文件并返回详细的命中信息(路径、命中方式、是否为 phar)。
何时使用
当你需要知道 Composer 是如何被发现的——例如排查 COMPOSER_PATH 是否生效、是默认路径命中还是 which 命中、检测到的是不是 .phar 文件——时使用。🔍
签名
go
func (d *Detector) DetectVerbose() (*DetectionResult, error)参数
| 参数 | 类型 | 说明 |
|---|---|---|
| 无 | — | 该方法无参数 |
返回值
*DetectionResult:包含Path(路径)、Method(命中方式)、IsPhar(是否 phar)的结构体。✅error:未找到时返回ErrExecutableNotFound。⚠️
Method 可能的取值:env:COMPOSER_PATH、env:COMPOSER_HOME/vendor/bin、default_path、which/where。
示例
go
package main
import (
"fmt"
"log"
"github.com/scagogogo/composer-skills/pkg/detector"
)
func main() {
d := detector.NewDetector()
result, err := d.DetectVerbose()
if err != nil {
log.Fatalf("未检测到 Composer: %v", err)
}
fmt.Printf("路径: %s\n", result.Path)
fmt.Printf("命中方式: %s\n", result.Method)
fmt.Printf("是否 phar: %v\n", result.IsPhar)
}进阶
- 与
Detect的区别:后者只返回路径字符串,本方法额外返回命中方式和 phar 标记。🚀 IsPhar在判断后续是否需要php composer.phar形式调用时很有用。⚡- 本方法不会检测
composer.phar兜底文件(Detect会),因此兜底场景下仍可能返回错误。