Skip to content

Commit

Permalink
Caddy php-cli command
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Oct 5, 2023
1 parent 7c81a1a commit 293f57f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
36 changes: 36 additions & 0 deletions caddy/php-cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package caddy

import (
"errors"
"os"

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.RunE = caddycmd.WrapCommandFuncForCobra(cmdPHPCLI)
},
})
}

func cmdPHPCLI(fs caddycmd.Flags) (int, error) {
args := fs.Args()
if len(args) < 1 {
return 1, errors.New("the path to the PHP script is required")
}

status := frankenphp.ExecuteScriptCLI(args[0], args)
os.Exit(status)

return status, nil
}
2 changes: 1 addition & 1 deletion caddy/command.go → caddy/php-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
func init() {
caddycmd.RegisterCommand(caddycmd.Command{
Name: "php-server",
Usage: "[--domain <example.com>] [--root <path>] [--listen <addr>] [--access-log]",
Usage: "[--domain <example.com>] [--root <path>] [--listen <addr>] [--access-log] [--debug] [--no-compress]",
Short: "Spins up a production-ready PHP server",
Long: `
A simple but production-ready PHP server. Useful for quick deployments,
Expand Down
2 changes: 2 additions & 0 deletions frankenphp.c
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,8 @@ int frankenphp_execute_script_cli(char *script, int argc, char **argv) {

int exit_status;

// The SAPI name "cli" is hardcoded into too many programs... let's usurp it.
php_embed_module.name = "cli";
php_embed_module.pretty_name = "PHP CLI embedded in FrankenPHP";

php_embed_init(argc, argv);
Expand Down

0 comments on commit 293f57f

Please sign in to comment.