mirror of
https://github.com/danog/ext-php-rs.git
synced 2024-12-03 10:07:44 +01:00
add output
This commit is contained in:
parent
51042e5310
commit
4f74bfcd8a
@ -1,7 +1,7 @@
|
||||
use crate::ffi::sapi_header_struct;
|
||||
use crate::{embed::SapiModule, error::Result};
|
||||
|
||||
use std::ffi::c_void;
|
||||
use std::ffi::{c_char, c_void};
|
||||
use std::{ffi::CString, ptr};
|
||||
|
||||
pub struct SapiBuilder {
|
||||
@ -56,6 +56,11 @@ impl SapiBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ub_write_function(mut self, func: SapiUbWriteFunc) -> Self {
|
||||
self.module.ub_write = Some(func);
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the send header function for this SAPI
|
||||
///
|
||||
/// # Arguments
|
||||
@ -81,8 +86,11 @@ impl SapiBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
/// A function to be called when the extension is starting up or shutting down.
|
||||
/// A function to be called when PHP send a header
|
||||
pub type SapiSendHeaderFunc =
|
||||
extern "C" fn(header: *mut sapi_header_struct, server_context: *mut c_void);
|
||||
|
||||
/// A function to be called when PHP write to the output buffer
|
||||
pub type SapiUbWriteFunc = extern "C" fn(str: *const c_char, str_length: usize) -> usize;
|
||||
|
||||
extern "C" fn dummy_send_header(_header: *mut sapi_header_struct, _server_context: *mut c_void) {}
|
||||
|
@ -1,6 +1,8 @@
|
||||
#![cfg_attr(windows, feature(abi_vectorcall))]
|
||||
extern crate ext_php_rs;
|
||||
|
||||
#[cfg(feature = "embed")]
|
||||
use std::ffi::c_char;
|
||||
#[cfg(feature = "embed")]
|
||||
use ext_php_rs::builders::SapiBuilder;
|
||||
#[cfg(feature = "embed")]
|
||||
@ -14,10 +16,29 @@ use ext_php_rs::prelude::*;
|
||||
#[cfg(feature = "embed")]
|
||||
use ext_php_rs::zend::try_catch;
|
||||
|
||||
#[cfg(feature = "embed")]
|
||||
static mut LAST_OUTPUT: String = String::new();
|
||||
|
||||
#[cfg(feature = "embed")]
|
||||
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
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "embed")]
|
||||
fn test_sapi() {
|
||||
let builder = SapiBuilder::new("test", "Test");
|
||||
let mut builder = SapiBuilder::new("test", "Test");
|
||||
builder = builder.ub_write_function(output_tester);
|
||||
|
||||
let sapi = builder.build().unwrap().into_raw();
|
||||
let module = get_module();
|
||||
|
||||
@ -50,6 +71,10 @@ fn test_sapi() {
|
||||
let string = zval.string().unwrap();
|
||||
|
||||
assert_eq!(string.to_string(), "Hello, foo!");
|
||||
|
||||
let result = Embed::eval("var_dump($foo);");
|
||||
|
||||
assert!(result.is_ok());
|
||||
},
|
||||
true,
|
||||
);
|
||||
@ -58,6 +83,10 @@ fn test_sapi() {
|
||||
php_request_shutdown(std::ptr::null_mut());
|
||||
}
|
||||
|
||||
unsafe {
|
||||
assert_eq!(LAST_OUTPUT, "string(11) \"Hello, foo!\"\n");
|
||||
}
|
||||
|
||||
unsafe {
|
||||
php_module_shutdown();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user