Skip to content

Commit 104718e

Browse files
committed
Detect PHP version from system %PATH% variable
1 parent 90b449c commit 104718e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

discovery_windows.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,20 @@ package phpstore
2121

2222
import (
2323
"os"
24+
"os/exec"
2425
"path/filepath"
2526
"regexp"
27+
"runtime"
2628
)
2729

2830
// see https://github.com/composer/windows-setup/blob/master/src/composer.iss
2931
func (s *PHPStore) doDiscover() {
3032
systemDir := systemDir()
3133
userHomeDir := userHomeDir()
3234

35+
// %PATH%
36+
s.addFromEnv()
37+
3338
// XAMPP
3439
s.addFromDir(filepath.Join(systemDir, "xampp", "php"), nil, "XAMPP")
3540

@@ -53,6 +58,27 @@ func (s *PHPStore) doDiscover() {
5358
}
5459
}
5560

61+
func (s *PHPStore) addFromEnv() {
62+
63+
// Determine the executable name based on the operating system
64+
exeName := "php"
65+
if runtime.GOOS == "windows" {
66+
exeName = "php.exe"
67+
}
68+
69+
// Alternative approach using exec.LookPath
70+
phpPath, err := exec.LookPath(exeName)
71+
if err != nil {
72+
return
73+
}
74+
75+
dir := filepath.Dir(phpPath)
76+
if v := s.discoverPHP(dir, "php"); v != nil {
77+
s.addVersion(v)
78+
}
79+
80+
}
81+
5682
func systemDir() string {
5783
cwd, err := os.Getwd()
5884
if err != nil {

0 commit comments

Comments
 (0)