Skip to content

Commit 6ecd323

Browse files
feat: add support for -i and -v flags when using php-cli
1 parent abfd893 commit 6ecd323

4 files changed

Lines changed: 107 additions & 7 deletions

File tree

caddy/php-cli.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ func cmdPHPCLI(fs caddycmd.Flags) (int, error) {
3838
}
3939

4040
var status int
41+
42+
if len(args) == 1 {
43+
switch args[0] {
44+
case "-i", "--info":
45+
status = frankenphp.DisplayPHPInfo()
46+
os.Exit(status)
47+
return status, nil
48+
case "-v", "--version":
49+
status = frankenphp.DisplayPHPVersion()
50+
os.Exit(status)
51+
return status, nil
52+
}
53+
}
54+
4155
if len(args) >= 2 && args[0] == "-r" {
4256
status = frankenphp.ExecutePHPCode(args[1])
4357
} else {

frankenphp.c

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <errno.h>
77
#include <ext/spl/spl_exceptions.h>
88
#include <ext/standard/head.h>
9+
#include <ext/standard/info.h>
910
#include <inttypes.h>
1011
#include <php.h>
1112
#include <php_config.h>
@@ -1144,21 +1145,16 @@ static void *execute_script_cli(void *arg) {
11441145
return exit_status;
11451146
}
11461147

1147-
int frankenphp_execute_script_cli(char *script, int argc, char **argv,
1148-
bool eval) {
1148+
static int frankenphp_execute_in_thread(void *(*thread_func)(void *), void *arg) {
11491149
pthread_t thread;
11501150
int err;
11511151
void *exit_status;
11521152

1153-
cli_script = script;
1154-
cli_argc = argc;
1155-
cli_argv = argv;
1156-
11571153
/*
11581154
* Start the script in a dedicated thread to prevent conflicts between Go and
11591155
* PHP signal handlers
11601156
*/
1161-
err = pthread_create(&thread, NULL, execute_script_cli, (void *)eval);
1157+
err = pthread_create(&thread, NULL, thread_func, arg);
11621158
if (err != 0) {
11631159
return err;
11641160
}
@@ -1171,6 +1167,76 @@ int frankenphp_execute_script_cli(char *script, int argc, char **argv,
11711167
return (intptr_t)exit_status;
11721168
}
11731169

1170+
int frankenphp_execute_script_cli(char *script, int argc, char **argv,
1171+
bool eval) {
1172+
cli_script = script;
1173+
cli_argc = argc;
1174+
cli_argv = argv;
1175+
1176+
return frankenphp_execute_in_thread(execute_script_cli, (void *)eval);
1177+
}
1178+
1179+
static void *frankenphp_execute_with_php_embed(void *arg) {
1180+
php_embed_context *ctx = (php_embed_context *)arg;
1181+
void *exit_status;
1182+
1183+
php_embed_module.name = ctx->module_name;
1184+
php_embed_module.pretty_name = ctx->pretty_name;
1185+
1186+
php_embed_init(ctx->argc, ctx->argv);
1187+
1188+
zend_first_try {
1189+
ctx->execute_func(ctx->execute_arg);
1190+
} zend_end_try();
1191+
1192+
exit_status = (void *)(intptr_t)EG(exit_status);
1193+
1194+
php_embed_shutdown();
1195+
1196+
return exit_status;
1197+
}
1198+
1199+
static void *frankenphp_execute_standard(void (*execute_func)(void *)) {
1200+
php_embed_context ctx = {
1201+
.module_name = "frankenphp",
1202+
.pretty_name = "FrankenPHP",
1203+
.argc = 0,
1204+
.argv = NULL,
1205+
.execute_func = execute_func,
1206+
.execute_arg = NULL
1207+
};
1208+
1209+
return frankenphp_execute_with_php_embed(&ctx);
1210+
}
1211+
1212+
static void *standard_thread_wrapper(void *arg) {
1213+
void (*execute_func)(void *) = (void (*)(void *))arg;
1214+
return frankenphp_execute_standard(execute_func);
1215+
}
1216+
1217+
static int frankenphp_execute_standard_in_thread(void (*execute_func)(void *)) {
1218+
return frankenphp_execute_in_thread(standard_thread_wrapper, (void *)execute_func);
1219+
}
1220+
1221+
static void execute_phpinfo(void *arg) {
1222+
sapi_module.phpinfo_as_text = 1;
1223+
php_print_info(PHP_INFO_ALL);
1224+
php_output_end_all();
1225+
}
1226+
1227+
static void execute_version_print(void *arg) {
1228+
const char *vi = php_version();
1229+
php_printf("%s\n", vi);
1230+
}
1231+
1232+
int frankenphp_print_phpinfo(void) {
1233+
return frankenphp_execute_standard_in_thread(execute_phpinfo);
1234+
}
1235+
1236+
int frankenphp_print_php_version(void) {
1237+
return frankenphp_execute_standard_in_thread(execute_version_print);
1238+
}
1239+
11741240
int frankenphp_reset_opcache(void) {
11751241
zend_function *opcache_reset =
11761242
zend_hash_str_find_ptr(CG(function_table), ZEND_STRL("opcache_reset"));

frankenphp.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,14 @@ func ExecutePHPCode(phpCode string) int {
628628
return int(C.frankenphp_execute_script_cli(cCode, 0, nil, true))
629629
}
630630

631+
func DisplayPHPInfo() int {
632+
return int(C.frankenphp_print_phpinfo())
633+
}
634+
635+
func DisplayPHPVersion() int {
636+
return int(C.frankenphp_print_php_version())
637+
}
638+
631639
func convertArgs(args []string) (C.int, []*C.char) {
632640
argc := C.int(len(args))
633641
argv := make([]*C.char, argc)

frankenphp.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ int frankenphp_execute_script(char *file_name);
6565
int frankenphp_execute_script_cli(char *script, int argc, char **argv,
6666
bool eval);
6767

68+
typedef struct {
69+
char *module_name;
70+
char *pretty_name;
71+
int argc;
72+
char **argv;
73+
void (*execute_func)(void *);
74+
void *execute_arg;
75+
} php_embed_context;
76+
77+
int frankenphp_print_phpinfo();
78+
int frankenphp_print_php_version();
79+
6880
void frankenphp_register_variables_from_request_info(
6981
zval *track_vars_array, zend_string *content_type,
7082
zend_string *path_translated, zend_string *query_string,

0 commit comments

Comments
 (0)