forked from php/frankenphp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathphp-cli.go
More file actions
43 lines (34 loc) · 1.03 KB
/
php-cli.go
File metadata and controls
43 lines (34 loc) · 1.03 KB
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
package caddy
import (
"os"
"path/filepath"
"strings"
caddycmd "github.com/caddyserver/caddy/v2/cmd"
"github.com/dunglas/frankenphp"
"github.com/spf13/cobra"
)
func init() {
caddycmd.RegisterCommand(caddycmd.Command{
Name: "php-cli",
Usage: "script.php [args ...]",
Short: "Runs a PHP command",
Long: `
Executes a PHP script similarly to the CLI SAPI.`,
CobraFunc: func(cmd *cobra.Command) {
cmd.DisableFlagParsing = true
cmd.RunE = caddycmd.WrapCommandFuncForCobra(cmdPHPCLI)
},
})
}
func cmdPHPCLI(fs caddycmd.Flags) (int, error) {
// php's cli sapi expects the 0th arg to be the program itself, only filter out 'php-cli' arg
args := append([]string{os.Args[0]}, os.Args[2:]...)
if frankenphp.EmbeddedAppPath != "" && len(args) > 1 && !strings.HasPrefix(args[1], "-") && strings.HasSuffix(args[1], ".php") {
if _, err := os.Stat(args[1]); err != nil {
args[1] = filepath.Join(frankenphp.EmbeddedAppPath, args[1])
}
}
status := frankenphp.ExecuteScriptCLI(args)
os.Exit(status)
return status, nil
}