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 desc in T::get_metadata().all_properties() {
504                let default_stub = if desc.nullable {
505                    Some("null".into())
506                } else {
507                    None
508                };
509                builder = builder.property(crate::builders::ClassProperty {
510                    name: desc.name.into(),
511                    flags: desc.flags,
512                    default: None,
513                    docs: desc.docs,
514                    ty: Some(desc.ty),
515                    nullable: desc.nullable,
516                    readonly: desc.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#[cfg(feature = "observer")]
579impl<'a> ModuleBuilder<'a> {
580    /// Register a [`ZendExtensionHandler`] and configure which hooks PHP should
581    /// call.
582    ///
583    /// Returns a [`ZendExtensionBuilder`] that exposes three opt-in methods
584    /// (`hook_statements`, `hook_fcalls`, `hook_op_array_compile`). Call
585    /// [`ZendExtensionBuilder::finish`] to return to `ModuleBuilder`.
586    ///
587    /// # Example
588    ///
589    /// ```ignore
590    /// module
591    ///     .zend_extension(|| MyProfiler::new())
592    ///     .hook_statements()
593    ///     .finish()
594    /// ```
595    ///
596    /// # Panics
597    ///
598    /// Panics if called more than once on the same module.
599    ///
600    /// [`ZendExtensionHandler`]: crate::zend::ZendExtensionHandler
601    /// [`ZendExtensionBuilder`]: crate::zend::ZendExtensionBuilder
602    /// [`ZendExtensionBuilder::finish`]: crate::zend::ZendExtensionBuilder::finish
603    pub fn zend_extension<F, H>(
604        self,
605        factory: F,
606    ) -> crate::zend::zend_extension::ZendExtensionBuilder<'a>
607    where
608        F: Fn() -> H + Send + Sync + 'static,
609        H: crate::zend::ZendExtensionHandler,
610    {
611        crate::zend::zend_extension::ZendExtensionBuilder::new(self, factory)
612    }
613}
614
615/// Artifacts from the [`ModuleBuilder`] that should be revisited inside the
616/// extension startup function.
617pub struct ModuleStartup {
618    #[cfg(feature = "observer")]
619    name: String,
620    #[cfg(feature = "observer")]
621    version: String,
622    constants: Vec<(String, Box<dyn IntoConst + Send>)>,
623    classes: Vec<fn() -> ClassBuilder>,
624    interfaces: Vec<fn() -> ClassBuilder>,
625    #[cfg(feature = "enum")]
626    enums: Vec<fn() -> EnumBuilder>,
627}
628
629impl ModuleStartup {
630    /// Completes startup of the module. Should only be called inside the module
631    /// startup function.
632    ///
633    /// # Errors
634    ///
635    /// * Returns an error if a constant could not be registered.
636    ///
637    /// # Panics
638    ///
639    /// * Panics if a class could not be registered.
640    pub fn startup(self, _ty: i32, mod_num: i32) -> Result<()> {
641        for (name, val) in self.constants {
642            val.register_constant(&name, mod_num)?;
643        }
644
645        // Interfaces must be registered before classes so that classes can implement
646        // them
647        self.interfaces.into_iter().map(|c| c()).for_each(|c| {
648            c.register().expect("Failed to build interface");
649        });
650
651        self.classes.into_iter().map(|c| c()).for_each(|c| {
652            c.register().expect("Failed to build class");
653        });
654
655        #[cfg(feature = "enum")]
656        self.enums
657            .into_iter()
658            .map(|builder| builder())
659            .for_each(|e| {
660                e.register().expect("Failed to build enum");
661            });
662
663        // Initialize observer systems if registered
664        #[cfg(feature = "observer")]
665        unsafe {
666            crate::zend::observer::observer_startup();
667            crate::zend::error_observer::error_observer_startup();
668            crate::zend::exception_observer::exception_observer_startup();
669            crate::zend::zend_extension::zend_extension_startup(&self.name, &self.version);
670        }
671
672        Ok(())
673    }
674}
675
676/// A function to be called when the extension is starting up or shutting down.
677pub type StartupShutdownFunc = unsafe extern "C" fn(_type: i32, _module_number: i32) -> i32;
678
679/// A function to be called when `phpinfo();` is called.
680pub type InfoFunc = unsafe extern "C" fn(zend_module: *mut ModuleEntry);
681
682/// Builds a [`ModuleEntry`] and [`ModuleStartup`] from a [`ModuleBuilder`].
683/// This is the entry point for the module to be registered with PHP.
684impl TryFrom<ModuleBuilder<'_>> for (ModuleEntry, ModuleStartup) {
685    type Error = crate::error::Error;
686
687    fn try_from(builder: ModuleBuilder) -> Result<Self, Self::Error> {
688        let mut functions = builder
689            .functions
690            .into_iter()
691            .map(FunctionBuilder::build)
692            .collect::<Result<Vec<_>>>()?;
693        functions.push(FunctionEntry::end());
694        let functions = Box::into_raw(functions.into_boxed_slice()) as *const FunctionEntry;
695
696        #[cfg(feature = "observer")]
697        let ext_name = builder.name.clone();
698        #[cfg(feature = "observer")]
699        let ext_version = builder.version.clone();
700
701        let name = CString::new(builder.name)?.into_raw();
702        let version = CString::new(builder.version)?.into_raw();
703
704        let startup = ModuleStartup {
705            #[cfg(feature = "observer")]
706            name: ext_name,
707            #[cfg(feature = "observer")]
708            version: ext_version,
709            constants: builder
710                .constants
711                .into_iter()
712                .map(|(n, v, _)| (n, v))
713                .collect(),
714            classes: builder.classes,
715            interfaces: builder.interfaces,
716            #[cfg(feature = "enum")]
717            enums: builder.enums,
718        };
719
720        #[cfg(not(php_zts))]
721        let module_entry = ModuleEntry {
722            size: mem::size_of::<ModuleEntry>().try_into()?,
723            zend_api: ZEND_MODULE_API_NO,
724            zend_debug: u8::from(PHP_DEBUG),
725            zts: u8::from(PHP_ZTS),
726            ini_entry: ptr::null(),
727            deps: ptr::null(),
728            name,
729            functions,
730            module_startup_func: builder.startup_func,
731            module_shutdown_func: builder.shutdown_func,
732            request_startup_func: builder.request_startup_func,
733            request_shutdown_func: builder.request_shutdown_func,
734            info_func: builder.info_func,
735            version,
736            globals_size: builder.globals_size,
737            globals_ptr: builder.globals_ptr,
738            globals_ctor: builder.globals_ctor,
739            globals_dtor: builder.globals_dtor,
740            post_deactivate_func: builder.post_deactivate_func,
741            module_started: 0,
742            type_: 0,
743            handle: ptr::null_mut(),
744            module_number: 0,
745            build_id: unsafe { ext_php_rs_php_build_id() },
746        };
747
748        #[cfg(php_zts)]
749        let module_entry = ModuleEntry {
750            size: mem::size_of::<ModuleEntry>().try_into()?,
751            zend_api: ZEND_MODULE_API_NO,
752            zend_debug: u8::from(PHP_DEBUG),
753            zts: u8::from(PHP_ZTS),
754            ini_entry: ptr::null(),
755            deps: ptr::null(),
756            name,
757            functions,
758            module_startup_func: builder.startup_func,
759            module_shutdown_func: builder.shutdown_func,
760            request_startup_func: builder.request_startup_func,
761            request_shutdown_func: builder.request_shutdown_func,
762            info_func: builder.info_func,
763            version,
764            globals_size: builder.globals_size,
765            globals_id_ptr: builder.globals_id_ptr,
766            globals_ctor: builder.globals_ctor,
767            globals_dtor: builder.globals_dtor,
768            post_deactivate_func: builder.post_deactivate_func,
769            module_started: 0,
770            type_: 0,
771            handle: ptr::null_mut(),
772            module_number: 0,
773            build_id: unsafe { ext_php_rs_php_build_id() },
774        };
775
776        Ok((module_entry, startup))
777    }
778}
779
780#[cfg(test)]
781mod tests {
782    use crate::test::{
783        test_deactivate_function, test_function, test_info_function, test_startup_shutdown_function,
784    };
785
786    use super::*;
787
788    #[test]
789    fn test_new() {
790        let builder = ModuleBuilder::new("test", "1.0");
791        assert_eq!(builder.name, "test");
792        assert_eq!(builder.version, "1.0");
793        assert!(builder.functions.is_empty());
794        assert!(builder.constants.is_empty());
795        assert!(builder.classes.is_empty());
796        assert!(builder.interfaces.is_empty());
797        assert!(builder.startup_func.is_none());
798        assert!(builder.shutdown_func.is_none());
799        assert!(builder.request_startup_func.is_none());
800        assert!(builder.request_shutdown_func.is_none());
801        assert!(builder.post_deactivate_func.is_none());
802        assert!(builder.info_func.is_none());
803        #[cfg(feature = "enum")]
804        assert!(builder.enums.is_empty());
805    }
806
807    #[test]
808    fn test_name() {
809        let builder = ModuleBuilder::new("test", "1.0").name("new_test");
810        assert_eq!(builder.name, "new_test");
811    }
812
813    #[test]
814    fn test_version() {
815        let builder = ModuleBuilder::new("test", "1.0").version("2.0");
816        assert_eq!(builder.version, "2.0");
817    }
818
819    #[test]
820    fn test_startup_function() {
821        let builder =
822            ModuleBuilder::new("test", "1.0").startup_function(test_startup_shutdown_function);
823        assert!(builder.startup_func.is_some());
824    }
825
826    #[test]
827    fn test_shutdown_function() {
828        let builder =
829            ModuleBuilder::new("test", "1.0").shutdown_function(test_startup_shutdown_function);
830        assert!(builder.shutdown_func.is_some());
831    }
832
833    #[test]
834    fn test_request_startup_function() {
835        let builder = ModuleBuilder::new("test", "1.0")
836            .request_startup_function(test_startup_shutdown_function);
837        assert!(builder.request_startup_func.is_some());
838    }
839
840    #[test]
841    fn test_request_shutdown_function() {
842        let builder = ModuleBuilder::new("test", "1.0")
843            .request_shutdown_function(test_startup_shutdown_function);
844        assert!(builder.request_shutdown_func.is_some());
845    }
846
847    #[test]
848    fn test_set_post_deactivate_function() {
849        let builder =
850            ModuleBuilder::new("test", "1.0").post_deactivate_function(test_deactivate_function);
851        assert!(builder.post_deactivate_func.is_some());
852    }
853
854    #[test]
855    fn test_set_info_function() {
856        let builder = ModuleBuilder::new("test", "1.0").info_function(test_info_function);
857        assert!(builder.info_func.is_some());
858    }
859
860    #[test]
861    fn test_add_function() {
862        let builder =
863            ModuleBuilder::new("test", "1.0").function(FunctionBuilder::new("test", test_function));
864        assert_eq!(builder.functions.len(), 1);
865    }
866
867    #[test]
868    #[cfg(feature = "embed")]
869    fn test_add_constant() {
870        let builder =
871            ModuleBuilder::new("test", "1.0").constant(("TEST_CONST", 42, DocComments::default()));
872        assert_eq!(builder.constants.len(), 1);
873        assert_eq!(builder.constants[0].0, "TEST_CONST");
874        // TODO: Check if the value is 42
875        assert_eq!(builder.constants[0].2, DocComments::default());
876    }
877}