🔍 Detect
Detects the Composer executable on the system and returns its full path.
When to Use
Use this when you need the actual path of the Composer executable (for direct invocation, logging, passing to composer.New's ExecutablePath, etc.). The method searches level by level in the order "environment variable → default paths → which/where → composer.phar". 📦
Signature
go
func (d *Detector) Detect() (string, error)Parameters
| Parameter | Type | Description |
|---|---|---|
| None | — | This method takes no parameters; the set of candidate paths comes from the Detector's internal state |
Return Value
string: The full path of the detected Composer executable.error: ReturnsErrExecutableNotFound("未找到composer可执行文件") when not found. ⚠️
Example
go
package main
import (
"fmt"
"log"
"github.com/scagogogo/composer-skills/pkg/detector"
)
func main() {
d := detector.NewDetector()
path, err := d.Detect()
if err != nil {
log.Fatalf("Composer not detected: %v", err)
}
fmt.Printf("Composer path: %s\n", path)
}Advanced
- To learn which method (environment variable / default path / which) the detected path was hit by, use
DetectVerbose. - If you only need to know whether it is installed without needing the path,
IsInstalledis lighter. ✅ - You can force a path via the
COMPOSER_PATHenvironment variable, which takes priority and is matched first. 🎯