Skip to content

Commit 4f74bfc

Browse files
committed
add output
1 parent 51042e5 commit 4f74bfc

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/builders/sapi.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::ffi::sapi_header_struct;
22
use crate::{embed::SapiModule, error::Result};
33

4-
use std::ffi::c_void;
4+
use std::ffi::{c_char, c_void};
55
use std::{ffi::CString, ptr};
66

77
pub struct SapiBuilder {
@@ -56,6 +56,11 @@ impl SapiBuilder {
5656
}
5757
}
5858

59+
pub fn ub_write_function(mut self, func: SapiUbWriteFunc) -> Self {
60+
self.module.ub_write = Some(func);
61+
self
62+
}
63+
5964
/// Sets the send header function for this SAPI
6065
///
6166
/// # Arguments
@@ -81,8 +86,11 @@ impl SapiBuilder {
8186
}
8287
}
8388

84-
/// A function to be called when the extension is starting up or shutting down.
89+
/// A function to be called when PHP send a header
8590
pub type SapiSendHeaderFunc =
8691
extern "C" fn(header: *mut sapi_header_struct, server_context: *mut c_void);
8792

93+
/// A function to be called when PHP write to the output buffer
94+
pub type SapiUbWriteFunc = extern "C" fn(str: *const c_char, str_length: usize) -> usize;
95+
8896
extern "C" fn dummy_send_header(_header: *mut sapi_header_struct, _server_context: *mut c_void) {}

tests/sapi.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#![cfg_attr(windows, feature(abi_vectorcall))]
22
extern crate ext_php_rs;
33

4+
#[cfg(feature = "embed")]
5+
use std::ffi::c_char;
46
#[cfg(feature = "embed")]
57
use ext_php_rs::builders::SapiBuilder;
68
#[cfg(feature = "embed")]
@@ -14,10 +16,29 @@ use ext_php_rs::prelude::*;
1416
#[cfg(feature = "embed")]
1517
use ext_php_rs::zend::try_catch;
1618

19+
#[cfg(feature = "embed")]
20+
static mut LAST_OUTPUT: String = String::new();
21+
22+
#[cfg(feature = "embed")]
23+
extern "C" fn output_tester(str: *const c_char, str_length: usize) -> usize {
24+
let char = unsafe { std::slice::from_raw_parts(str as *const u8, str_length) };
25+
let string = String::from_utf8_lossy(char);
26+
27+
println!("{}", string);
28+
29+
unsafe {
30+
LAST_OUTPUT = string.to_string();
31+
};
32+
33+
str_length
34+
}
35+
1736
#[test]
1837
#[cfg(feature = "embed")]
1938
fn test_sapi() {
20-
let builder = SapiBuilder::new("test", "Test");
39+
let mut builder = SapiBuilder::new("test", "Test");
40+
builder = builder.ub_write_function(output_tester);
41+
2142
let sapi = builder.build().unwrap().into_raw();
2243
let module = get_module();
2344

@@ -50,6 +71,10 @@ fn test_sapi() {
5071
let string = zval.string().unwrap();
5172

5273
assert_eq!(string.to_string(), "Hello, foo!");
74+
75+
let result = Embed::eval("var_dump($foo);");
76+
77+
assert!(result.is_ok());
5378
},
5479
true,
5580
);
@@ -58,6 +83,10 @@ fn test_sapi() {
5883
php_request_shutdown(std::ptr::null_mut());
5984
}
6085

86+
unsafe {
87+
assert_eq!(LAST_OUTPUT, "string(11) \"Hello, foo!\"\n");
88+
}
89+
6190
unsafe {
6291
php_module_shutdown();
6392
}

0 commit comments

Comments
 (0)