3333ZEND_TSRMLS_CACHE_DEFINE ()
3434#endif
3535
36+ #define INITIAL_MODULES_CAPACITY 8
37+
3638static const char * MODULES_TO_RELOAD [] = {"filter" , "session" , NULL };
39+ frankenphp_modules_to_reload modules_to_reload = {NULL , 0 , 0 };
3740
3841frankenphp_version frankenphp_get_version () {
3942 return (frankenphp_version ){
@@ -115,18 +118,45 @@ static void frankenphp_release_temporary_streams() {
115118 ZEND_HASH_FOREACH_END ();
116119}
117120
121+ static void init_modules_to_reload (void ) {
122+ if (modules_to_reload .names != NULL ) {
123+ return ;
124+ }
125+
126+ size_t count = 0 ;
127+ for (const char * * ptr = MODULES_TO_RELOAD ; * ptr != NULL ; ptr ++ ) {
128+ count ++ ;
129+ }
130+
131+ size_t capacity = count > 0 ? count * 2 : INITIAL_MODULES_CAPACITY ;
132+ modules_to_reload .names = malloc (capacity * sizeof (char * ));
133+ if (!modules_to_reload .names ) {
134+ return ; // TODO: handle this as an error
135+ }
136+
137+ for (size_t i = 0 ; i < count ; i ++ ) {
138+ modules_to_reload .names [i ] = strdup (MODULES_TO_RELOAD [i ]);
139+ }
140+
141+ modules_to_reload .count = count ;
142+ modules_to_reload .capacity = capacity ;
143+ }
144+
118145/* Adapted from php_request_shutdown */
119146static void frankenphp_worker_request_shutdown () {
120147 /* Flush all output buffers */
121148 zend_try { php_output_end_all (); }
122149 zend_end_try ();
123150
124- /* TODO: store the list of modules to reload in a global module variable */
125- const char * * module_name ;
151+ if (modules_to_reload .names == NULL ) {
152+ init_modules_to_reload ();
153+ }
154+
126155 zend_module_entry * module ;
127- for (module_name = MODULES_TO_RELOAD ; * module_name ; module_name ++ ) {
128- if ((module = zend_hash_str_find_ptr (& module_registry , * module_name ,
129- strlen (* module_name )))) {
156+ for (size_t i = 0 ; i < modules_to_reload .count ; i ++ ) {
157+ if ((module = zend_hash_str_find_ptr (& module_registry ,
158+ modules_to_reload .names [i ],
159+ strlen (modules_to_reload .names [i ])))) {
130160 module -> request_shutdown_func (module -> type , module -> module_number );
131161 }
132162 }
@@ -144,6 +174,39 @@ static void frankenphp_worker_request_shutdown() {
144174 zend_set_memory_limit (PG (memory_limit ));
145175}
146176
177+ /* API for extensions to register their modules to reload */
178+ bool frankenphp_register_module_to_reload (const char * module_name ) {
179+ if (module_name == NULL ) {
180+ return false;
181+ }
182+
183+ if (modules_to_reload .names == NULL ) {
184+ init_modules_to_reload ();
185+ }
186+
187+ for (size_t i = 0 ; i < modules_to_reload .count ; i ++ ) {
188+ if (strcmp (modules_to_reload .names [i ], module_name ) == 0 ) {
189+ return true;
190+ }
191+ }
192+
193+ if (modules_to_reload .count >= modules_to_reload .capacity ) {
194+ size_t new_capacity = modules_to_reload .capacity * 2 ;
195+ const char * * new_names =
196+ realloc (modules_to_reload .names , new_capacity * sizeof (char * ));
197+ if (!new_names ) {
198+ return false; // Out of memory
199+ }
200+ modules_to_reload .names = new_names ;
201+ modules_to_reload .capacity = new_capacity ;
202+ }
203+
204+ modules_to_reload .names [modules_to_reload .count ] = strdup (module_name );
205+ modules_to_reload .count ++ ;
206+
207+ return true;
208+ }
209+
147210// shutdown the dummy request that starts the worker script
148211bool frankenphp_shutdown_dummy_request (void ) {
149212 if (SG (server_context ) == NULL ) {
@@ -223,12 +286,17 @@ static int frankenphp_worker_request_startup() {
223286 }
224287 }
225288
226- /* TODO: store the list of modules to reload in a global module variable */
227- const char * * module_name ;
289+ /* Initialize modules to reload if needed */
290+ if (modules_to_reload .names == NULL ) {
291+ init_modules_to_reload ();
292+ }
293+
294+ /* Reload modules */
228295 zend_module_entry * module ;
229- for (module_name = MODULES_TO_RELOAD ; * module_name ; module_name ++ ) {
230- if ((module = zend_hash_str_find_ptr (& module_registry , * module_name ,
231- strlen (* module_name ))) &&
296+ for (size_t i = 0 ; i < modules_to_reload .count ; i ++ ) {
297+ if ((module = zend_hash_str_find_ptr (
298+ & module_registry , modules_to_reload .names [i ],
299+ strlen (modules_to_reload .names [i ]))) &&
232300 module -> request_startup_func ) {
233301 module -> request_startup_func (module -> type , module -> module_number );
234302 }
0 commit comments