use std::cell::OnceCell;
use std::ffi::CString;
use super::ensure_sapi;
use crate::sapi::zend_update_current_locale;
thread_local! {
pub(crate) static THREAD_ID_CSTR: OnceCell<CString> = const { OnceCell::new() };
}
pub(crate) fn bind_php_thread(
thread_id: impl std::fmt::Display,
owner: super::interrupt::ExecutionOwner,
) -> Result<(), String> {
ensure_sapi().map_err(|err| format!("failed to init SAPI: {err:?}"))?;
unsafe {
ext_php_rs::embed::ext_php_rs_sapi_per_thread_init();
zend_update_current_locale();
}
crate::functions::ensure_functions_for_current_thread().map_err(|err| {
format!("failed to register claviron PHP functions: {err}")
})?;
crate::intercept::ensure_cache_invalidation_intercepts_for_current_thread();
crate::intercept::sleep::ensure_sleep_intercept_for_current_thread();
THREAD_ID_CSTR.with(|cell| {
let _ =
cell.set(CString::new(thread_id.to_string()).unwrap_or_default());
});
super::interrupt::bind_current_thread(owner);
Ok(())
}
pub(crate) struct PhpThreadContext;
impl Drop for PhpThreadContext {
fn drop(&mut self) {
super::interrupt::unbind_current_thread();
unsafe { ext_php_rs::embed::ext_php_rs_sapi_per_thread_shutdown() };
}
}