php-cli.go 926 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package caddy
  2. import (
  3. "errors"
  4. "os"
  5. "path/filepath"
  6. caddycmd "github.com/caddyserver/caddy/v2/cmd"
  7. "github.com/dunglas/frankenphp"
  8. "github.com/spf13/cobra"
  9. )
  10. func init() {
  11. caddycmd.RegisterCommand(caddycmd.Command{
  12. Name: "php-cli",
  13. Usage: "script.php [args ...]",
  14. Short: "Runs a PHP command",
  15. Long: `
  16. Executes a PHP script similarly to the CLI SAPI.`,
  17. CobraFunc: func(cmd *cobra.Command) {
  18. cmd.DisableFlagParsing = true
  19. cmd.RunE = caddycmd.WrapCommandFuncForCobra(cmdPHPCLI)
  20. },
  21. })
  22. }
  23. func cmdPHPCLI(fs caddycmd.Flags) (int, error) {
  24. args := os.Args[2:]
  25. if len(args) < 1 {
  26. return 1, errors.New("the path to the PHP script is required")
  27. }
  28. if frankenphp.EmbeddedAppPath != "" {
  29. if _, err := os.Stat(args[0]); err != nil {
  30. args[0] = filepath.Join(frankenphp.EmbeddedAppPath, args[0])
  31. }
  32. }
  33. status := frankenphp.ExecuteScriptCLI(args[0], args)
  34. os.Exit(status)
  35. return status, nil
  36. }