🌡️ Environment
Control behavior via Composer's environment variables (COMPOSER_*): timeout, memory, superuser, interaction, vendor/bin directory, CA certificates, dev dependencies, change discard, etc., and query Composer's actual install path and environment config listing.
These are all process-level os.Setenv operations affecting all subsequent composer subprocesses. Except for GetComposerPath / GetEnvironmentInfo, the rest are package-level functions (no *Composer instance needed).
When to Use
- 🐳 Docker / CI containers running as root, need
EnableSuperuserto run Composer. - ⏱️ Long tasks (large monorepo
composer update) where the default 300-second timeout is insufficient, useSetProcessTimeoutto increase. - 💾 PHP memory overflow during large dependency resolution, use
SetMemoryLimitto set to-1. - 🤖 CI must be non-interactive, use
DisableInteractionto avoid hanging on prompts. - 🔒 Intranet using self-signed certificates, use
SetCaFileto specify trusted CA bundle. - 📦 Redirect vendor directory to a container volume for faster cache reuse.
Structured Types
EnvironmentVariable
Predefined environment variable constants in environment.go, used as input for SetEnvVariable / GetEnvVariable.
type EnvironmentVariable string
const (
EnvComposerHome EnvironmentVariable = "COMPOSER_HOME"
EnvComposerCacheDir EnvironmentVariable = "COMPOSER_CACHE_DIR"
EnvComposerProcessTimeout EnvironmentVariable = "COMPOSER_PROCESS_TIMEOUT"
EnvComposerAllowSuperuser EnvironmentVariable = "COMPOSER_ALLOW_SUPERUSER"
EnvComposerMemoryLimit EnvironmentVariable = "COMPOSER_MEMORY_LIMIT"
EnvComposerDisableXdebugWarn EnvironmentVariable = "COMPOSER_DISABLE_XDEBUG_WARN"
EnvComposerNoInteraction EnvironmentVariable = "COMPOSER_NO_INTERACTION"
EnvComposerVendorDir EnvironmentVariable = "COMPOSER_VENDOR_DIR"
EnvComposerBinDir EnvironmentVariable = "COMPOSER_BIN_DIR"
EnvComposerCafile EnvironmentVariable = "COMPOSER_CAFILE"
EnvComposerNoDev EnvironmentVariable = "COMPOSER_NO_DEV"
EnvComposerDiscardChanges EnvironmentVariable = "COMPOSER_DISCARD_CHANGES"
EnvComposerHtaccessProtect EnvironmentVariable = "COMPOSER_HTACCESS_PROTECT"
EnvComposerMirrorPathRepos EnvironmentVariable = "COMPOSER_MIRROR_PATH_REPOS"
)Method Signatures
| Method | Signature | Description |
|---|---|---|
| 🌡️ SetEnvVariable | func SetEnvVariable(name EnvironmentVariable, value string) error | Set any COMPOSER_* variable |
| 📖 GetEnvVariable | func GetEnvVariable(name EnvironmentVariable) string | Read COMPOSER_* variable (empty string if none) |
| ⏱️ SetProcessTimeout | func SetProcessTimeout(seconds int) error | Set process timeout in seconds |
| 🐙 EnableSuperuser | func EnableSuperuser() error | Allow root execution |
| 🚫 DisableSuperuser | func DisableSuperuser() error | Disallow root execution |
| 💾 SetMemoryLimit | func SetMemoryLimit(limit string) error | Set PHP memory limit |
| 🤖 DisableInteraction | func DisableInteraction() error | Disable interactive prompts |
| 💬 EnableInteraction | func EnableInteraction() error | Enable interactive prompts |
| 📦 SetVendorDir | func SetVendorDir(path string) error | Redirect vendor directory |
| 🗂️ SetBinDir | func SetBinDir(path string) error | Redirect bin directory |
| 🔒 SetCaFile | func SetCaFile(path string) error | Specify CA certificate file |
| 🚫 DisableDev | func DisableDev() error | Don't install dev dependencies |
| ✅ EnableDev | func EnableDev() error | Restore dev dependencies |
| 🗑️ SetDiscardChanges | func SetDiscardChanges(value string) error | Control change handling (true/false/stash) |
| 🔍 GetComposerPath | func GetComposerPath() (string, error) | Find composer / composer.phar path |
| 📋 GetEnvironmentInfo | func (c *Composer) GetEnvironmentInfo() (map[string]string, error) | Parse composer config --list into map |
Parameters
SetMemoryLimit
| Parameter | Type | Description |
|---|---|---|
limit | string | Memory limit, e.g., 512M, 2G, -1 (unlimited) |
SetDiscardChanges
| Parameter | Type | Description |
|---|---|---|
value | string | true=discard changes, false=ask, stash=stash |
Examples
One-time CI Container Environment Configuration
package main
import (
"fmt"
"log"
"github.com/scagogogo/composer-skills/pkg/composer"
)
func main() {
// Classic CI combo
_ = composer.EnableSuperuser() // Run as root in container
_ = composer.DisableInteraction() // Non-interactive
_ = composer.SetProcessTimeout(0) // No timeout (0 = unlimited)
_ = composer.SetMemoryLimit("-1") // PHP memory unlimited
_ = composer.DisableDev() // Don't install require-dev
comp, err := composer.New(composer.DefaultOptions())
if err != nil {
log.Fatal(err)
}
info, err := comp.GetEnvironmentInfo()
if err != nil {
log.Fatal(err)
}
fmt.Printf("home = %s\n", info["home"])
fmt.Printf("bin-dir = %s\n", info["bin-dir"])
}Intranet Self-signed Certificate
if err := composer.SetCaFile("/etc/ssl/certs/internal-ca.pem"); err != nil {
log.Fatal(err)
}
// Composer can then pull intranet https repositoriesRedirect vendor Directory to Cache Volume
_ = composer.SetVendorDir("/cache/vendor")
_ = composer.SetBinDir("/cache/vendor/bin")Generic SetEnvVariable / GetEnvVariable
// Set cache directory to fast SSD
_ = composer.SetEnvVariable(composer.EnvComposerCacheDir, "/mnt/ssd/composer-cache")
// Read back to confirm
fmt.Println("cache-dir =", composer.GetEnvVariable(composer.EnvComposerCacheDir))Find Composer Executable
path, err := composer.GetComposerPath()
if err != nil {
log.Fatalf("Composer not found: %v", err)
}
fmt.Println("composer path:", path)Advanced
Process-level Side Effects
All Set* functions call os.Setenv, affecting the entire process and subsequently spawned composer subprocesses, and cannot be rolled back to pre-call values. Disable* / Enable* come in pairs so you can toggle state, but they only restore "default values", not "original pre-call values".
GetEnvironmentInfo Parsing
GetEnvironmentInfo executes composer config --list, splitting each line by : into key/value and writing to map. Note that different Composer versions may use or : as separator; this method uses SplitN(line, ":", 2), effective for key: value format.
GetComposerPath Search Order
First exec.LookPath("composer"), then looks for composer.phar if not found. Returns error if neither exists; in that case, use the Installer to auto-install Composer.