2023-10-30 16:21:40 +01:00
|
|
|
#![cfg_attr(windows, feature(abi_vectorcall))]
|
2023-10-31 10:31:53 +01:00
|
|
|
#![cfg(feature = "embed")]
|
2023-10-30 16:21:40 +01:00
|
|
|
extern crate ext_php_rs;
|
|
|
|
|
|
|
|
use ext_php_rs::builders::SapiBuilder;
|
|
|
|
use ext_php_rs::embed::{ext_php_rs_sapi_startup, Embed};
|
|
|
|
use ext_php_rs::ffi::{
|
|
|
|
php_module_shutdown, php_module_startup, php_request_shutdown, php_request_startup,
|
|
|
|
sapi_shutdown, sapi_startup, ZEND_RESULT_CODE_SUCCESS,
|
|
|
|
};
|
|
|
|
use ext_php_rs::prelude::*;
|
2023-10-31 10:31:53 +01:00
|
|
|
use ext_php_rs::zend::try_catch_first;
|
|
|
|
use std::ffi::c_char;
|
2023-10-30 16:21:40 +01:00
|
|
|
|
2023-10-30 16:58:00 +01:00
|
|
|
static mut LAST_OUTPUT: String = String::new();
|
|
|
|
|
|
|
|
extern "C" fn output_tester(str: *const c_char, str_length: usize) -> usize {
|
|
|
|
let char = unsafe { std::slice::from_raw_parts(str as *const u8, str_length) };
|
|
|
|
let string = String::from_utf8_lossy(char);
|
|
|
|
|
|
|
|
println!("{}", string);
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
LAST_OUTPUT = string.to_string();
|
|
|
|
};
|
|
|
|
|
|
|
|
str_length
|
|
|
|
}
|
|
|
|
|
2023-10-30 16:21:40 +01:00
|
|
|
#[test]
|
|
|
|
fn test_sapi() {
|
2023-10-30 16:58:00 +01:00
|
|
|
let mut builder = SapiBuilder::new("test", "Test");
|
|
|
|
builder = builder.ub_write_function(output_tester);
|
|
|
|
|
2023-10-30 16:21:40 +01:00
|
|
|
let sapi = builder.build().unwrap().into_raw();
|
|
|
|
let module = get_module();
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
ext_php_rs_sapi_startup();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
sapi_startup(sapi);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
php_module_startup(sapi, module);
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = unsafe { php_request_startup() };
|
|
|
|
|
|
|
|
assert_eq!(result, ZEND_RESULT_CODE_SUCCESS);
|
|
|
|
|
2023-10-31 10:31:53 +01:00
|
|
|
let _ = try_catch_first(|| {
|
|
|
|
let result = Embed::eval("$foo = hello_world('foo');");
|
2023-10-30 16:21:40 +01:00
|
|
|
|
2023-10-31 10:31:53 +01:00
|
|
|
assert!(result.is_ok());
|
2023-10-30 16:21:40 +01:00
|
|
|
|
2023-10-31 10:31:53 +01:00
|
|
|
let zval = result.unwrap();
|
2023-10-30 16:21:40 +01:00
|
|
|
|
2023-10-31 10:31:53 +01:00
|
|
|
assert!(zval.is_string());
|
2023-10-30 16:21:40 +01:00
|
|
|
|
2023-10-31 10:31:53 +01:00
|
|
|
let string = zval.string().unwrap();
|
2023-10-30 16:21:40 +01:00
|
|
|
|
2023-10-31 10:31:53 +01:00
|
|
|
assert_eq!(string.to_string(), "Hello, foo!");
|
2023-10-30 16:58:00 +01:00
|
|
|
|
2023-10-31 10:31:53 +01:00
|
|
|
let result = Embed::eval("var_dump($foo);");
|
2023-10-30 16:58:00 +01:00
|
|
|
|
2023-10-31 10:31:53 +01:00
|
|
|
assert!(result.is_ok());
|
|
|
|
});
|
2023-10-30 16:21:40 +01:00
|
|
|
|
|
|
|
unsafe {
|
|
|
|
php_request_shutdown(std::ptr::null_mut());
|
|
|
|
}
|
|
|
|
|
2023-10-30 16:58:00 +01:00
|
|
|
unsafe {
|
|
|
|
assert_eq!(LAST_OUTPUT, "string(11) \"Hello, foo!\"\n");
|
|
|
|
}
|
|
|
|
|
2023-10-30 16:21:40 +01:00
|
|
|
unsafe {
|
|
|
|
php_module_shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
sapi_shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gives you a nice greeting!
|
|
|
|
///
|
|
|
|
/// @param string $name Your name.
|
|
|
|
///
|
|
|
|
/// @return string Nice greeting!
|
|
|
|
#[php_function]
|
|
|
|
pub fn hello_world(name: String) -> String {
|
|
|
|
format!("Hello, {}!", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[php_module]
|
|
|
|
pub fn module(module: ModuleBuilder) -> ModuleBuilder {
|
|
|
|
module
|
|
|
|
}
|