📦 BatchRequire
Batch adds multiple Composer dependency packages, with support for continue-on-error and a development-dependency flag.
When to use
Use when initializing a project, migrating a dependency manifest, or script-installing a group of related packages, and you want to complete it in a single call and get per-package success/failure statistics.
Signature
go
func (c *Composer) BatchRequire(packages map[string]string, dev bool, continueOnError bool) (*BatchRequireResult, error)Parameters
| Parameter | Type | Description |
|---|---|---|
packages | map[string]string | Mapping of package name to version constraint, e.g. {"symfony/console": "^5.4"} |
dev | bool | Whether to add as require-dev (development dependencies) |
continueOnError | bool | Whether to continue adding subsequent packages on error; when false, stops at the first error |
Return value
*BatchRequireResult: Batch add result, including per-package results and countserror: The first error encountered (only returned whencontinueOnErrorisfalse; otherwisenil)
go
type BatchRequireResult struct {
Results []RequireResult `json:"results,omitempty"`
SuccessCount int `json:"success_count"`
FailCount int `json:"fail_count"`
TotalCount int `json:"total_count"`
}Example
go
package main
import (
"fmt"
"log"
"github.com/scagogogo/composer-skills/pkg/composer"
)
func main() {
comp, err := composer.New(composer.DefaultOptions())
if err != nil {
log.Fatal(err)
}
packages := map[string]string{
"symfony/console": "^5.4",
"monolog/monolog": "^2.0",
"psr/log": "^1.1",
}
result, err := comp.BatchRequire(packages, false, true)
if err != nil {
log.Printf("some additions failed: %v", err)
}
fmt.Printf("Success: %d, Failed: %d, Total: %d\n",
result.SuccessCount, result.FailCount, result.TotalCount)
}Advanced
- This method calls
RequirePackagefor each package; failure information is written to the correspondingRequireResult.Warnings - For atomicity (all or rollback), set
continueOnError=falseand handleerroryourself - The corresponding removal operation is BatchRemove
- For single-package addition, use
RequirePackageorRequirePackageWithOptions(see packages.go)