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+
11741240int frankenphp_reset_opcache (void ) {
11751241 zend_function * opcache_reset =
11761242 zend_hash_str_find_ptr (CG (function_table ), ZEND_STRL ("opcache_reset" ));
0 commit comments