ext_php_rs/zend/
streams.rs

1use std::ptr::{self, NonNull};
2
3use crate::{
4    error::Error,
5    ffi::{
6        php_register_url_stream_wrapper, php_register_url_stream_wrapper_volatile, php_stream,
7        php_stream_context, php_stream_locate_url_wrapper, php_stream_wrapper,
8        php_stream_wrapper_ops, php_unregister_url_stream_wrapper,
9        php_unregister_url_stream_wrapper_volatile, zend_string,
10    },
11    types::ZendStr,
12};
13
14/// Wrapper for PHP streams
15pub type StreamWrapper = php_stream_wrapper;
16
17/// Stream opener function
18pub type StreamOpener = unsafe extern "C" fn(
19    *mut StreamWrapper,
20    *const std::ffi::c_char,
21    *const std::ffi::c_char,
22    i32,
23    *mut *mut zend_string,
24    *mut php_stream_context,
25    i32,
26    *const std::ffi::c_char,
27    u32,
28    *const std::ffi::c_char,
29    u32,
30) -> *mut Stream;
31
32impl StreamWrapper {
33    /// Get wrapped stream by name
34    #[must_use]
35    pub fn get(name: &str) -> Option<&Self> {
36        unsafe {
37            let result = php_stream_locate_url_wrapper(name.as_ptr().cast(), ptr::null_mut(), 0);
38            Some(NonNull::new(result)?.as_ref())
39        }
40    }
41
42    /// Get mutable wrapped stream by name
43    #[must_use]
44    #[allow(clippy::mut_from_ref)]
45    pub fn get_mut(name: &str) -> Option<&mut Self> {
46        unsafe {
47            let result = php_stream_locate_url_wrapper(name.as_ptr().cast(), ptr::null_mut(), 0);
48            Some(NonNull::new(result)?.as_mut())
49        }
50    }
51
52    /// Register stream wrapper for name
53    ///
54    /// # Errors
55    ///
56    /// * `Error::StreamWrapperRegistrationFailure` - If the stream wrapper
57    ///   could not be registered
58    ///
59    /// # Panics
60    ///
61    /// * If the name cannot be converted to a C string
62    pub fn register(self, name: &str) -> Result<Self, Error> {
63        // We have to convert it to a static so owned streamwrapper doesn't get dropped.
64        let copy = Box::new(self);
65        let copy = Box::leak(copy);
66        let name = std::ffi::CString::new(name).expect("Could not create C string for name!");
67        let result = unsafe { php_register_url_stream_wrapper(name.as_ptr(), copy) };
68        if result == 0 {
69            Ok(*copy)
70        } else {
71            Err(Error::StreamWrapperRegistrationFailure)
72        }
73    }
74
75    /// Register volatile stream wrapper for name
76    ///
77    /// # Errors
78    ///
79    /// * `Error::StreamWrapperRegistrationFailure` - If the stream wrapper
80    ///   could not be registered
81    pub fn register_volatile(self, name: &str) -> Result<Self, Error> {
82        // We have to convert it to a static so owned streamwrapper doesn't get dropped.
83        let copy = Box::new(self);
84        let copy = Box::leak(copy);
85        let name = ZendStr::new(name, false);
86        let result =
87            unsafe { php_register_url_stream_wrapper_volatile((*name).as_ptr().cast_mut(), copy) };
88        if result == 0 {
89            Ok(*copy)
90        } else {
91            Err(Error::StreamWrapperRegistrationFailure)
92        }
93    }
94
95    /// Unregister stream wrapper by name
96    ///
97    /// # Errors
98    ///
99    /// * `Error::StreamWrapperUnregistrationFailure` - If the stream wrapper
100    ///   could not be unregistered
101    ///
102    /// # Panics
103    ///
104    /// * If the name cannot be converted to a C string
105    pub fn unregister(name: &str) -> Result<(), Error> {
106        let name = std::ffi::CString::new(name).expect("Could not create C string for name!");
107        match unsafe { php_unregister_url_stream_wrapper(name.as_ptr()) } {
108            0 => Ok(()),
109            _ => Err(Error::StreamWrapperUnregistrationFailure),
110        }
111    }
112
113    /// Unregister volatile stream wrapper by name
114    ///
115    /// # Errors
116    ///
117    /// * `Error::StreamWrapperUnregistrationFailure` - If the stream wrapper
118    ///   could not be unregistered
119    pub fn unregister_volatile(name: &str) -> Result<(), Error> {
120        let name = ZendStr::new(name, false);
121        match unsafe { php_unregister_url_stream_wrapper_volatile((*name).as_ptr().cast_mut()) } {
122            0 => Ok(()),
123            _ => Err(Error::StreamWrapperUnregistrationFailure),
124        }
125    }
126
127    /// Get the operations the stream wrapper can perform
128    #[must_use]
129    pub fn wops(&self) -> &php_stream_wrapper_ops {
130        unsafe { &*self.wops }
131    }
132
133    /// Get the mutable operations the stream can perform
134    pub fn wops_mut(&mut self) -> &mut php_stream_wrapper_ops {
135        unsafe { &mut *(self.wops.cast_mut()) }
136    }
137}
138
139/// A PHP stream
140pub type Stream = php_stream;
141
142/// Operations that can be performed with a stream wrapper
143pub type StreamWrapperOps = php_stream_wrapper_ops;
144
145impl StreamWrapperOps {}