💻 Platform
Check whether the system meets platform requirements such as PHP version and extensions, and retrieve runtime environment information.
Composer's platform concept refers to PHP itself and its extensions (ext-xxx), PHP libraries (lib-xxx), and other runtime dependencies. Items declared in composer.json's require as php, ext-*, lib-* are platform requirements. Composer Skills provides two categories of methods: requirement checks based on check-platform, and convenience queries that directly read PHP runtime information.
When to Use
- 💻 Pre-deployment checks: whether the target machine meets the PHP version and extensions declared in
composer.json. - 🧩 Pre-install validation for dependencies: before
composer install, determine whether key extensions are missing and give a readable error instead of letting Composer fail mid-run. - 🚀 CI matrix construction: dynamically decide the test matrix based on
GetPHPVersion/GetExtensions. - 🔍 Health checks: as one of the inputs to
HealthCheck.
Structured Return Types
PlatformInfo
A single platform requirement entry returned by CheckPlatform / CheckPlatformWithLock.
type PlatformInfo struct {
Name string `json:"name"`
Version string `json:"version"`
Available bool `json:"available"`
Required string `json:"required,omitempty"`
}| Field | Type | Description |
|---|---|---|
Name | string | Platform item name, e.g., php, ext-mbstring |
Version | string | Actual version on the current system |
Available | bool | Whether it is satisfied/available |
Required | string | Declared version constraint (may be empty) |
PlatformRequirements
The intermediate structure parsed internally by CheckPlatform / CheckPlatformWithLock.
type PlatformRequirements struct {
Platform map[string]PlatformInfo `json:"platform"`
Lock map[string]PlatformInfo `json:"lock,omitempty"`
}| Field | Type | Description |
|---|---|---|
Platform | map[string]PlatformInfo | Platform requirements declared in composer.json |
Lock | map[string]PlatformInfo | Platform requirements locked in composer.lock (may be empty) |
Two Sources
CheckPlatform reads the Platform field (requirements from composer.json); CheckPlatformWithLock reads the Lock field (requirements locked in the lock file). Both are based on composer check-platform --format=json, differing only by the --lock flag.
PlatformRequirement / PlatformCheckResult
Returned by CheckPlatformReqsStructured, corresponds to composer check-platform-reqs --format=json.
type PlatformRequirement struct {
Package string `json:"package"`
Version string `json:"version,omitempty"`
Status string `json:"status"` // "ok", "missing", "mismatch"
Required string `json:"required,omitempty"`
}
type PlatformCheckResult struct {
Requirements []PlatformRequirement `json:"requirements"`
OK bool `json:"ok"`
}| Field | Type | Description |
|---|---|---|
Package | string | Platform package name |
Version | string | Actual version |
Status | string | Status: ok/success, missing, mismatch |
Required | string | Required version constraint |
Requirements | []PlatformRequirement | All requirement entries |
OK | bool | Whether all pass (any entry that is not ok/success makes it false) |
Difference between check-platform and check-platform-reqs
check-platform(CheckPlatformfamily): checks whether the platform requirements declared incomposer.json/composer.lockare satisfied.check-platform-reqs(CheckPlatformReqsStructured): checks the platform requirements Composer actually needs after resolution, closer to what Composer itself validates at install time, with clearer status fields (missing/mismatch).
CheckPlatform
💻 Check whether the current system meets the platform requirements defined in composer.json.
Signature
func (c *Composer) CheckPlatform() ([]PlatformInfo, error)Parameters
None.
Return Values
| Value | Type | Description |
|---|---|---|
| Platform requirements | []PlatformInfo | List of platform requirements declared in composer.json |
| Error | error | Returned when execution or JSON parsing fails |
Equivalent Command
composer check-platform --format=json
Example
platforms, err := comp.CheckPlatform()
if err != nil {
log.Fatalf("Failed to check platform requirements: %v", err)
}
for _, platform := range platforms {
status := "not satisfied"
if platform.Available {
status = "satisfied"
}
fmt.Printf("%s %s: %s\n", platform.Name, platform.Version, status)
}CheckPlatformWithLock
💻 Check whether the current system meets the platform requirements locked in composer.lock.
Signature
func (c *Composer) CheckPlatformWithLock() ([]PlatformInfo, error)Parameters
None.
Return Values
| Value | Type | Description |
|---|---|---|
| Platform requirements | []PlatformInfo | List of platform requirements locked in composer.lock |
| Error | error | Returned when execution or JSON parsing fails |
Equivalent Command
composer check-platform --lock --format=json
Example
platforms, err := comp.CheckPlatformWithLock()
if err != nil {
log.Fatalf("Failed to check lock file platform requirements: %v", err)
}
for _, platform := range platforms {
if !platform.Available {
fmt.Printf("Warning: %s %s requirement not satisfied\n", platform.Name, platform.Required)
}
}Which to Choose
When deploying to production, the versions actually installed are the ones locked in the lock file, so CheckPlatformWithLock is closer to reality. Use CheckPlatform during development or when no lock file has been generated yet.
IsPlatformAvailable
💻 Check whether the specified platform requirement is satisfied.
Signature
func (c *Composer) IsPlatformAvailable(platform string, version string) (bool, error)Parameters
| Parameter | Type | Description |
|---|---|---|
platform | string | Platform name, e.g., php or ext-mbstring |
version | string | Version constraint, e.g., >=7.4; pass an empty string to skip version constraint |
Return Values
| Value | Type | Description |
|---|---|---|
| Satisfied | bool | true means the platform is available/satisfied |
| Error | error | Returned when the check fails |
Implementation Notes
- First calls
CheckPlatformto look for an entry with the same name among declared requirements; if found, returns itsAvailable. - If not listed in the requirements, constructs
platform[:version]and directly executescomposer check-platform <item>, then decides based on whether the output containsis not available.
Example
// Check PHP version
available, err := comp.IsPlatformAvailable("php", ">=7.4")
if err != nil {
log.Fatalf("Failed to check PHP version: %v", err)
}
if available {
fmt.Println("PHP version satisfies the requirement")
} else {
fmt.Println("PHP version does not satisfy the requirement")
}
// Check extension
available, err = comp.IsPlatformAvailable("ext-mbstring", "")
if err != nil {
log.Fatalf("Failed to check extension: %v", err)
}
if available {
fmt.Println("mbstring extension is installed")
} else {
fmt.Println("mbstring extension is not installed")
}GetPHPVersion
💻 Get the PHP version number used on the current system.
Signature
func (c *Composer) GetPHPVersion() (string, error)Parameters
None.
Return Values
| Value | Type | Description |
|---|---|---|
| PHP version | string | Current PHP version number; returns an empty string if it cannot be parsed from the output |
| Error | error | Returned when execution fails |
Equivalent Command
composer run --php-show-version
Implementation Notes
Executes composer run --php-show-version, scans line by line for a line starting with PHP , and takes the second space-separated field as the version number.
Example
phpVersion, err := comp.GetPHPVersion()
if err != nil {
log.Fatalf("Failed to get PHP version: %v", err)
}
fmt.Printf("Current PHP version: %s\n", phpVersion)GetExtensions
💻 Get the list of installed extensions in the current PHP environment.
Signature
func (c *Composer) GetExtensions() ([]string, error)Parameters
None.
Return Values
| Value | Type | Description |
|---|---|---|
| Extension list | []string | List of installed extension names; empty slice if none |
| Error | error | Returned when execution fails |
Equivalent Command
composer run --show-extensions
Implementation Notes
Executes composer run --show-extensions, parses the output line by line, and skips blank lines and the Loaded extensions: title line.
Example
extensions, err := comp.GetExtensions()
if err != nil {
log.Fatalf("Failed to get PHP extensions: %v", err)
}
fmt.Println("Installed PHP extensions:")
for _, ext := range extensions {
fmt.Println("- " + ext)
}HasExtension
💻 Check whether the specified PHP extension is installed.
Signature
func (c *Composer) HasExtension(extension string) (bool, error)Parameters
| Parameter | Type | Description |
|---|---|---|
extension | string | Extension name to check, e.g., mbstring or pdo |
Return Values
| Value | Type | Description |
|---|---|---|
| Installed | bool | true means installed |
| Error | error | Returned when the check fails |
Implementation Notes
Internally calls GetExtensions and matches the extension name exactly in the result.
Example
hasJson, err := comp.HasExtension("json")
if err != nil {
log.Fatalf("Failed to check extension: %v", err)
}
if hasJson {
fmt.Println("JSON extension is installed")
} else {
fmt.Println("JSON extension is not installed")
}Advanced
CheckPlatformReqsStructured
Structured check-platform-reqs, returns *PlatformCheckResult, with clearer status fields than CheckPlatform.
func (c *Composer) CheckPlatformReqsStructured() (*PlatformCheckResult, error)| Value | Type | Description |
|---|---|---|
| Result | *PlatformCheckResult | Contains the Requirements list and the overall OK |
| Error | error | Returned when execution or parsing fails |
result, err := comp.CheckPlatformReqsStructured()
if err != nil {
log.Fatalf("Platform requirement check failed: %v", err)
}
if !result.OK {
for _, req := range result.Requirements {
if req.Status != "ok" && req.Status != "success" {
fmt.Printf("Not satisfied: %s (requires %s, actual %s, status %s)\n",
req.Package, req.Required, req.Version, req.Status)
}
}
}ParsePlatformCheckResult
A pure function that parses any composer check-platform-reqs --format=json output into *PlatformCheckResult.
func ParsePlatformCheckResult(output string) (*PlatformCheckResult, error)ParseCheckPlatformReqsOutput
A parsing function defined in parsing.go that parses text output into []PlatformRequirement, suitable for non-JSON scenarios.
func ParseCheckPlatformReqsOutput(output string) ([]PlatformRequirement, error)CheckPlatformReqsWithFormat / CheckPlatformReqs / CheckPlatformReqsLock
| Method | Equivalent Command | Description |
|---|---|---|
CheckPlatformReqs() (string, error) | check-platform-reqs | Text output (defined in config.go) |
CheckPlatformReqsLock() (string, error) | check-platform-reqs --lock | Check lock file platform requirements |
CheckPlatformReqsWithFormat(format string) (string, error) | check-platform-reqs --format=FORMAT | Specify output format (defined in additional_methods.go) |
🔍 Related Methods
- Validate:
ValidateSchemaetc. trigger platform-related checks;Prohibitshows packages prohibited by platform requirements. - Diagnosis & Health Check:
HealthCheckinternally callsinstaller.HasPHP()/installer.GetPHPVersion()to check the PHP environment, writing results toPHPAvailable/PHPVersion.