-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdiscovery.go
349 lines (324 loc) · 10.5 KB
/
discovery.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* Copyright (c) 2021-present Fabien Potencier <[email protected]>
*
* This file is part of Symfony CLI project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package phpstore
import (
"bufio"
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
version "github.com/hashicorp/go-version"
"github.com/pkg/errors"
)
// discover tries to find all PHP versions on the current machine
func (s *PHPStore) discover() {
s.doDiscover()
// Under $PATH
paths := s.pathDirectories(s.configDir)
s.log("Looking for PHP in the PATH (%s)", paths)
for _, path := range paths {
for _, version := range s.findFromDir(path, nil, "PATH") {
idx := s.addVersion(version)
// the first one is the default/system PHP binary
if s.pathVersion == nil {
s.pathVersion = s.versions[idx]
s.pathVersion.IsSystem = true
s.log(" System PHP version (first in PATH)")
}
}
}
}
func (s *PHPStore) discoverFromDir(root string, phpRegexp *regexp.Regexp, pathRegexp *regexp.Regexp, why string) {
maxDepth := 1
if pathRegexp != nil {
maxDepth += strings.Count(pathRegexp.String(), "/")
}
filepath.Walk(root, func(path string, finfo os.FileInfo, err error) error {
if err != nil {
// prevent panic by handling failure accessing a path
return nil
}
// bypass current directory and non-directory
if root == path || !finfo.IsDir() {
return nil
}
rel, err := filepath.Rel(root, path)
if err != nil {
return errors.WithStack(err)
}
// only maxDepth+1 levels of depth
if strings.Count(rel, string(os.PathSeparator)) > maxDepth {
return filepath.SkipDir
}
s.log("Looking for PHP in %s (%+v) -- %s", path, pathRegexp, why)
if pathRegexp == nil || pathRegexp.MatchString(rel) {
s.addFromDir(path, phpRegexp, why)
return filepath.SkipDir
}
return nil
})
}
func (s *PHPStore) addFromDir(dir string, phpRegexp *regexp.Regexp, why string) {
for _, v := range s.findFromDir(dir, phpRegexp, why) {
s.addVersion(v)
}
}
func (s *PHPStore) findFromDir(dir string, phpRegexp *regexp.Regexp, why string) []*Version {
s.log("Looking for PHP in %s (%+v) -- %s", dir, phpRegexp, why)
root := dir
if filepath.Base(dir) == "bin" {
dir = filepath.Dir(dir)
} else if runtime.GOOS != "windows" {
root = filepath.Join(dir, "bin")
}
if phpRegexp == nil {
if v := s.discoverPHP(dir, "php"); v != nil {
return []*Version{v}
}
return nil
}
if _, err := os.Stat(root); err != nil {
s.log(" Skipping %s as it does not exist", root)
return nil
}
var versions []*Version
filepath.Walk(root, func(path string, finfo os.FileInfo, err error) error {
if err != nil {
// prevent panic by handling failure accessing a path
return nil
}
if root != path && finfo.IsDir() {
return filepath.SkipDir
}
if phpRegexp.MatchString(filepath.Base(path)) {
if i := s.discoverPHP(dir, filepath.Base(path)); i != nil {
versions = append(versions, i)
}
return nil
}
return nil
})
return versions
}
func (s *PHPStore) discoverPHP(dir, binName string) *Version {
// when php-config is not available/useable, fallback to discovering via php, slower but always work
if runtime.GOOS == "windows" {
// php-config does not exist on Windows
return s.discoverPHPViaPHP(dir, binName)
}
phpConfigPath := filepath.Join(dir, "bin", strings.Replace(binName, "php", "php-config", 1))
fi, err := os.Lstat(phpConfigPath)
if err != nil {
return s.discoverPHPViaPHP(dir, binName)
}
// on Linux, when using alternatives, php-config does not point to right PHP version, so, it cannot be used
if fi.Mode()&os.ModeSymlink != 0 {
if path, err := os.Readlink(phpConfigPath); err == nil && strings.Contains(path, "/alternatives/") {
return s.discoverPHPViaPHP(dir, binName)
}
}
return s.discoverPHPViaPHPConfig(dir, binName)
}
func (s *PHPStore) discoverPHPViaPHP(dir, binName string) *Version {
php := filepath.Join(dir, "bin", binName)
if runtime.GOOS == "windows" {
binName += ".exe"
php = filepath.Join(dir, binName)
}
if _, err := os.Stat(php); err != nil {
return nil
}
var buf bytes.Buffer
cmd := exec.Command(php, "--version")
cmd.Stdout = &buf
cmd.Stderr = &buf
if err := cmd.Run(); err != nil {
s.log(` Unable to run "%s --version: %s"`, php, err)
return nil
}
r := regexp.MustCompile("PHP (\\d+\\.\\d+\\.\\d+)")
data := r.FindSubmatch(buf.Bytes())
if data == nil {
s.log(" %s is not a PHP binary", php)
return nil
}
php = filepath.Clean(php)
var err error
php, err = filepath.EvalSymlinks(php)
if err != nil {
s.log(" %s is not a valid symlink", php)
return nil
}
v := s.validateVersion(dir, normalizeVersion(string(data[1])))
if v == nil {
return nil
}
version := &Version{
Path: dir,
Version: v.String(),
FullVersion: v,
PHPPath: php,
}
fpm := filepath.Join(dir, "sbin", strings.Replace(binName, "php", "php-fpm", 1))
if _, err := os.Stat(fpm); os.IsNotExist(err) {
fpm = filepath.Join(dir, "bin", strings.Replace(binName, "php", "php-fpm", 1))
}
cgi := filepath.Join(dir, "bin", strings.Replace(binName, "php", "php-cgi", 1))
phpconfig := filepath.Join(dir, "bin", strings.Replace(binName, "php", "php-config", 1))
phpize := filepath.Join(dir, "bin", strings.Replace(binName, "php", "phpize", 1))
phpdbg := filepath.Join(dir, "bin", strings.Replace(binName, "php", "phpdbg", 1))
if runtime.GOOS == "windows" {
fpm = filepath.Join(dir, strings.Replace(binName, "php", "php-fpm", 1))
cgi = filepath.Join(dir, strings.Replace(binName, "php", "php-cgi", 1))
phpconfig = filepath.Join(dir, strings.Replace(binName, "php", "php-config", 1))
phpize = filepath.Join(dir, strings.Replace(binName, "php", "phpize", 1))
phpdbg = filepath.Join(dir, strings.Replace(binName, "php", "phpdbg", 1))
}
s.log(version.setServer(fpm, cgi, phpconfig, phpize, phpdbg))
return version
}
func (s *PHPStore) discoverPHPViaPHPConfig(dir, binName string) *Version {
phpConfig := filepath.Join(dir, "bin", strings.Replace(binName, "php", "php-config", 1))
file, err := os.Open(phpConfig)
if err != nil {
s.log(" Unable to open %s: %s", phpConfig, err)
return nil
}
version := &Version{
Path: dir,
}
sc := bufio.NewScanner(file)
programPrefix := ""
programSuffix := ""
programExtension := ""
phpCgiBinary := ""
allFound := 0
for sc.Scan() {
if strings.HasPrefix(sc.Text(), "vernum=") {
v := s.validateVersion(dir, strings.Trim(sc.Text()[len("vernum="):], `"`))
if v == nil {
return nil
}
version.Version = v.String()
version.FullVersion = v
allFound++
} else if strings.HasPrefix(sc.Text(), "program_prefix=") {
programPrefix = strings.Trim(sc.Text()[len("program_prefix="):], `"`)
allFound++
} else if strings.HasPrefix(sc.Text(), "program_suffix=") {
programSuffix = strings.Trim(sc.Text()[len("program_suffix="):], `"`)
allFound++
} else if strings.HasPrefix(sc.Text(), " php_cgi_binary=") {
phpCgiBinary = strings.Trim(sc.Text()[len(" php_cgi_binary="):], `"`)
allFound++
} else if strings.HasPrefix(sc.Text(), "exe_extension=") {
programExtension = strings.Trim(sc.Text()[len("exe_extension="):], `"`)
allFound++
}
}
if version.FullVersion == nil {
s.log(" Unable to find version in %s", phpConfig)
return nil
}
if allFound != 5 {
s.log(" Unable to parse all information from %s", phpConfig)
return nil
}
if phpCgiBinary == "" {
phpCgiBinary = fmt.Sprintf("%sphp%s-cgi%s", programPrefix, programSuffix, programExtension)
} else {
phpCgiBinary = strings.Replace(phpCgiBinary, "${program_prefix}", programPrefix, 1)
phpCgiBinary = strings.Replace(phpCgiBinary, "${program_suffix}", programSuffix, 1)
phpCgiBinary = strings.Replace(phpCgiBinary, "${exe_extension}", programExtension, 1)
phpCgiBinary = strings.Replace(phpCgiBinary, "${exec_prefix}/", "", 1)
phpCgiBinary = strings.Replace(phpCgiBinary, "bin/", "", 1)
}
version.PHPPath = filepath.Join(version.Path, "bin", fmt.Sprintf("%sphp%s%s", programPrefix, programSuffix, programExtension))
s.log(version.setServer(
filepath.Join(version.Path, "sbin", fmt.Sprintf("%sphp-fpm%s%s", programPrefix, programSuffix, programExtension)),
filepath.Join(version.Path, "bin", phpCgiBinary),
filepath.Join(version.Path, "bin", fmt.Sprintf("%sphp-config%s%s", programPrefix, programSuffix, programExtension)),
filepath.Join(version.Path, "bin", fmt.Sprintf("%sphpize%s%s", programPrefix, programSuffix, programExtension)),
filepath.Join(version.Path, "bin", fmt.Sprintf("%sphpdbg%s%s", programPrefix, programSuffix, programExtension)),
))
return version
}
func (s *PHPStore) validateVersion(path, v string) *version.Version {
if len(v) != 5 {
s.log(" Unable to parse version %s for PHP at %s: version is non-standard", v, path)
return nil
}
version, err := version.NewVersion(fmt.Sprintf("%c.%s.%s", v[0], v[1:3], v[3:5]))
if err != nil {
s.log(" Unable to parse version %s for PHP at %s: %s", v, path, err)
return nil
}
return version
}
func normalizeVersion(v string) string {
// version is XYYZZ
parts := strings.Split(v, ".")
version := parts[0]
if len(parts[1]) == 1 {
version += "0"
}
version += parts[1]
if len(parts[2]) == 1 {
version += "0"
}
return version + parts[2]
}
func (s *PHPStore) pathDirectories(configDir string) []string {
phpShimDir := filepath.Join(configDir, "bin")
path := os.Getenv("PATH")
if runtime.GOOS == "windows" {
path = os.Getenv("Path")
}
user := os.Getenv("USERPROFILE")
dirs := []string{}
seen := make(map[string]bool)
for _, dir := range filepath.SplitList(path) {
dir = strings.Replace(dir, "%%USERPROFILE%%", user, 1)
edir, err := filepath.EvalSymlinks(dir)
if err != nil {
continue
}
if edir == phpShimDir {
continue
}
if edir == "" {
continue
}
if _, ok := seen[edir]; ok {
if dir != edir {
s.log(" Skipping %s (alias of %s), already in the PATH", dir, edir)
} else {
s.log(" Skipping %s, already in the PATH", dir)
}
continue
}
dirs = append(dirs, edir)
seen[edir] = true
}
return dirs
}