@@ -40,7 +40,10 @@ static const char HARDCODED_INI[] = "max_execution_time=0\n"
40
40
"max_input_time=-1\n\0" ;
41
41
#endif
42
42
43
+ #define INITIAL_MODULES_CAPACITY 8
44
+
43
45
static const char * MODULES_TO_RELOAD [] = {"filter" , "session" , NULL };
46
+ frankenphp_modules_to_reload modules_to_reload = {NULL , 0 , 0 };
44
47
45
48
frankenphp_version frankenphp_get_version () {
46
49
return (frankenphp_version ){
@@ -130,18 +133,45 @@ static void frankenphp_release_temporary_streams() {
130
133
ZEND_HASH_FOREACH_END ();
131
134
}
132
135
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
+
133
160
/* Adapted from php_request_shutdown */
134
161
static void frankenphp_worker_request_shutdown () {
135
162
/* Flush all output buffers */
136
163
zend_try { php_output_end_all (); }
137
164
zend_end_try ();
138
165
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
+
141
170
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 ])))) {
145
175
module -> request_shutdown_func (module -> type , module -> module_number );
146
176
}
147
177
}
@@ -159,6 +189,38 @@ static void frankenphp_worker_request_shutdown() {
159
189
zend_set_memory_limit (PG (memory_limit ));
160
190
}
161
191
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 = realloc (modules_to_reload .names , new_capacity * sizeof (char * ));
211
+ if (!new_names ) {
212
+ return false; // Out of memory
213
+ }
214
+ modules_to_reload .names = new_names ;
215
+ modules_to_reload .capacity = new_capacity ;
216
+ }
217
+
218
+ modules_to_reload .names [modules_to_reload .count ] = strdup (module_name );
219
+ modules_to_reload .count ++ ;
220
+
221
+ return true;
222
+ }
223
+
162
224
PHPAPI void get_full_env (zval * track_vars_array ) {
163
225
struct go_getfullenv_return full_env = go_getfullenv (thread_index );
164
226
@@ -225,12 +287,17 @@ static int frankenphp_worker_request_startup() {
225
287
frankenphp_server_context * ctx = SG (server_context );
226
288
ctx -> finished = false;
227
289
228
- /* TODO: store the list of modules to reload in a global module variable */
229
- const char * * module_name ;
290
+ /* Initialize modules to reload if needed */
291
+ if (modules_to_reload .names == NULL ) {
292
+ init_modules_to_reload ();
293
+ }
294
+
295
+ /* Reload modules */
230
296
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 )) &&
297
+ for (size_t i = 0 ; i < modules_to_reload .count ; i ++ ) {
298
+ if ((module = zend_hash_str_find_ptr (& module_registry ,
299
+ modules_to_reload .names [i ],
300
+ strlen (modules_to_reload .names [i ]))) &&
234
301
module -> request_startup_func ) {
235
302
module -> request_startup_func (module -> type , module -> module_number );
236
303
}
0 commit comments