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::SapiModule;
32pub use handlers::ZendObjectHandlers;
33pub use ini_entry_def::IniEntryDef;
34pub use linked_list::ZendLinkedList;
35pub use module::ModuleEntry;
36pub use streams::*;
37#[cfg(feature = "embed")]
38pub(crate) use try_catch::panic_wrapper;
39pub use try_catch::{bailout, try_catch, try_catch_first, CatchError};
40
41// Used as the format string for `php_printf`.
42const FORMAT_STR: &[u8] = b"%s\0";
43
44/// Prints to stdout using the `php_printf` function.
45///
46/// Also see the [`php_print`] and [`php_println`] macros.
47///
48/// # Arguments
49///
50/// * message - The message to print to stdout.
51///
52/// # Errors
53///
54/// * If the message could not be converted to a [`CString`].
55pub fn printf(message: &str) -> Result<()> {
56    let message = CString::new(message)?;
57    unsafe {
58        php_printf(FORMAT_STR.as_ptr().cast(), message.as_ptr());
59    };
60    Ok(())
61}
62
63/// Get the name of the SAPI module.
64///
65/// # Panics
66///
67/// * If the module name is not a valid [`CStr`]
68///
69/// [`CStr`]: std::ffi::CStr
70pub fn php_sapi_name() -> String {
71    let c_str = unsafe { std::ffi::CStr::from_ptr(sapi_module.name) };
72    c_str.to_str().expect("Unable to parse CStr").to_string()
73}