ripht_php_sapi/execution/
hooks.rs

1use std::path::Path;
2
3use super::message::ExecutionMessage;
4use super::result::ExecutionResult;
5
6/// What to do with PHP output data.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8#[non_exhaustive]
9pub enum OutputAction {
10    /// Accumulate in the result body.
11    Buffer,
12    /// Already handled (e.g., streamed to client).
13    Handled,
14}
15
16/// No-op implementation of `ExecutionHooks`.
17pub struct NoOpHooks;
18
19impl ExecutionHooks for NoOpHooks {}
20
21/// Wraps a closure as an `ExecutionHooks` implementation for streaming output.
22pub struct StreamingCallback<F> {
23    output_fn: F,
24}
25
26impl<F> StreamingCallback<F>
27where
28    F: FnMut(&[u8]),
29{
30    pub fn new(output_fn: F) -> Self {
31        Self { output_fn }
32    }
33}
34
35impl<F> ExecutionHooks for StreamingCallback<F>
36where
37    F: FnMut(&[u8]),
38{
39    fn on_output(&mut self, data: &[u8]) -> OutputAction {
40        (self.output_fn)(data);
41        OutputAction::Handled
42    }
43}
44
45/// Callbacks invoked during PHP request execution.
46///
47/// All methods have default implementations that do nothing or return
48/// sensible defaults. Override only what you need.
49pub trait ExecutionHooks {
50    /// Called after ServerContext is created.
51    fn on_context_created(&mut self) {}
52    /// Called before php_request_startup.
53    fn on_request_starting(&mut self) {}
54    /// Called after php_request_startup succeeds.
55    fn on_request_started(&mut self) {}
56
57    /// Called before script execution begins.
58    fn on_script_executing(&mut self, script_path: &Path) {
59        let _ = script_path;
60    }
61
62    /// Called after script execution completes.
63    fn on_script_executed(&mut self, success: bool) {
64        let _ = success;
65    }
66
67    /// Called when PHP writes output. Return `Handled` to suppress buffering.
68    fn on_output(&mut self, data: &[u8]) -> OutputAction {
69        let _ = data;
70        OutputAction::Buffer
71    }
72
73    /// Called when PHP flushes output.
74    fn on_flush(&mut self) {}
75
76    /// Called for each response header. Return false to suppress the header.
77    fn on_header(&mut self, name: &str, value: &str) -> bool {
78        let _ = (name, value);
79        true
80    }
81
82    /// Called when HTTP status code is set.
83    fn on_status(&mut self, code: u16) {
84        let _ = code;
85    }
86
87    /// Called for PHP errors, warnings, and notices.
88    fn on_php_message(&mut self, message: &ExecutionMessage) {
89        let _ = message;
90    }
91
92    /// Return false to abort execution (e.g., client disconnected).
93    fn is_connection_alive(&self) -> bool {
94        true
95    }
96
97    /// Called before php_request_shutdown.
98    fn on_request_finishing(&mut self) {}
99
100    /// Called after request completes with the final result.
101    fn on_request_finished(&mut self, result: &ExecutionResult) {
102        let _ = result;
103    }
104}