⚡ Update
Updates the project's dependencies to the latest versions allowed by the composer.json constraints. You can specify particular packages or update all dependencies.
When to use
Use this when you want to obtain new dependency versions, fix bugs, or upgrade packages. Note that update rewrites composer.lock, unlike install which restores from the lock. Equivalent to running composer update [--no-dev] [packages...].
Signature
go
func (c *Composer) Update(packages []string, noDev bool) errorParameters
| Parameter | Type | Description |
|---|---|---|
packages | []string | List of package names to update; pass an empty slice []string{} to update all packages |
noDev | bool | When true, development dependencies are not updated |
Return value
error: returns the corresponding error message when an error occurs during the update; nil on success.
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.Fatalf("init failed: %v", err)
}
// Update all dependencies (including development dependencies)
if err := comp.Update([]string{}, false); err != nil {
log.Fatalf("update dependencies failed: %v", err)
}
// Update only the specified packages
if err := comp.Update([]string{"symfony/console", "symfony/process"}, false); err != nil {
log.Fatalf("update specified packages failed: %v", err)
}
fmt.Println("dependencies update complete")
}Advanced
- When you need to pass options such as
--prefer-distor--with-dependencies, useUpdateWithOptions. - When you only want to simulate the update and preview the changes, use
UpdateDryRun. - To update dependency packages as well, see
UpdateWithDependencies. - To only refresh the lock hash without upgrading package versions, see
UpdateWithLock.