-
Notifications
You must be signed in to change notification settings - Fork 221
/
laravel.go
106 lines (91 loc) · 2.44 KB
/
laravel.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
package scanner
import (
"encoding/base64"
"fmt"
"os/exec"
"regexp"
"strconv"
"github.com/superfly/flyctl/helpers"
)
type ComposerLock struct {
Platform PhpVersion `json:"platform,omitempty"`
}
type PhpVersion struct {
Version string `json:"php"`
}
// setup Laravel with a sqlite database
func configureLaravel(sourceDir string, config *ScannerConfig) (*SourceInfo, error) {
// Laravel projects contain the `artisan` command
if !checksPass(sourceDir, fileExists("artisan")) {
return nil, nil
}
files := templates("templates/laravel")
s := &SourceInfo{
Env: map[string]string{
"APP_ENV": "production",
"LOG_CHANNEL": "stderr",
"LOG_LEVEL": "info",
"LOG_STDERR_FORMATTER": "Monolog\\Formatter\\JsonFormatter",
"SESSION_DRIVER": "cookie",
"SESSION_SECURE_COOKIE": "true",
},
Family: "Laravel",
Files: files,
Port: 8080,
Secrets: []Secret{
{
Key: "APP_KEY",
Help: "Laravel needs a unique application key.",
Generate: func() (string, error) {
// Method used in RandBytes never returns an error
r, _ := helpers.RandBytes(32)
return "base64:" + base64.StdEncoding.EncodeToString(r), nil
},
},
},
SkipDatabase: true,
ConsoleCommand: "php /var/www/html/artisan tinker",
}
phpVersion, err := extractPhpVersion()
if err != nil || phpVersion == "" {
// Fallback to 8.0, which has
// the broadest compatibility
phpVersion = "8.0"
}
s.BuildArgs = map[string]string{
"PHP_VERSION": phpVersion,
"NODE_VERSION": "18",
}
return s, nil
}
func extractPhpVersion() (string, error) {
/* Example Output:
PHP 8.1.8 (cli) (built: Jul 8 2022 10:58:31) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.8, Copyright (c) Zend Technologies
with Zend OPcache v8.1.8, Copyright (c), by Zend Technologies
*/
cmd := exec.Command("php", "-v")
out, err := cmd.CombinedOutput()
if err != nil {
return "", err
}
// Capture major/minor version (leaving out revision version)
re := regexp.MustCompile(`PHP ([0-9]+\.[0-9]+)\.[0-9]`)
match := re.FindStringSubmatch(string(out))
if len(match) > 1 {
// If the PHP version is below 7.4, we won't have a
// container for it, so we'll use PHP 7.4
if match[1][0:1] == "7" {
vers, err := strconv.ParseFloat(match[1], 32)
if err != nil {
return "7.4", nil
}
if vers < 7.4 {
return "7.4", nil
}
}
return match[1], nil
}
return "", fmt.Errorf("could not find php version")
}