Skip to main content

start_output_handler

Function start_output_handler 

Source
pub fn start_output_handler<F>(
    name: &str,
    chunk_size: usize,
    flags: OutputHandlerFlags,
    handler: F,
) -> Result<()>
where F: for<'a> FnMut(&'a [u8], OutputOp) -> Cow<'a, [u8]> + 'static,
Expand description

Pushes handler on top of PHP’s output buffer stack, like ob_start() with a callback.

PHP runs handler with the buffered bytes and an OutputOp when the buffer reaches chunk_size bytes (0 means no limit), and when it is flushed, cleaned or removed. PHP sends on the bytes that handler returns. Return Cow::Borrowed(input) to pass the input through unchanged, or an empty slice to drop it. At the end of the request, PHP runs handler one last time with OutputOp::Final and then drops it.

The input is not copied. A borrowed start of the input (input or &input[..n]) is sent without a copy. Any other output is copied once to the request heap.

PHP discards the output that handler writes while it runs. If handler panics, PHP sends the buffered bytes unchanged and disables the handler. If handler causes a fatal error, the closure leaks.

§Errors

Error::OutputHandlerStartFailed if no request is active (for example during MINIT), or if a conflict check registered for name rejects the handler.

§Example

ⓘ
use std::borrow::Cow;
use ext_php_rs::zend::{OutputHandlerFlags, start_output_handler};

start_output_handler("upper", 0, OutputHandlerFlags::Std, |input, _| {
    Cow::Owned(input.to_ascii_uppercase())
})?;