Skip to content

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

go
type PlatformInfo struct {
    Name      string `json:"name"`
    Version   string `json:"version"`
    Available bool   `json:"available"`
    Required  string `json:"required,omitempty"`
}
FieldTypeDescription
NamestringPlatform item name, e.g., php, ext-mbstring
VersionstringActual version on the current system
AvailableboolWhether it is satisfied/available
RequiredstringDeclared version constraint (may be empty)

PlatformRequirements

The intermediate structure parsed internally by CheckPlatform / CheckPlatformWithLock.

go
type PlatformRequirements struct {
    Platform map[string]PlatformInfo `json:"platform"`
    Lock     map[string]PlatformInfo `json:"lock,omitempty"`
}
FieldTypeDescription
Platformmap[string]PlatformInfoPlatform requirements declared in composer.json
Lockmap[string]PlatformInfoPlatform 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.

go
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"`
}
FieldTypeDescription
PackagestringPlatform package name
VersionstringActual version
StatusstringStatus: ok/success, missing, mismatch
RequiredstringRequired version constraint
Requirements[]PlatformRequirementAll requirement entries
OKboolWhether all pass (any entry that is not ok/success makes it false)

Difference between check-platform and check-platform-reqs

  • check-platform (CheckPlatform family): checks whether the platform requirements declared in composer.json/composer.lock are 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

go
func (c *Composer) CheckPlatform() ([]PlatformInfo, error)

Parameters

None.

Return Values

ValueTypeDescription
Platform requirements[]PlatformInfoList of platform requirements declared in composer.json
ErrorerrorReturned when execution or JSON parsing fails

Equivalent Command

composer check-platform --format=json

Example

go
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

go
func (c *Composer) CheckPlatformWithLock() ([]PlatformInfo, error)

Parameters

None.

Return Values

ValueTypeDescription
Platform requirements[]PlatformInfoList of platform requirements locked in composer.lock
ErrorerrorReturned when execution or JSON parsing fails

Equivalent Command

composer check-platform --lock --format=json

Example

go
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

go
func (c *Composer) IsPlatformAvailable(platform string, version string) (bool, error)

Parameters

ParameterTypeDescription
platformstringPlatform name, e.g., php or ext-mbstring
versionstringVersion constraint, e.g., >=7.4; pass an empty string to skip version constraint

Return Values

ValueTypeDescription
Satisfiedbooltrue means the platform is available/satisfied
ErrorerrorReturned when the check fails

Implementation Notes

  1. First calls CheckPlatform to look for an entry with the same name among declared requirements; if found, returns its Available.
  2. If not listed in the requirements, constructs platform[:version] and directly executes composer check-platform <item>, then decides based on whether the output contains is not available.

Example

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

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

Parameters

None.

Return Values

ValueTypeDescription
PHP versionstringCurrent PHP version number; returns an empty string if it cannot be parsed from the output
ErrorerrorReturned 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

go
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

go
func (c *Composer) GetExtensions() ([]string, error)

Parameters

None.

Return Values

ValueTypeDescription
Extension list[]stringList of installed extension names; empty slice if none
ErrorerrorReturned 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

go
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

go
func (c *Composer) HasExtension(extension string) (bool, error)

Parameters

ParameterTypeDescription
extensionstringExtension name to check, e.g., mbstring or pdo

Return Values

ValueTypeDescription
Installedbooltrue means installed
ErrorerrorReturned when the check fails

Implementation Notes

Internally calls GetExtensions and matches the extension name exactly in the result.

Example

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

go
func (c *Composer) CheckPlatformReqsStructured() (*PlatformCheckResult, error)
ValueTypeDescription
Result*PlatformCheckResultContains the Requirements list and the overall OK
ErrorerrorReturned when execution or parsing fails
go
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.

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

go
func ParseCheckPlatformReqsOutput(output string) ([]PlatformRequirement, error)

CheckPlatformReqsWithFormat / CheckPlatformReqs / CheckPlatformReqsLock

MethodEquivalent CommandDescription
CheckPlatformReqs() (string, error)check-platform-reqsText output (defined in config.go)
CheckPlatformReqsLock() (string, error)check-platform-reqs --lockCheck lock file platform requirements
CheckPlatformReqsWithFormat(format string) (string, error)check-platform-reqs --format=FORMATSpecify output format (defined in additional_methods.go)
  • Validate: ValidateSchema etc. trigger platform-related checks; Prohibit shows packages prohibited by platform requirements.
  • Diagnosis & Health Check: HealthCheck internally calls installer.HasPHP() / installer.GetPHPVersion() to check the PHP environment, writing results to PHPAvailable / PHPVersion.

Released under the MIT License