ext_php_rs/zend/
mod.rs

1//! Types used to interact with the Zend engine.
2
3mod _type;
4pub mod ce;
5mod class;
6mod ex;
7mod function;
8mod globals;
9mod handlers;
10mod ini_entry_def;
11mod linked_list;
12mod module;
13mod streams;
14mod try_catch;
15
16use crate::{
17    error::Result,
18    ffi::{php_printf, sapi_module},
19};
20use std::ffi::CString;
21
22pub use _type::ZendType;
23pub use class::ClassEntry;
24pub use ex::ExecuteData;
25pub use function::Function;
26pub use function::FunctionEntry;
27pub use globals::ExecutorGlobals;
28pub use globals::FileGlobals;
29pub use globals::ProcessGlobals;
30pub use globals::SapiGlobals;
31pub use globals::SapiHeader;
32pub use globals::SapiHeaders;
33pub use globals::SapiModule;
34pub use handlers::ZendObjectHandlers;
35pub use ini_entry_def::IniEntryDef;
36pub use linked_list::ZendLinkedList;
37pub use module::ModuleEntry;
38pub use streams::*;
39#[cfg(feature = "embed")]
40pub(crate) use try_catch::panic_wrapper;
41pub use try_catch::{bailout, try_catch, try_catch_first, CatchError};
42
43// Used as the format string for `php_printf`.
44const FORMAT_STR: &[u8] = b"%s\0";
45
46/// Prints to stdout using the `php_printf` function.
47///
48/// Also see the [`php_print`] and [`php_println`] macros.
49///
50/// # Arguments
51///
52/// * message - The message to print to stdout.
53///
54/// # Errors
55///
56/// * If the message could not be converted to a [`CString`].
57pub fn printf(message: &str) -> Result<()> {
58    let message = CString::new(message)?;
59    unsafe {
60        php_printf(FORMAT_STR.as_ptr().cast(), message.as_ptr());
61    };
62    Ok(())
63}
64
65/// Get the name of the SAPI module.
66///
67/// # Panics
68///
69/// * If the module name is not a valid [`CStr`]
70///
71/// [`CStr`]: std::ffi::CStr
72pub fn php_sapi_name() -> String {
73    let c_str = unsafe { std::ffi::CStr::from_ptr(sapi_module.name) };
74    c_str.to_str().expect("Unable to parse CStr").to_string()
75}