Project Management API
The project management API provides functionality for creating, configuring, and managing PHP projects.
Project Creation
CreateProject
Creates a new PHP project from a Composer package.
func (c *Composer) CreateProject(packageName string, directory string, version string) error
Parameters:
packageName
- Name of the package to create project fromdirectory
- Target directory for the new projectversion
- Version constraint (optional, use "" for latest)
Returns:
error
- Error if project creation fails
Example:
// Create a Laravel project
err := comp.CreateProject("laravel/laravel", "my-laravel-app", "")
// Create a Symfony project with specific version
err := comp.CreateProject("symfony/skeleton", "my-symfony-app", "^6.0")
if err != nil {
log.Printf("Failed to create project: %v", err)
}
CreateProjectWithOptions
Creates a project with advanced options.
func (c *Composer) CreateProjectWithOptions(options CreateProjectOptions) error
Parameters:
options
- Advanced project creation options
Example:
options := CreateProjectOptions{
PackageName: "laravel/laravel",
Directory: "my-app",
Version: "^10.0",
PreferDist: true,
NoScripts: false,
NoProgress: false,
KeepVcs: false,
NoInstall: false,
IgnorePlatformReqs: false,
}
err := comp.CreateProjectWithOptions(options)
Configuration Management
Validate
Validates the composer.json file in the current project.
func (c *Composer) Validate() error
Returns:
error
- Error if validation fails or file is invalid
Example:
err := comp.Validate()
if err != nil {
log.Printf("composer.json validation failed: %v", err)
} else {
fmt.Println("composer.json is valid")
}
ValidateWithOptions
Validates composer.json with specific options.
func (c *Composer) ValidateWithOptions(options ValidateOptions) error
Example:
options := ValidateOptions{
NoCheckAll: false,
NoCheckLock: false,
NoCheckPublish: false,
WithDependencies: true,
Strict: true,
}
err := comp.ValidateWithOptions(options)
GetConfig
Gets a configuration value.
func (c *Composer) GetConfig(key string) (string, error)
Parameters:
key
- Configuration key to retrieve
Returns:
string
- Configuration valueerror
- Error if key doesn't exist or retrieval fails
Example:
// Get cache directory
cacheDir, err := comp.GetConfig("cache-dir")
if err != nil {
log.Printf("Failed to get config: %v", err)
} else {
fmt.Printf("Cache directory: %s\n", cacheDir)
}
// Get other common configs
homeDir, _ := comp.GetConfig("home")
vendorDir, _ := comp.GetConfig("vendor-dir")
SetConfig
Sets a configuration value.
func (c *Composer) SetConfig(key string, value string) error
Parameters:
key
- Configuration key to setvalue
- Configuration value
Returns:
error
- Error if setting fails
Example:
// Set memory limit
err := comp.SetConfig("memory-limit", "-1")
// Set process timeout
err = comp.SetConfig("process-timeout", "600")
// Set preferred install method
err = comp.SetConfig("preferred-install", "dist")
if err != nil {
log.Printf("Failed to set config: %v", err)
}
UnsetConfig
Removes a configuration value.
func (c *Composer) UnsetConfig(key string) error
Parameters:
key
- Configuration key to remove
Example:
err := comp.UnsetConfig("github-oauth.github.com")
if err != nil {
log.Printf("Failed to unset config: %v", err)
}
Script Management
RunScript
Executes a script defined in composer.json.
func (c *Composer) RunScript(scriptName string) error
Parameters:
scriptName
- Name of the script to run
Returns:
error
- Error if script execution fails
Example:
// Run common scripts
err := comp.RunScript("test")
err = comp.RunScript("post-install-cmd")
err = comp.RunScript("build")
if err != nil {
log.Printf("Script execution failed: %v", err)
}
RunScriptWithArgs
Executes a script with additional arguments.
func (c *Composer) RunScriptWithArgs(scriptName string, args []string) error
Parameters:
scriptName
- Name of the script to runargs
- Additional arguments to pass to the script
Example:
// Run PHPUnit with specific options
args := []string{"--coverage-html", "coverage/"}
err := comp.RunScriptWithArgs("test", args)
// Run custom script with parameters
err = comp.RunScriptWithArgs("deploy", []string{"production", "--force"})
ListScripts
Lists all available scripts in the project.
func (c *Composer) ListScripts() (string, error)
Returns:
string
- List of available scriptserror
- Error if listing fails
Example:
scripts, err := comp.ListScripts()
if err != nil {
log.Printf("Failed to list scripts: %v", err)
} else {
fmt.Printf("Available scripts:\n%s\n", scripts)
}
Autoloader Management
DumpAutoload
Generates the autoloader files.
func (c *Composer) DumpAutoload() error
Returns:
error
- Error if autoloader generation fails
Example:
err := comp.DumpAutoload()
if err != nil {
log.Printf("Failed to dump autoload: %v", err)
}
DumpAutoloadOptimized
Generates optimized autoloader files.
func (c *Composer) DumpAutoloadOptimized() error
Returns:
error
- Error if optimized autoloader generation fails
Example:
err := comp.DumpAutoloadOptimized()
if err != nil {
log.Printf("Failed to dump optimized autoload: %v", err)
}
DumpAutoloadWithOptions
Generates autoloader with specific options.
func (c *Composer) DumpAutoloadWithOptions(options AutoloadOptions) error
Example:
options := AutoloadOptions{
Optimize: true,
Classmap: true,
Apcu: false,
ApcuPrefix: "",
NoDev: false,
}
err := comp.DumpAutoloadWithOptions(options)
Composer.json Management
ReadComposerJSON
Reads and parses the composer.json file.
func (c *Composer) ReadComposerJSON() (*ComposerJSON, error)
Returns:
*ComposerJSON
- Parsed composer.json structureerror
- Error if reading or parsing fails
Example:
composerData, err := comp.ReadComposerJSON()
if err != nil {
log.Printf("Failed to read composer.json: %v", err)
return
}
fmt.Printf("Project name: %s\n", composerData.Name)
fmt.Printf("Description: %s\n", composerData.Description)
fmt.Printf("Type: %s\n", composerData.Type)
// Access dependencies
for pkg, version := range composerData.Require {
fmt.Printf("Requires: %s %s\n", pkg, version)
}
WriteComposerJSON
Writes a ComposerJSON structure to the composer.json file.
func (c *Composer) WriteComposerJSON(data *ComposerJSON) error
Parameters:
data
- ComposerJSON structure to write
Returns:
error
- Error if writing fails
Example:
// Read existing composer.json
composerData, err := comp.ReadComposerJSON()
if err != nil {
log.Fatal(err)
}
// Modify the data
composerData.Description = "Updated description"
composerData.Require["new/package"] = "^1.0"
// Write back to file
err = comp.WriteComposerJSON(composerData)
if err != nil {
log.Printf("Failed to write composer.json: %v", err)
}
InitProject
Initializes a new composer.json file in the current directory.
func (c *Composer) InitProject(options InitOptions) error
Parameters:
options
- Project initialization options
Example:
options := InitOptions{
Name: "vendor/my-package",
Description: "My awesome PHP package",
Type: "library",
License: "MIT",
Authors: []Author{
{
Name: "John Doe",
Email: "john@example.com",
},
},
MinimumStability: "stable",
PreferStable: true,
}
err := comp.InitProject(options)
if err != nil {
log.Printf("Failed to initialize project: %v", err)
}
Archive Management
Archive
Creates an archive of the project.
func (c *Composer) Archive(format string, file string) error
Parameters:
format
- Archive format ("tar", "zip")file
- Output file path
Returns:
error
- Error if archive creation fails
Example:
// Create a ZIP archive
err := comp.Archive("zip", "my-project.zip")
// Create a TAR archive
err = comp.Archive("tar", "my-project.tar")
if err != nil {
log.Printf("Failed to create archive: %v", err)
}
ArchivePackage
Creates an archive of a specific package.
func (c *Composer) ArchivePackage(packageName string, version string, format string, file string) error
Parameters:
packageName
- Name of the package to archiveversion
- Version to archiveformat
- Archive formatfile
- Output file path
Example:
err := comp.ArchivePackage("symfony/console", "^6.0", "zip", "symfony-console.zip")
Type Definitions
CreateProjectOptions
type CreateProjectOptions struct {
PackageName string
Directory string
Version string
PreferDist bool
PreferSource bool
NoScripts bool
NoProgress bool
KeepVcs bool
NoInstall bool
IgnorePlatformReqs bool
}
ValidateOptions
type ValidateOptions struct {
NoCheckAll bool
NoCheckLock bool
NoCheckPublish bool
WithDependencies bool
Strict bool
}
AutoloadOptions
type AutoloadOptions struct {
Optimize bool
Classmap bool
Apcu bool
ApcuPrefix string
NoDev bool
}
InitOptions
type InitOptions struct {
Name string
Description string
Type string
License string
Authors []Author
MinimumStability string
PreferStable bool
Require map[string]string
RequireDev map[string]string
}
Best Practices
- Always validate composer.json after making changes
- Use version constraints appropriately in project templates
- Regenerate autoloader after adding new classes
- Keep scripts organized and well-documented
- Use appropriate archive formats for distribution
- Set meaningful project metadata during initialization
- Handle configuration changes carefully in production environments