33
33
ZEND_TSRMLS_CACHE_DEFINE ()
34
34
#endif
35
35
36
- static const char * MODULES_TO_RELOAD [] = {"filter" , "session" , NULL };
36
+ #define INITIAL_MODULES_CAPACITY 8
37
+
38
+ static const char * MODULES_TO_RELOAD [] = {"filter" , "session" };
39
+ frankenphp_modules_to_reload modules_to_reload = {NULL , 0 , 0 };
37
40
38
41
frankenphp_version frankenphp_get_version () {
39
42
return (frankenphp_version ){
@@ -115,18 +118,42 @@ static void frankenphp_release_temporary_streams() {
115
118
ZEND_HASH_FOREACH_END ();
116
119
}
117
120
121
+ static void init_modules_to_reload (void ) {
122
+ if (modules_to_reload .names != NULL ) {
123
+ return ;
124
+ }
125
+
126
+ size_t length = sizeof MODULES_TO_RELOAD / sizeof * MODULES_TO_RELOAD ;
127
+
128
+ size_t capacity = length > 0 ? length * 2 : INITIAL_MODULES_CAPACITY ;
129
+ modules_to_reload .names = malloc (capacity * sizeof (char * ));
130
+ if (!modules_to_reload .names ) {
131
+ return ; // TODO: handle this as an error
132
+ }
133
+
134
+ for (size_t i = 0 ; i < length ; i ++ ) {
135
+ modules_to_reload .names [i ] = strdup (MODULES_TO_RELOAD [i ]);
136
+ }
137
+
138
+ modules_to_reload .length = length ;
139
+ modules_to_reload .capacity = capacity ;
140
+ }
141
+
118
142
/* Adapted from php_request_shutdown */
119
143
static void frankenphp_worker_request_shutdown () {
120
144
/* Flush all output buffers */
121
145
zend_try { php_output_end_all (); }
122
146
zend_end_try ();
123
147
124
- /* TODO: store the list of modules to reload in a global module variable */
125
- const char * * module_name ;
148
+ init_modules_to_reload ();
149
+
126
150
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 )))) {
151
+ for (size_t i = 0 ; i < modules_to_reload .length ; i ++ ) {
152
+ module =
153
+ zend_hash_str_find_ptr (& module_registry , modules_to_reload .names [i ],
154
+ strlen (modules_to_reload .names [i ]));
155
+
156
+ if (module && module -> request_shutdown_func ) {
130
157
module -> request_shutdown_func (module -> type , module -> module_number );
131
158
}
132
159
}
@@ -144,6 +171,37 @@ static void frankenphp_worker_request_shutdown() {
144
171
zend_set_memory_limit (PG (memory_limit ));
145
172
}
146
173
174
+ /* API for extensions to register their modules to reload */
175
+ bool frankenphp_register_module_to_reload (const char * module_name ) {
176
+ if (module_name == NULL ) {
177
+ return false;
178
+ }
179
+
180
+ init_modules_to_reload ();
181
+
182
+ for (size_t i = 0 ; i < modules_to_reload .length ; i ++ ) {
183
+ if (strcmp (modules_to_reload .names [i ], module_name ) == 0 ) {
184
+ return true;
185
+ }
186
+ }
187
+
188
+ if (modules_to_reload .length >= modules_to_reload .capacity ) {
189
+ size_t new_capacity = modules_to_reload .capacity * 2 ;
190
+ const char * * new_names =
191
+ realloc (modules_to_reload .names , new_capacity * sizeof (char * ));
192
+ if (!new_names ) {
193
+ return false; // Out of memory
194
+ }
195
+ modules_to_reload .names = new_names ;
196
+ modules_to_reload .capacity = new_capacity ;
197
+ }
198
+
199
+ modules_to_reload .names [modules_to_reload .length ] = strdup (module_name );
200
+ modules_to_reload .length ++ ;
201
+
202
+ return true;
203
+ }
204
+
147
205
// shutdown the dummy request that starts the worker script
148
206
bool frankenphp_shutdown_dummy_request (void ) {
149
207
if (SG (server_context ) == NULL ) {
@@ -223,13 +281,15 @@ static int frankenphp_worker_request_startup() {
223
281
}
224
282
}
225
283
226
- /* TODO: store the list of modules to reload in a global module variable */
227
- const char * * module_name ;
284
+ init_modules_to_reload ();
285
+
286
+ /* Reload modules */
228
287
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 ))) &&
232
- module -> request_startup_func ) {
288
+ for (size_t i = 0 ; i < modules_to_reload .length ; i ++ ) {
289
+ module =
290
+ zend_hash_str_find_ptr (& module_registry , modules_to_reload .names [i ],
291
+ strlen (modules_to_reload .names [i ]));
292
+ if (module && module -> request_startup_func ) {
233
293
module -> request_startup_func (module -> type , module -> module_number );
234
294
}
235
295
}
0 commit comments