2023-10-09 12:38:15 +00:00
|
|
|
package caddy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os"
|
2023-12-03 15:38:58 +00:00
|
|
|
"path/filepath"
|
2023-10-09 12:38:15 +00:00
|
|
|
|
|
|
|
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) {
|
|
|
|
args := os.Args[2:]
|
|
|
|
if len(args) < 1 {
|
|
|
|
return 1, errors.New("the path to the PHP script is required")
|
|
|
|
}
|
|
|
|
|
2023-12-03 15:38:58 +00:00
|
|
|
if frankenphp.EmbeddedAppPath != "" {
|
|
|
|
if _, err := os.Stat(args[0]); err != nil {
|
|
|
|
args[0] = filepath.Join(frankenphp.EmbeddedAppPath, args[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-09 12:38:15 +00:00
|
|
|
status := frankenphp.ExecuteScriptCLI(args[0], args)
|
|
|
|
os.Exit(status)
|
|
|
|
|
|
|
|
return status, nil
|
|
|
|
}
|