✅ IsPackageInstalled
Checks whether the specified package is installed, equivalent to running composer show <package-name> and checking whether the command succeeds.
When to use
Use this to confirm whether a package has been installed in the current project before invoking functionality it provides. For example, before a toolchain loads a plugin, use it to check whether the plugin package exists.
Signature
go
func (c *Composer) IsPackageInstalled(packageName string) boolParameters
| Parameter | Type | Description |
|---|---|---|
packageName | string | The package name to check, e.g. symfony/console |
Return value
| Return value | Type | Description |
|---|---|---|
| Single return value | bool | Returns true if the package is installed; false if it is not installed or the command fails |
Example
go
package main
import (
"fmt"
"log"
"github.com/scagogogo/composer-skills/pkg/composer"
)
func main() {
comp, err := composer.New(composer.DefaultOptions())
if err != nil {
log.Fatalf("Initialization failed: %v", err)
}
if comp.IsPackageInstalled("symfony/console") {
fmt.Println("symfony/console is installed ✅")
} else {
fmt.Println("symfony/console is not installed ❌")
}
}Advanced
- To get the package's installed version number, use GetRequireWithVersion.
- To check whether a package is a development dependency (require-dev), use IsPackageDev.
- To get the list of all installed package names, use GetInstalledPackageNames.