-
-
Notifications
You must be signed in to change notification settings - Fork 248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add CLI support #239
Changes from 7 commits
d5cebf4
5200298
ddac085
a15dca5
8957b4b
2f97ffc
bc88ade
40f0a29
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
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.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") | ||
} | ||
|
||
status := frankenphp.ExecuteScriptCLI(args[0], args) | ||
os.Exit(status) | ||
|
||
return status, nil | ||
Comment on lines
+34
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm. This return will never be reached. I think returning will let Caddy call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't work (the returned status code is always There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 then that's possibly a bug in Caddy. Good to know. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Opened caddyserver/caddy#5874 to fix that. The status code wasn't being carried through since we enabled Cobra |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
|
||
"github.com/dunglas/frankenphp" | ||
) | ||
|
||
func main() { | ||
if len(os.Args) <= 1 { | ||
log.Println("Usage: testcli script.php") | ||
os.Exit(1) | ||
} | ||
|
||
os.Exit(frankenphp.ExecuteScriptCLI(os.Args[1], os.Args)) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
|
||
var_dump($argv, $_SERVER); | ||
echo "From the CLI\n"; | ||
|
||
exit(3); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes me think, is there value in making
php-cli -i
work for example? (Could be useful to check enabled features). Some people usephp -a
andphp -l
on occasion too. But 🤷♂️There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be great if
./frankenphp php <args>
behaved identical tophp <args>
with php-cli installed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would be nice, but it's a bit more work. The best option would be to allow the embedding of the official PHP SAPI (this requires a patch on PHP). I suggest merging this patch as is and trying to improve it later.