composer

package
v0.9.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package composer implements Composer build tool support for PHP projects within omnibump.

It handles parsing composer.lock files, performing dependency updates via the composer CLI, and analyzing project dependencies.

Parsing

Use ParseLock to parse a composer.lock file into a list of LockPackage values representing all installed packages (both regular and dev).

Updating

The Composer type implements the PHP BuildTool interface, providing detection, update, and validation operations for Composer-based projects. Updates are performed by modifying composer.json constraints and optionally running composer update.

Analysis

The Analyzer type implements the analyzer.Analyzer interface, providing dependency analysis and update strategy recommendations for Composer projects. All Composer updates use direct dependency updates (no property indirection).

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrLockNotFound = errors.New("composer.lock not found")

ErrLockNotFound indicates composer.lock was not found.

View Source
var ErrRemoteAnalysisNotImplemented = errors.New("remote analysis not implemented")

ErrRemoteAnalysisNotImplemented indicates remote analysis is not yet supported.

Functions

func DoUpdate

func DoUpdate(ctx context.Context, packages map[string]*Package, composerPackages []LockPackage, cfg *UpdateConfig) error

DoUpdate performs the actual update of Composer package dependencies.

func RunRequire

func RunRequire(ctx context.Context, name, version, composerRoot string, extraArgs ...string) (string, error)

RunRequire updates a specific package to a precise version. Uses: composer require <package>:<version> --no-interaction Additional args can be passed (e.g., "--no-install" for testing).

func RunRequireDev

func RunRequireDev(ctx context.Context, name, version, composerRoot string, extraArgs ...string) (string, error)

RunRequireDev updates a specific dev package to a precise version. Uses: composer require --dev <package>:<version> --no-interaction Additional args can be passed (e.g., "--no-install" for testing).

func RunUpdate

func RunUpdate(ctx context.Context, composerRoot string, extraArgs ...string) (string, error)

RunUpdate runs 'composer update' to refresh the composer.lock file. Additional args can be passed (e.g., "--no-install" for testing).

Types

type Analyzer

type Analyzer struct{}

Analyzer implements the analyzer.Analyzer interface for PHP Composer projects.

func (*Analyzer) Analyze

func (ca *Analyzer) Analyze(ctx context.Context, projectPath string) (*analyzer.AnalysisResult, error)

Analyze performs dependency analysis on a Composer project.

func (*Analyzer) AnalyzeRemote

func (ca *Analyzer) AnalyzeRemote(_ context.Context, _ map[string][]byte) (*analyzer.RemoteAnalysisResult, error)

AnalyzeRemote performs dependency analysis on remotely-fetched manifest files.

func (*Analyzer) RecommendStrategy

func (ca *Analyzer) RecommendStrategy(ctx context.Context, analysis *analyzer.AnalysisResult, deps []analyzer.Dependency) (*analyzer.Strategy, error)

RecommendStrategy suggests update strategy for Composer dependencies. For Composer, updates are always direct using composer require.

type Composer

type Composer struct{}

Composer implements the BuildTool interface for PHP Composer projects.

func (*Composer) Detect

func (c *Composer) Detect(_ context.Context, dir string) (bool, error)

Detect checks if Composer manifest files exist in the directory.

Example
package main

import (
	"context"
	"fmt"
	"os"
	"path/filepath"

	"github.com/chainguard-dev/omnibump/pkg/languages/php/composer"
)

func main() {
	dir, err := os.MkdirTemp("", "composer-example-*")
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	defer func() { _ = os.RemoveAll(dir) }()

	if err := os.WriteFile(filepath.Join(dir, "composer.lock"), []byte(`{"packages":[]}`), 0o644); err != nil {
		fmt.Println("error:", err)
		return
	}

	c := &composer.Composer{}
	detected, err := c.Detect(context.Background(), dir)
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	fmt.Println("Detected:", detected)
}
Output:
Detected: true

func (*Composer) GetAnalyzer

func (c *Composer) GetAnalyzer() analyzer.Analyzer

GetAnalyzer returns the Composer analyzer.

func (*Composer) GetManifestFiles

func (c *Composer) GetManifestFiles() []string

GetManifestFiles returns Composer manifest files.

Example
package main

import (
	"fmt"

	"github.com/chainguard-dev/omnibump/pkg/languages/php/composer"
)

func main() {
	c := &composer.Composer{}
	files := c.GetManifestFiles()
	for _, f := range files {
		fmt.Println(f)
	}
}
Output:
composer.json
composer.lock

func (*Composer) Name

func (c *Composer) Name() string

Name returns the build tool identifier.

func (*Composer) Update

func (c *Composer) Update(ctx context.Context, cfg *languages.UpdateConfig) error

Update performs dependency updates on a Composer project.

func (*Composer) Validate

func (c *Composer) Validate(ctx context.Context, cfg *languages.UpdateConfig) error

Validate checks if the updates were applied successfully.

type Lock

type Lock struct {
	Packages    []lockEntry `json:"packages"`
	PackagesDev []lockEntry `json:"packages-dev"`
}

Lock represents the structure of a composer.lock file.

type LockPackage

type LockPackage struct {
	Name    string   `json:"name"`
	Version string   `json:"version"`
	Require []string `json:"require,omitempty"`
}

LockPackage represents a package from composer.lock.

func ParseLock

func ParseLock(r io.Reader) ([]LockPackage, error)

ParseLock parses a composer.lock file and returns the list of packages.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/chainguard-dev/omnibump/pkg/languages/php/composer"
)

func main() {
	lockContent := `{
		"packages": [
			{"name": "monolog/monolog", "version": "3.8.0"},
			{"name": "psr/log", "version": "3.0.2"}
		],
		"packages-dev": [
			{"name": "phpunit/phpunit", "version": "11.5.3"}
		]
	}`

	packages, err := composer.ParseLock(strings.NewReader(lockContent))
	if err != nil {
		fmt.Println("error:", err)
		return
	}

	for _, pkg := range packages {
		fmt.Printf("%s: %s\n", pkg.Name, pkg.Version)
	}
}
Output:
monolog/monolog: 3.8.0
phpunit/phpunit: 11.5.3
psr/log: 3.0.2

type Package

type Package struct {
	Name    string
	Version string
	Index   int // For ordering updates
}

Package represents a Composer package dependency to update.

type UpdateConfig

type UpdateConfig struct {
	ComposerRoot string
	Update       bool // Run 'composer update' before updating packages
	ShowDiff     bool
	NoInstall    bool // Skip package installation (useful for testing)
}

UpdateConfig holds configuration for Composer project updates.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL