Skip to main content

ext_php_rs/builders/
module.rs

1use std::{convert::TryFrom, ffi::CString, mem, ptr};
2
3use super::{ClassBuilder, FunctionBuilder};
4use crate::{
5    PHP_DEBUG, PHP_ZTS,
6    class::RegisteredClass,
7    constant::IntoConst,
8    describe::DocComments,
9    error::Result,
10    ffi::{ZEND_MODULE_API_NO, ext_php_rs_php_build_id},
11    flags::ClassFlags,
12    zend::{FunctionEntry, ModuleEntry, ModuleGlobal, ModuleGlobals},
13};
14#[cfg(feature = "enum")]
15use crate::{builders::enum_builder::EnumBuilder, enum_::RegisteredEnum};
16
17/// Builds a Zend module extension to be registered with PHP. Must be called
18/// from within an external function called `get_module`, returning a mutable
19/// pointer to a `ModuleEntry`.
20///
21/// ```rust,no_run
22/// use ext_php_rs::{
23///     builders::ModuleBuilder,
24///     zend::{ModuleEntry, StaticModuleEntry},
25///     info_table_start, info_table_end, info_table_row
26/// };
27///
28/// #[unsafe(no_mangle)]
29/// pub extern "C" fn php_module_info(_module: *mut ModuleEntry) {
30///     info_table_start!();
31///     info_table_row!("column 1", "column 2");
32///     info_table_end!();
33/// }
34///
35/// #[unsafe(no_mangle)]
36/// pub extern "C" fn get_module() -> *mut ModuleEntry {
37///     static MODULE: StaticModuleEntry = StaticModuleEntry::new();
38///     MODULE.get_or_init(|| {
39///         let (entry, _) = ModuleBuilder::new("ext-name", "ext-version")
40///             .info_function(php_module_info)
41///             .try_into()
42///             .unwrap();
43///         entry
44///     })
45/// }
46/// ```
47#[must_use]
48#[derive(Debug)]
49pub struct ModuleBuilder<'a> {
50    pub(crate) name: String,
51    pub(crate) version: String,
52    pub(crate) functions: Vec<FunctionBuilder<'a>>,
53    pub(crate) constants: Vec<(String, Box<dyn IntoConst + Send>, DocComments)>,
54    pub(crate) classes: Vec<fn() -> ClassBuilder>,
55    pub(crate) interfaces: Vec<fn() -> ClassBuilder>,
56    #[cfg(feature = "enum")]
57    pub(crate) enums: Vec<fn() -> EnumBuilder>,
58    startup_func: Option<StartupShutdownFunc>,
59    shutdown_func: Option<StartupShutdownFunc>,
60    request_startup_func: Option<StartupShutdownFunc>,
61    request_shutdown_func: Option<StartupShutdownFunc>,
62    post_deactivate_func: Option<unsafe extern "C" fn() -> i32>,
63    info_func: Option<InfoFunc>,
64    globals_size: usize,
65    #[cfg(php_zts)]
66    globals_id_ptr: *mut i32,
67    #[cfg(not(php_zts))]
68    globals_ptr: *mut std::ffi::c_void,
69    globals_ctor: Option<unsafe extern "C" fn(*mut std::ffi::c_void)>,
70    globals_dtor: Option<unsafe extern "C" fn(*mut std::ffi::c_void)>,
71}
72
73impl Default for ModuleBuilder<'_> {
74    fn default() -> Self {
75        Self {
76            name: String::new(),
77            version: String::new(),
78            functions: vec![],
79            constants: vec![],
80            classes: vec![],
81            interfaces: vec![],
82            #[cfg(feature = "enum")]
83            enums: vec![],
84            startup_func: None,
85            shutdown_func: None,
86            request_startup_func: None,
87            request_shutdown_func: None,
88            post_deactivate_func: None,
89            info_func: None,
90            globals_size: 0,
91            #[cfg(php_zts)]
92            globals_id_ptr: ptr::null_mut(),
93            #[cfg(not(php_zts))]
94            globals_ptr: ptr::null_mut(),
95            globals_ctor: None,
96            globals_dtor: None,
97        }
98    }
99}
100
101impl ModuleBuilder<'_> {
102    /// Creates a new module builder with a given name and version.
103    ///
104    /// # Arguments
105    ///
106    /// * `name` - The name of the extension.
107    /// * `version` - The current version of the extension.
108    pub fn new(name: impl Into<String>, version: impl Into<String>) -> Self {
109        Self {
110            name: name.into(),
111            version: version.into(),
112            ..Default::default()
113        }
114    }
115
116    /// Overrides module name.
117    ///
118    /// # Arguments
119    ///
120    /// * `name` - The name of the extension.
121    pub fn name(mut self, name: impl Into<String>) -> Self {
122        self.name = name.into();
123        self
124    }
125
126    /// Overrides module version.
127    ///
128    /// # Arguments
129    ///
130    /// * `version` - The current version of the extension.
131    pub fn version(mut self, version: impl Into<String>) -> Self {
132        self.version = version.into();
133        self
134    }
135
136    /// Sets the startup function for the extension.
137    ///
138    /// # Arguments
139    ///
140    /// * `func` - The function to be called on startup.
141    pub fn startup_function(mut self, func: StartupShutdownFunc) -> Self {
142        self.startup_func = Some(func);
143        self
144    }
145
146    /// Sets the shutdown function for the extension.
147    ///
148    /// # Arguments
149    ///
150    /// * `func` - The function to be called on shutdown.
151    pub fn shutdown_function(mut self, func: StartupShutdownFunc) -> Self {
152        self.shutdown_func = Some(func);
153        self
154    }
155
156    /// Sets the request startup function for the extension.
157    ///
158    /// # Arguments
159    ///
160    /// * `func` - The function to be called when startup is requested.
161    pub fn request_startup_function(mut self, func: StartupShutdownFunc) -> Self {
162        self.request_startup_func = Some(func);
163        self
164    }
165
166    /// Sets the request shutdown function for the extension.
167    ///
168    /// # Arguments
169    ///
170    /// * `func` - The function to be called when shutdown is requested.
171    pub fn request_shutdown_function(mut self, func: StartupShutdownFunc) -> Self {
172        self.request_shutdown_func = Some(func);
173        self
174    }
175
176    /// Sets the post request shutdown function for the extension.
177    ///
178    /// This function can be useful if you need to do any final cleanup at the
179    /// very end of a request, after all other resources have been released. For
180    /// example, if your extension creates any persistent resources that last
181    /// beyond a single request, you could use this function to clean those up.
182    /// # Arguments
183    ///
184    /// * `func` - The function to be called when shutdown is requested.
185    pub fn post_deactivate_function(mut self, func: unsafe extern "C" fn() -> i32) -> Self {
186        self.post_deactivate_func = Some(func);
187        self
188    }
189
190    /// Sets the extension information function for the extension.
191    ///
192    /// # Arguments
193    ///
194    /// * `func` - The function to be called to retrieve the information about
195    ///   the extension.
196    pub fn info_function(mut self, func: InfoFunc) -> Self {
197        self.info_func = Some(func);
198        self
199    }
200
201    /// Registers a module globals struct with this extension.
202    ///
203    /// PHP will allocate per-thread storage (ZTS) or use the static's inline
204    /// storage (non-ZTS), calling GINIT/GSHUTDOWN callbacks automatically.
205    ///
206    /// Only one globals struct per module is supported (PHP limitation).
207    /// Calling this a second time will overwrite the previous registration.
208    ///
209    /// # Arguments
210    ///
211    /// * `handle` - A static [`ModuleGlobals`] that will hold the globals.
212    ///
213    /// # Examples
214    ///
215    /// ```ignore
216    /// use ext_php_rs::prelude::*;
217    /// use ext_php_rs::zend::{ModuleGlobal, ModuleGlobals};
218    ///
219    /// #[derive(Default)]
220    /// struct MyGlobals { counter: i64 }
221    /// impl ModuleGlobal for MyGlobals {}
222    ///
223    /// static MY_GLOBALS: ModuleGlobals<MyGlobals> = ModuleGlobals::new();
224    ///
225    /// #[php_module]
226    /// pub fn module(module: ModuleBuilder) -> ModuleBuilder {
227    ///     module.globals(&MY_GLOBALS)
228    /// }
229    /// ```
230    pub fn globals<T: ModuleGlobal>(mut self, handle: &'static ModuleGlobals<T>) -> Self {
231        use crate::zend::module_globals::{ginit_callback, gshutdown_callback};
232
233        self.globals_size = std::mem::size_of::<T>();
234        #[cfg(php_zts)]
235        {
236            self.globals_id_ptr = handle.id_ptr();
237        }
238        #[cfg(not(php_zts))]
239        {
240            self.globals_ptr = handle.data_ptr();
241        }
242        self.globals_ctor = Some(ginit_callback::<T>);
243        self.globals_dtor = Some(gshutdown_callback::<T>);
244        self
245    }
246
247    /// Registers a function call observer for profiling or tracing.
248    ///
249    /// The factory function is called once globally during MINIT to create
250    /// a singleton observer instance shared across all requests and threads.
251    /// The observer must be `Send + Sync` as it may be accessed concurrently
252    /// in ZTS builds.
253    ///
254    /// # Arguments
255    ///
256    /// * `factory` - A function that creates an observer instance
257    ///
258    /// # Example
259    ///
260    /// ```ignore
261    /// use ext_php_rs::prelude::*;
262    /// use ext_php_rs::zend::{FcallObserver, FcallInfo, ExecuteData};
263    /// use ext_php_rs::types::Zval;
264    ///
265    /// struct MyProfiler;
266    ///
267    /// impl FcallObserver for MyProfiler {
268    ///     fn should_observe(&self, info: &FcallInfo) -> bool {
269    ///         !info.is_internal
270    ///     }
271    ///     fn begin(&self, _: &ExecuteData) {}
272    ///     fn end(&self, _: &ExecuteData, _: Option<&Zval>) {}
273    /// }
274    ///
275    /// #[php_module]
276    /// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
277    ///     module.fcall_observer(|| MyProfiler)
278    /// }
279    /// ```
280    ///
281    /// # Panics
282    ///
283    /// Panics if called more than once on the same module.
284    #[cfg(feature = "observer")]
285    pub fn fcall_observer<F, O>(self, factory: F) -> Self
286    where
287        F: Fn() -> O + Send + Sync + 'static,
288        O: crate::zend::FcallObserver + Send + Sync,
289    {
290        let boxed_factory: Box<
291            dyn Fn() -> Box<dyn crate::zend::FcallObserver + Send + Sync> + Send + Sync,
292        > = Box::new(move || Box::new(factory()));
293        crate::zend::observer::register_fcall_observer_factory(boxed_factory);
294        self
295    }
296
297    /// Registers an error observer for monitoring PHP errors.
298    ///
299    /// The factory function is called once during MINIT to create
300    /// a singleton observer instance shared across all requests.
301    /// The observer must be `Send + Sync` for ZTS builds.
302    ///
303    /// # Arguments
304    ///
305    /// * `factory` - A function that creates an observer instance
306    ///
307    /// # Example
308    ///
309    /// ```ignore
310    /// use ext_php_rs::prelude::*;
311    ///
312    /// struct MyErrorLogger;
313    ///
314    /// impl ErrorObserver for MyErrorLogger {
315    ///     fn should_observe(&self, error_type: ErrorType) -> bool {
316    ///         ErrorType::FATAL.contains(error_type)
317    ///     }
318    ///
319    ///     fn on_error(&self, error: &ErrorInfo) {
320    ///         eprintln!("[{}:{}] {}",
321    ///             error.filename.unwrap_or("<unknown>"),
322    ///             error.lineno,
323    ///             error.message
324    ///         );
325    ///     }
326    /// }
327    ///
328    /// #[php_module]
329    /// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
330    ///     module.error_observer(MyErrorLogger)
331    /// }
332    /// ```
333    ///
334    /// # Panics
335    ///
336    /// Panics if called more than once on the same module.
337    #[cfg(feature = "observer")]
338    pub fn error_observer<F, O>(self, factory: F) -> Self
339    where
340        F: Fn() -> O + Send + Sync + 'static,
341        O: crate::zend::ErrorObserver + Send + Sync,
342    {
343        let boxed_factory: Box<
344            dyn Fn() -> Box<dyn crate::zend::ErrorObserver + Send + Sync> + Send + Sync,
345        > = Box::new(move || Box::new(factory()));
346        crate::zend::error_observer::register_error_observer_factory(boxed_factory);
347        self
348    }
349
350    /// Registers an exception observer for monitoring thrown PHP exceptions.
351    ///
352    /// The factory function is called once during MINIT to create
353    /// a singleton observer instance shared across all requests.
354    /// The observer must be `Send + Sync` for ZTS builds.
355    ///
356    /// The observer is called at throw time, before any catch blocks are
357    /// evaluated.
358    ///
359    /// # Arguments
360    ///
361    /// * `factory` - A function that creates an observer instance
362    ///
363    /// # Example
364    ///
365    /// ```ignore
366    /// use ext_php_rs::prelude::*;
367    ///
368    /// struct MyExceptionLogger;
369    ///
370    /// impl ExceptionObserver for MyExceptionLogger {
371    ///     fn on_exception(&self, exception: &ExceptionInfo) {
372    ///         eprintln!("[EXCEPTION] {}: {} at {}:{}",
373    ///             exception.class_name,
374    ///             exception.message.as_deref().unwrap_or("<no message>"),
375    ///             exception.file.as_deref().unwrap_or("<unknown>"),
376    ///             exception.line
377    ///         );
378    ///     }
379    /// }
380    ///
381    /// #[php_module]
382    /// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
383    ///     module.exception_observer(|| MyExceptionLogger)
384    /// }
385    /// ```
386    ///
387    /// # Panics
388    ///
389    /// Panics if called more than once on the same module.
390    #[cfg(feature = "observer")]
391    pub fn exception_observer<F, O>(self, factory: F) -> Self
392    where
393        F: Fn() -> O + Send + Sync + 'static,
394        O: crate::zend::ExceptionObserver + Send + Sync,
395    {
396        let boxed_factory: Box<
397            dyn Fn() -> Box<dyn crate::zend::ExceptionObserver + Send + Sync> + Send + Sync,
398        > = Box::new(move || Box::new(factory()));
399        crate::zend::exception_observer::register_exception_observer_factory(boxed_factory);
400        self
401    }
402
403    /// Adds a function to the extension.
404    ///
405    /// # Arguments
406    ///
407    /// * `func` - The function to be added to the extension.
408    pub fn function(mut self, func: FunctionBuilder<'static>) -> Self {
409        self.functions.push(func);
410        self
411    }
412
413    /// Adds a constant to the extension.
414    ///
415    /// # Arguments
416    ///
417    /// * `const` - Tuple containing the name, value and doc comments for the
418    ///   constant. This is a tuple to support the [`wrap_constant`] macro.
419    ///
420    /// [`wrap_constant`]: crate::wrap_constant
421    pub fn constant(
422        mut self,
423        r#const: (&str, impl IntoConst + Send + 'static, DocComments),
424    ) -> Self {
425        let (name, val, docs) = r#const;
426        self.constants.push((
427            name.into(),
428            Box::new(val) as Box<dyn IntoConst + Send>,
429            docs,
430        ));
431        self
432    }
433
434    /// Adds a interface to the extension.
435    ///
436    /// # Panics
437    ///
438    /// * Panics if a constant could not be registered.
439    pub fn interface<T: RegisteredClass>(mut self) -> Self {
440        self.interfaces.push(|| {
441            let mut builder = ClassBuilder::new(T::CLASS_NAME);
442            for (method, flags) in T::method_builders() {
443                builder = builder.method(method, flags);
444            }
445            for interface in T::IMPLEMENTS {
446                builder = builder.implements(*interface);
447            }
448            for (name, value, docs) in T::constants() {
449                builder = builder
450                    .dyn_constant(*name, *value, docs)
451                    .expect("Failed to register constant");
452            }
453
454            if let Some(modifier) = T::BUILDER_MODIFIER {
455                builder = modifier(builder);
456            }
457
458            builder = builder.flags(ClassFlags::Interface);
459            // Note: interfaces should NOT have object_override because they cannot be
460            // instantiated
461            builder
462                .registration(|ce| {
463                    T::get_metadata().set_ce(ce);
464                })
465                .docs(T::DOC_COMMENTS)
466        });
467        self
468    }
469
470    /// Adds a class to the extension.
471    ///
472    /// # Panics
473    ///
474    /// * Panics if a constant could not be registered.
475    pub fn class<T: RegisteredClass>(mut self) -> Self {
476        self.classes.push(|| {
477            let mut builder = ClassBuilder::new(T::CLASS_NAME);
478            for (method, flags) in T::method_builders() {
479                builder = builder.method(method, flags);
480            }
481            // Methods from #[php_impl_interface] trait implementations.
482            // Uses the inventory crate for cross-crate method discovery.
483            for (method, flags) in T::interface_method_implementations() {
484                builder = builder.method(method, flags);
485            }
486            if let Some(parent) = T::EXTENDS {
487                builder = builder.extends(parent);
488            }
489            // Interfaces declared via #[php(implements(...))] attribute
490            for interface in T::IMPLEMENTS {
491                builder = builder.implements(*interface);
492            }
493            // Interfaces from #[php_impl_interface] trait implementations.
494            // Uses the inventory crate for cross-crate interface discovery.
495            for interface in T::interface_implementations() {
496                builder = builder.implements(interface);
497            }
498            for (name, value, docs) in T::constants() {
499                builder = builder
500                    .dyn_constant(*name, *value, docs)
501                    .expect("Failed to register constant");
502            }
503            for (name, prop_info) in T::get_properties() {
504                let default_stub = if prop_info.nullable {
505                    Some("null".into())
506                } else {
507                    None
508                };
509                builder = builder.property(crate::builders::ClassProperty {
510                    name: name.into(),
511                    flags: prop_info.flags,
512                    default: None,
513                    docs: prop_info.docs,
514                    ty: Some(prop_info.ty),
515                    nullable: prop_info.nullable,
516                    readonly: prop_info.readonly,
517                    default_stub,
518                });
519            }
520            for (name, flags, default, docs) in T::static_properties() {
521                let default_stub = default.map(crate::convert::IntoZvalDyn::stub_value);
522                let default_fn = default.map(|v| {
523                    Box::new(move || v.as_zval(true))
524                        as Box<dyn FnOnce() -> crate::error::Result<crate::types::Zval>>
525                });
526                builder = builder.property(crate::builders::ClassProperty {
527                    name: (*name).into(),
528                    flags: *flags,
529                    default: default_fn,
530                    docs,
531                    ty: None,
532                    nullable: false,
533                    readonly: false,
534                    default_stub,
535                });
536            }
537            if let Some(modifier) = T::BUILDER_MODIFIER {
538                builder = modifier(builder);
539            }
540
541            builder
542                .flags(T::FLAGS)
543                .object_override::<T>()
544                .registration(|ce| {
545                    T::get_metadata().set_ce(ce);
546                })
547                .docs(T::DOC_COMMENTS)
548        });
549        self
550    }
551
552    /// Adds an enum to the extension.
553    #[cfg(feature = "enum")]
554    pub fn enumeration<T>(mut self) -> Self
555    where
556        T: RegisteredClass + RegisteredEnum,
557    {
558        self.enums.push(|| {
559            let mut builder = EnumBuilder::new(T::CLASS_NAME);
560            for case in T::CASES {
561                builder = builder.case(case);
562            }
563            for (method, flags) in T::method_builders() {
564                builder = builder.method(method, flags);
565            }
566
567            builder
568                .registration(|ce| {
569                    T::get_metadata().set_ce(ce);
570                })
571                .docs(T::DOC_COMMENTS)
572        });
573
574        self
575    }
576}
577
578/// Artifacts from the [`ModuleBuilder`] that should be revisited inside the
579/// extension startup function.
580pub struct ModuleStartup {
581    constants: Vec<(String, Box<dyn IntoConst + Send>)>,
582    classes: Vec<fn() -> ClassBuilder>,
583    interfaces: Vec<fn() -> ClassBuilder>,
584    #[cfg(feature = "enum")]
585    enums: Vec<fn() -> EnumBuilder>,
586}
587
588impl ModuleStartup {
589    /// Completes startup of the module. Should only be called inside the module
590    /// startup function.
591    ///
592    /// # Errors
593    ///
594    /// * Returns an error if a constant could not be registered.
595    ///
596    /// # Panics
597    ///
598    /// * Panics if a class could not be registered.
599    pub fn startup(self, _ty: i32, mod_num: i32) -> Result<()> {
600        for (name, val) in self.constants {
601            val.register_constant(&name, mod_num)?;
602        }
603
604        // Interfaces must be registered before classes so that classes can implement
605        // them
606        self.interfaces.into_iter().map(|c| c()).for_each(|c| {
607            c.register().expect("Failed to build interface");
608        });
609
610        self.classes.into_iter().map(|c| c()).for_each(|c| {
611            c.register().expect("Failed to build class");
612        });
613
614        #[cfg(feature = "enum")]
615        self.enums
616            .into_iter()
617            .map(|builder| builder())
618            .for_each(|e| {
619                e.register().expect("Failed to build enum");
620            });
621
622        // Initialize observer systems if registered
623        #[cfg(feature = "observer")]
624        unsafe {
625            crate::zend::observer::observer_startup();
626            crate::zend::error_observer::error_observer_startup();
627            crate::zend::exception_observer::exception_observer_startup();
628        }
629
630        Ok(())
631    }
632}
633
634/// A function to be called when the extension is starting up or shutting down.
635pub type StartupShutdownFunc = unsafe extern "C" fn(_type: i32, _module_number: i32) -> i32;
636
637/// A function to be called when `phpinfo();` is called.
638pub type InfoFunc = unsafe extern "C" fn(zend_module: *mut ModuleEntry);
639
640/// Builds a [`ModuleEntry`] and [`ModuleStartup`] from a [`ModuleBuilder`].
641/// This is the entry point for the module to be registered with PHP.
642impl TryFrom<ModuleBuilder<'_>> for (ModuleEntry, ModuleStartup) {
643    type Error = crate::error::Error;
644
645    fn try_from(builder: ModuleBuilder) -> Result<Self, Self::Error> {
646        let mut functions = builder
647            .functions
648            .into_iter()
649            .map(FunctionBuilder::build)
650            .collect::<Result<Vec<_>>>()?;
651        functions.push(FunctionEntry::end());
652        let functions = Box::into_raw(functions.into_boxed_slice()) as *const FunctionEntry;
653
654        let name = CString::new(builder.name)?.into_raw();
655        let version = CString::new(builder.version)?.into_raw();
656
657        let startup = ModuleStartup {
658            constants: builder
659                .constants
660                .into_iter()
661                .map(|(n, v, _)| (n, v))
662                .collect(),
663            classes: builder.classes,
664            interfaces: builder.interfaces,
665            #[cfg(feature = "enum")]
666            enums: builder.enums,
667        };
668
669        #[cfg(not(php_zts))]
670        let module_entry = ModuleEntry {
671            size: mem::size_of::<ModuleEntry>().try_into()?,
672            zend_api: ZEND_MODULE_API_NO,
673            zend_debug: u8::from(PHP_DEBUG),
674            zts: u8::from(PHP_ZTS),
675            ini_entry: ptr::null(),
676            deps: ptr::null(),
677            name,
678            functions,
679            module_startup_func: builder.startup_func,
680            module_shutdown_func: builder.shutdown_func,
681            request_startup_func: builder.request_startup_func,
682            request_shutdown_func: builder.request_shutdown_func,
683            info_func: builder.info_func,
684            version,
685            globals_size: builder.globals_size,
686            globals_ptr: builder.globals_ptr,
687            globals_ctor: builder.globals_ctor,
688            globals_dtor: builder.globals_dtor,
689            post_deactivate_func: builder.post_deactivate_func,
690            module_started: 0,
691            type_: 0,
692            handle: ptr::null_mut(),
693            module_number: 0,
694            build_id: unsafe { ext_php_rs_php_build_id() },
695        };
696
697        #[cfg(php_zts)]
698        let module_entry = ModuleEntry {
699            size: mem::size_of::<ModuleEntry>().try_into()?,
700            zend_api: ZEND_MODULE_API_NO,
701            zend_debug: u8::from(PHP_DEBUG),
702            zts: u8::from(PHP_ZTS),
703            ini_entry: ptr::null(),
704            deps: ptr::null(),
705            name,
706            functions,
707            module_startup_func: builder.startup_func,
708            module_shutdown_func: builder.shutdown_func,
709            request_startup_func: builder.request_startup_func,
710            request_shutdown_func: builder.request_shutdown_func,
711            info_func: builder.info_func,
712            version,
713            globals_size: builder.globals_size,
714            globals_id_ptr: builder.globals_id_ptr,
715            globals_ctor: builder.globals_ctor,
716            globals_dtor: builder.globals_dtor,
717            post_deactivate_func: builder.post_deactivate_func,
718            module_started: 0,
719            type_: 0,
720            handle: ptr::null_mut(),
721            module_number: 0,
722            build_id: unsafe { ext_php_rs_php_build_id() },
723        };
724
725        Ok((module_entry, startup))
726    }
727}
728
729#[cfg(test)]
730mod tests {
731    use crate::test::{
732        test_deactivate_function, test_function, test_info_function, test_startup_shutdown_function,
733    };
734
735    use super::*;
736
737    #[test]
738    fn test_new() {
739        let builder = ModuleBuilder::new("test", "1.0");
740        assert_eq!(builder.name, "test");
741        assert_eq!(builder.version, "1.0");
742        assert!(builder.functions.is_empty());
743        assert!(builder.constants.is_empty());
744        assert!(builder.classes.is_empty());
745        assert!(builder.interfaces.is_empty());
746        assert!(builder.startup_func.is_none());
747        assert!(builder.shutdown_func.is_none());
748        assert!(builder.request_startup_func.is_none());
749        assert!(builder.request_shutdown_func.is_none());
750        assert!(builder.post_deactivate_func.is_none());
751        assert!(builder.info_func.is_none());
752        #[cfg(feature = "enum")]
753        assert!(builder.enums.is_empty());
754    }
755
756    #[test]
757    fn test_name() {
758        let builder = ModuleBuilder::new("test", "1.0").name("new_test");
759        assert_eq!(builder.name, "new_test");
760    }
761
762    #[test]
763    fn test_version() {
764        let builder = ModuleBuilder::new("test", "1.0").version("2.0");
765        assert_eq!(builder.version, "2.0");
766    }
767
768    #[test]
769    fn test_startup_function() {
770        let builder =
771            ModuleBuilder::new("test", "1.0").startup_function(test_startup_shutdown_function);
772        assert!(builder.startup_func.is_some());
773    }
774
775    #[test]
776    fn test_shutdown_function() {
777        let builder =
778            ModuleBuilder::new("test", "1.0").shutdown_function(test_startup_shutdown_function);
779        assert!(builder.shutdown_func.is_some());
780    }
781
782    #[test]
783    fn test_request_startup_function() {
784        let builder = ModuleBuilder::new("test", "1.0")
785            .request_startup_function(test_startup_shutdown_function);
786        assert!(builder.request_startup_func.is_some());
787    }
788
789    #[test]
790    fn test_request_shutdown_function() {
791        let builder = ModuleBuilder::new("test", "1.0")
792            .request_shutdown_function(test_startup_shutdown_function);
793        assert!(builder.request_shutdown_func.is_some());
794    }
795
796    #[test]
797    fn test_set_post_deactivate_function() {
798        let builder =
799            ModuleBuilder::new("test", "1.0").post_deactivate_function(test_deactivate_function);
800        assert!(builder.post_deactivate_func.is_some());
801    }
802
803    #[test]
804    fn test_set_info_function() {
805        let builder = ModuleBuilder::new("test", "1.0").info_function(test_info_function);
806        assert!(builder.info_func.is_some());
807    }
808
809    #[test]
810    fn test_add_function() {
811        let builder =
812            ModuleBuilder::new("test", "1.0").function(FunctionBuilder::new("test", test_function));
813        assert_eq!(builder.functions.len(), 1);
814    }
815
816    #[test]
817    #[cfg(feature = "embed")]
818    fn test_add_constant() {
819        let builder =
820            ModuleBuilder::new("test", "1.0").constant(("TEST_CONST", 42, DocComments::default()));
821        assert_eq!(builder.constants.len(), 1);
822        assert_eq!(builder.constants[0].0, "TEST_CONST");
823        // TODO: Check if the value is 42
824        assert_eq!(builder.constants[0].2, DocComments::default());
825    }
826}