Skip to content

🌡️ 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 EnableSuperuser to run Composer.
  • ⏱️ Long tasks (large monorepo composer update) where the default 300-second timeout is insufficient, use SetProcessTimeout to increase.
  • 💾 PHP memory overflow during large dependency resolution, use SetMemoryLimit to set to -1.
  • 🤖 CI must be non-interactive, use DisableInteraction to avoid hanging on prompts.
  • 🔒 Intranet using self-signed certificates, use SetCaFile to 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.

go
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

MethodSignatureDescription
🌡️ SetEnvVariablefunc SetEnvVariable(name EnvironmentVariable, value string) errorSet any COMPOSER_* variable
📖 GetEnvVariablefunc GetEnvVariable(name EnvironmentVariable) stringRead COMPOSER_* variable (empty string if none)
⏱️ SetProcessTimeoutfunc SetProcessTimeout(seconds int) errorSet process timeout in seconds
🐙 EnableSuperuserfunc EnableSuperuser() errorAllow root execution
🚫 DisableSuperuserfunc DisableSuperuser() errorDisallow root execution
💾 SetMemoryLimitfunc SetMemoryLimit(limit string) errorSet PHP memory limit
🤖 DisableInteractionfunc DisableInteraction() errorDisable interactive prompts
💬 EnableInteractionfunc EnableInteraction() errorEnable interactive prompts
📦 SetVendorDirfunc SetVendorDir(path string) errorRedirect vendor directory
🗂️ SetBinDirfunc SetBinDir(path string) errorRedirect bin directory
🔒 SetCaFilefunc SetCaFile(path string) errorSpecify CA certificate file
🚫 DisableDevfunc DisableDev() errorDon't install dev dependencies
✅ EnableDevfunc EnableDev() errorRestore dev dependencies
🗑️ SetDiscardChangesfunc SetDiscardChanges(value string) errorControl change handling (true/false/stash)
🔍 GetComposerPathfunc GetComposerPath() (string, error)Find composer / composer.phar path
📋 GetEnvironmentInfofunc (c *Composer) GetEnvironmentInfo() (map[string]string, error)Parse composer config --list into map

Parameters

SetMemoryLimit

ParameterTypeDescription
limitstringMemory limit, e.g., 512M, 2G, -1 (unlimited)

SetDiscardChanges

ParameterTypeDescription
valuestringtrue=discard changes, false=ask, stash=stash

Examples

One-time CI Container Environment Configuration

go
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

go
if err := composer.SetCaFile("/etc/ssl/certs/internal-ca.pem"); err != nil {
	log.Fatal(err)
}
// Composer can then pull intranet https repositories

Redirect vendor Directory to Cache Volume

go
_ = composer.SetVendorDir("/cache/vendor")
_ = composer.SetBinDir("/cache/vendor/bin")

Generic SetEnvVariable / GetEnvVariable

go
// 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

go
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.

Released under the MIT License