Skip to content

📦 Dependencies

This page covers methods in pkg/composer related to project dependency installation, updating, and autoloading, defined in dependencies.go and additional_methods.go. They correspond to composer's install, update, dump-autoload, suggests, fund, audit, and other subcommands.

🎯 When to Use

  • 📥 First dependency install after cloning a project → Install
  • 🔄 Upgrade dependencies to latest versions → Update
  • ⚡ Regenerate autoloader (after adding new classes) → DumpAutoload
  • 🧪 Want to dry-run an install/update without actual changes → InstallDryRun / UpdateDryRun
  • 🏭 Production deployment needs extreme optimization → InstallWithClassmapAuthoritative / InstallWithAPCu
  • 🔒 Only fix composer.lock hash without touching package versions → UpdateWithLock

📋 Method Overview

The table below lists all dependency management related methods. Variants with WithOptions accept map[string]string to customize composer long options; variants with DryRun only simulate without actual execution.

MethodSignature SummaryEquivalent Command
📥 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) errorExecute install in specified directory
🧪 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

Options Map Convention

For map[string]string received by *WithOptions, keys are composer long option names (without --), and values that are empty strings "" are treated as pure flag options (only --key is added), otherwise --key=value is generated. For example, {"no-dev": "", "prefer-dist": ""} becomes --no-dev --prefer-dist.


📥 Install

Install all project dependencies based on composer.json. This is the most commonly used method.

Signature

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

Parameters

ParameterTypeDescription
noDevbooltrue adds --no-dev, not installing dev dependencies from require-dev
optimizebooltrue adds --optimize-autoloader, optimizing the autoloader

Return Values

TypeDescription
errorOn failure, returns error wrapping ErrInstallFailed

Example

go
// Install all dependencies (including dev dependencies)
err := comp.Install(false, false)
if err != nil {
	log.Fatalf("Failed to install dependencies: %v", err)
}

// Install only production dependencies and optimize autoloader (common for production deployment)
err = comp.Install(true, true)

Advanced

  • Use InstallWithOptions when more options are needed.
  • Use InstallDryRun to dry-run without actually installing.

⚙️ InstallWithOptions

Install dependencies with custom options, supporting arbitrary combinations of composer long options.

Signature

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

Parameters

ParameterTypeDescription
optionsmap[string]stringInstall options map; keys are option names, values are option values (empty string for flag)

Example

go
options := map[string]string{
	"no-dev":            "",
	"optimize-autoloader": "",
	"prefer-dist":       "",
	"no-progress":       "",
}
err := comp.InstallWithOptions(options)
if err != nil {
	log.Fatalf("Failed to install dependencies: %v", err)
}

🔄 Update

Update project dependencies to latest versions; can specify package names or update all.

Signature

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

Parameters

ParameterTypeDescription
packages[]stringList of package names to update; empty slice updates all packages
noDevbooltrue adds --no-dev

Example

go
// Update all dependencies (including dev dependencies)
err := comp.Update([]string{}, false)

// Update only specified packages
err = comp.Update([]string{"symfony/console", "symfony/process"}, false)

// Update only production dependencies
err = comp.Update([]string{}, true)

Advanced

  • To update transitive dependencies as well, use UpdateWithDependencies or UpdateWithAllDependencies.
  • To only refresh lock hash, use UpdateWithLock.

⚙️ UpdateWithOptions

Update dependencies with custom options.

Signature

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

Parameters

ParameterTypeDescription
packages[]stringList of package names to update
optionsmap[string]stringUpdate options map

Example

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

DumpAutoload

Generate Composer's autoload files, optionally optimized.

Signature

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

Parameters

ParameterTypeDescription
optimizebooltrue adds --optimize, generates class map (recommended for production)

Example

go
// Generate standard autoload files (development environment)
err := comp.DumpAutoload(false)

// Generate optimized autoload files (production deployment)
err = comp.DumpAutoload(true)

Advanced

  • Use DumpAutoloadWithOptions when more options are needed (like --classmap-authoritative, --apcu, --no-dev).

⚙️ DumpAutoloadWithOptions

Generate autoload files with custom options.

Signature

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

Example

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

🔒 UpdateWithLock

Only update the hash value of the composer.lock file, without actually updating any package versions. Used to fix when composer.lock hash is out of sync with composer.json.

Signature

go
func (c *Composer) UpdateWithLock() error

Example

go
err := comp.UpdateWithLock()
if err != nil {
	log.Fatalf("Failed to update lock file: %v", err)
}

🧪 InstallDryRun / UpdateDryRun

Simulate install/update execution without actually modifying the filesystem, commonly used for CI dry-runs or checking which packages would change.

Signatures

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

Parameters (UpdateDryRun)

ParameterTypeDescription
packages[]stringList of package names to simulate updating; empty simulates updating all packages

Return Values

TypeDescription
stringSimulated execution output
errorExecution error

Example

go
output, err := comp.InstallDryRun()
if err != nil {
	log.Fatalf("Dry-run install failed: %v", err)
}
fmt.Println(output)

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

🏭 Single-option Shortcut Methods

Each of the following methods appends a single fixed option, with simple signatures and clear semantics:

MethodEquivalent CommandDescription
InstallWithPreferSource() errorinstall --prefer-sourceForce install from source (Git), convenient for debugging/source editing
InstallWithPreferDist() errorinstall --prefer-distForce install from distribution package (zip), fast, suitable for production
InstallNoScripts() errorinstall --no-scriptsSkip script execution, common in CI/CD
InstallWithClassmapAuthoritative() errorinstall --classmap-authoritativeAuthoritative class map, load only from class map, boosts production performance
InstallWithAPCu() errorinstall --apcu-autoloaderEnable APCu cache for autoloading, requires PHP APCu extension
InstallNoDev() errorinstall --no-devDon't install dev dependencies
UpdateWithPreferSource(packages []string) errorupdate --prefer-source [packages...]Update from source
UpdateWithPreferDist(packages []string) errorupdate --prefer-dist [packages...]Update from distribution package
UpdateNoScripts(packages []string) errorupdate --no-scripts [packages...]Update skipping scripts
UpdateNoDev(packages []string) errorupdate --no-dev [packages...]Don't update dev dependencies
UpdateWithDependencies(packages []string) errorupdate --with-dependencies [packages...]Update with dependencies
UpdateWithAllDependencies(packages []string) errorupdate --with-all-dependencies [packages...]Recursively update all dependencies

Example

go
// Production deployment: dist package + no scripts + authoritative class map
_ = comp.InstallWithPreferDist()
_ = comp.InstallNoScripts()
_ = comp.InstallWithClassmapAuthoritative()

// Update specified packages with dependencies
_ = comp.UpdateWithDependencies([]string{"symfony/console"})
_ = comp.UpdateWithAllDependencies([]string{"symfony/console"})

APCu Prerequisite

InstallWithAPCu requires the PHP APCu extension to be installed and enabled, otherwise composer will error.


📁 InstallWithWorkingDir

Execute install in a specified working directory, restoring the original working directory after completion. Suitable for one-off operations on other projects without affecting the instance's default directory.

Signature

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

Parameters

ParameterTypeDescription
workingDirstringWorking directory path
noDevboolWhether to skip dev dependencies
optimizeboolWhether to optimize autoloader

Example

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

Advanced

  • Implementation temporarily modifies c.workingDir and uses defer to restore the original value; it restores even if a panic occurs during execution.
  • For a more general "run any command in another directory", use ExecWithWorkingDir (see exec).

🔍 CheckDependencies

Check whether composer.json and composer.lock are in sync and whether dependencies have conflicts.

Signature

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

Example

go
output, err := comp.CheckDependencies()
if err != nil {
	log.Fatalf("Dependency check failed: %v", err)
}
fmt.Println("Dependency check result:", output)

💡 Suggests

View and install suggested packages (composer suggests).

Signature

go
func (c *Composer) Suggests() error

Advanced

  • For options, use SuggestsWithOptions(options map[string]string) (string, error).
  • To query suggestions for a specific package, use SuggestsForPackage(packageName string) (string, error).

💰 FundPackages / 🔒 RunAudit

MethodSignatureEquivalent Command
FundPackagesFundPackages() (string, error)composer fund — list packages accepting donations
RunAuditRunAudit() (string, error)composer audit — find known security vulnerabilities
go
fundOutput, _ := comp.FundPackages()
auditOutput, _ := comp.RunAudit()

Stronger Audit Capabilities

RunAudit returns raw text. For structured vulnerability lists and severity filtering, use GetAuditInfo(), HasVulnerabilities(), GetHighSeverityVulnerabilities() in the Security Audit module.


🧭 Next Steps

  • 🔍 PackagesRequirePackage / Remove / Show / Search / Outdated
  • 🛠️ Core RuntimeRun / RunWithContext / SetWorkingDir
  • 🔒 Security Audit — Structured vulnerability scanning and abandoned package detection

Released under the MIT License