@@ -40,7 +40,10 @@ static const char HARDCODED_INI[] = "max_execution_time=0\n"
4040 "max_input_time=-1\n\0" ;
4141#endif
4242
43+ #define INITIAL_MODULES_CAPACITY 8
44+
4345static const char * MODULES_TO_RELOAD [] = {"filter" , "session" , NULL };
46+ frankenphp_modules_to_reload modules_to_reload = {NULL , 0 , 0 };
4447
4548frankenphp_version frankenphp_get_version () {
4649 return (frankenphp_version ){
@@ -130,18 +133,45 @@ static void frankenphp_release_temporary_streams() {
130133 ZEND_HASH_FOREACH_END ();
131134}
132135
136+ static void init_modules_to_reload (void ) {
137+ if (modules_to_reload .names != NULL ) {
138+ return ;
139+ }
140+
141+ size_t count = 0 ;
142+ for (const char * * ptr = MODULES_TO_RELOAD ; * ptr != NULL ; ptr ++ ) {
143+ count ++ ;
144+ }
145+
146+ size_t capacity = count > 0 ? count * 2 : INITIAL_MODULES_CAPACITY ;
147+ modules_to_reload .names = malloc (capacity * sizeof (char * ));
148+ if (!modules_to_reload .names ) {
149+ return ; // TODO: handle this as an error
150+ }
151+
152+ for (size_t i = 0 ; i < count ; i ++ ) {
153+ modules_to_reload .names [i ] = strdup (MODULES_TO_RELOAD [i ]);
154+ }
155+
156+ modules_to_reload .count = count ;
157+ modules_to_reload .capacity = capacity ;
158+ }
159+
133160/* Adapted from php_request_shutdown */
134161static void frankenphp_worker_request_shutdown () {
135162 /* Flush all output buffers */
136163 zend_try { php_output_end_all (); }
137164 zend_end_try ();
138165
139- /* TODO: store the list of modules to reload in a global module variable */
140- const char * * module_name ;
166+ if (modules_to_reload .names == NULL ) {
167+ init_modules_to_reload ();
168+ }
169+
141170 zend_module_entry * module ;
142- for (module_name = MODULES_TO_RELOAD ; * module_name ; module_name ++ ) {
143- if ((module = zend_hash_str_find_ptr (& module_registry , * module_name ,
144- strlen (* module_name )))) {
171+ for (size_t i = 0 ; i < modules_to_reload .count ; i ++ ) {
172+ if ((module = zend_hash_str_find_ptr (& module_registry ,
173+ modules_to_reload .names [i ],
174+ strlen (modules_to_reload .names [i ])))) {
145175 module -> request_shutdown_func (module -> type , module -> module_number );
146176 }
147177 }
@@ -159,6 +189,39 @@ static void frankenphp_worker_request_shutdown() {
159189 zend_set_memory_limit (PG (memory_limit ));
160190}
161191
192+ /* API for extensions to register their modules to reload */
193+ bool frankenphp_register_module_to_reload (const char * module_name ) {
194+ if (module_name == NULL ) {
195+ return false;
196+ }
197+
198+ if (modules_to_reload .names == NULL ) {
199+ init_modules_to_reload ();
200+ }
201+
202+ for (size_t i = 0 ; i < modules_to_reload .count ; i ++ ) {
203+ if (strcmp (modules_to_reload .names [i ], module_name ) == 0 ) {
204+ return true;
205+ }
206+ }
207+
208+ if (modules_to_reload .count >= modules_to_reload .capacity ) {
209+ size_t new_capacity = modules_to_reload .capacity * 2 ;
210+ const char * * new_names =
211+ realloc (modules_to_reload .names , new_capacity * sizeof (char * ));
212+ if (!new_names ) {
213+ return false; // Out of memory
214+ }
215+ modules_to_reload .names = new_names ;
216+ modules_to_reload .capacity = new_capacity ;
217+ }
218+
219+ modules_to_reload .names [modules_to_reload .count ] = strdup (module_name );
220+ modules_to_reload .count ++ ;
221+
222+ return true;
223+ }
224+
162225PHPAPI void get_full_env (zval * track_vars_array ) {
163226 struct go_getfullenv_return full_env = go_getfullenv (thread_index );
164227
@@ -225,12 +288,17 @@ static int frankenphp_worker_request_startup() {
225288 frankenphp_server_context * ctx = SG (server_context );
226289 ctx -> finished = false;
227290
228- /* TODO: store the list of modules to reload in a global module variable */
229- const char * * module_name ;
291+ /* Initialize modules to reload if needed */
292+ if (modules_to_reload .names == NULL ) {
293+ init_modules_to_reload ();
294+ }
295+
296+ /* Reload modules */
230297 zend_module_entry * module ;
231- for (module_name = MODULES_TO_RELOAD ; * module_name ; module_name ++ ) {
232- if ((module = zend_hash_str_find_ptr (& module_registry , * module_name ,
233- sizeof (* module_name ) - 1 )) &&
298+ for (size_t i = 0 ; i < modules_to_reload .count ; i ++ ) {
299+ if ((module = zend_hash_str_find_ptr (
300+ & module_registry , modules_to_reload .names [i ],
301+ strlen (modules_to_reload .names [i ]))) &&
234302 module -> request_startup_func ) {
235303 module -> request_startup_func (module -> type , module -> module_number );
236304 }
0 commit comments