Skip to content

Commit bfce439

Browse files
committed
docs(guide): add guide for IniBuilder
1 parent 03a7f2a commit bfce439

File tree

3 files changed

+100
-17
lines changed

3 files changed

+100
-17
lines changed

docsrs_bindings.rs

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,11 @@ pub type __uid_t = ::std::os::raw::c_uint;
204204
pub type __gid_t = ::std::os::raw::c_uint;
205205
pub type __ino_t = ::std::os::raw::c_ulong;
206206
pub type __mode_t = ::std::os::raw::c_uint;
207-
pub type __nlink_t = ::std::os::raw::c_ulong;
207+
pub type __nlink_t = ::std::os::raw::c_uint;
208208
pub type __off_t = ::std::os::raw::c_long;
209209
pub type __off64_t = ::std::os::raw::c_long;
210210
pub type __time_t = ::std::os::raw::c_long;
211-
pub type __blksize_t = ::std::os::raw::c_long;
211+
pub type __blksize_t = ::std::os::raw::c_int;
212212
pub type __blkcnt_t = ::std::os::raw::c_long;
213213
pub type __syscall_slong_t = ::std::os::raw::c_long;
214214
pub type gid_t = __gid_t;
@@ -768,19 +768,20 @@ pub type zend_class_arrayaccess_funcs = _zend_class_arrayaccess_funcs;
768768
pub struct stat {
769769
pub st_dev: __dev_t,
770770
pub st_ino: __ino_t,
771-
pub st_nlink: __nlink_t,
772771
pub st_mode: __mode_t,
772+
pub st_nlink: __nlink_t,
773773
pub st_uid: __uid_t,
774774
pub st_gid: __gid_t,
775-
pub __pad0: ::std::os::raw::c_int,
776775
pub st_rdev: __dev_t,
776+
pub __pad1: __dev_t,
777777
pub st_size: __off_t,
778778
pub st_blksize: __blksize_t,
779+
pub __pad2: ::std::os::raw::c_int,
779780
pub st_blocks: __blkcnt_t,
780781
pub st_atim: timespec,
781782
pub st_mtim: timespec,
782783
pub st_ctim: timespec,
783-
pub __glibc_reserved: [__syscall_slong_t; 3usize],
784+
pub __glibc_reserved: [::std::os::raw::c_int; 2usize],
784785
}
785786
pub type zend_stream_fsizer_t =
786787
::std::option::Option<unsafe extern "C" fn(handle: *mut ::std::os::raw::c_void) -> usize>;
@@ -1420,7 +1421,7 @@ pub struct _zend_execute_data {
14201421
pub run_time_cache: *mut *mut ::std::os::raw::c_void,
14211422
pub extra_named_params: *mut zend_array,
14221423
}
1423-
pub type __jmp_buf = [::std::os::raw::c_long; 8usize];
1424+
pub type __jmp_buf = [::std::os::raw::c_ulonglong; 22usize];
14241425
#[repr(C)]
14251426
#[derive(Debug, Copy, Clone)]
14261427
pub struct __jmp_buf_tag {
@@ -2732,3 +2733,42 @@ pub struct _sapi_post_entry {
27322733
),
27332734
>,
27342735
}
2736+
#[doc = " A class which helps with constructing INI entries from the command\n line."]
2737+
#[repr(C)]
2738+
#[derive(Debug, Copy, Clone)]
2739+
pub struct php_ini_builder {
2740+
pub value: *mut ::std::os::raw::c_char,
2741+
pub length: usize,
2742+
}
2743+
extern "C" {
2744+
#[doc = " Prepend a string.\n\n @param src the source string\n @param length the size of the source string"]
2745+
pub fn php_ini_builder_prepend(
2746+
b: *mut php_ini_builder,
2747+
src: *const ::std::os::raw::c_char,
2748+
length: usize,
2749+
);
2750+
}
2751+
extern "C" {
2752+
#[doc = " Append an unquoted name/value pair."]
2753+
pub fn php_ini_builder_unquoted(
2754+
b: *mut php_ini_builder,
2755+
name: *const ::std::os::raw::c_char,
2756+
name_length: usize,
2757+
value: *const ::std::os::raw::c_char,
2758+
value_length: usize,
2759+
);
2760+
}
2761+
extern "C" {
2762+
#[doc = " Append a quoted name/value pair."]
2763+
pub fn php_ini_builder_quoted(
2764+
b: *mut php_ini_builder,
2765+
name: *const ::std::os::raw::c_char,
2766+
name_length: usize,
2767+
value: *const ::std::os::raw::c_char,
2768+
value_length: usize,
2769+
);
2770+
}
2771+
extern "C" {
2772+
#[doc = " Parse an INI entry from the command-line option \"--define\"."]
2773+
pub fn php_ini_builder_define(b: *mut php_ini_builder, arg: *const ::std::os::raw::c_char);
2774+
}

guide/src/ini-builder.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# INI Builder
2+
3+
When configuring a SAPI you may use `IniBuilder` to load INI settings as text.
4+
This is useful for setting up configurations required by the SAPI capabilities.
5+
6+
INI settings applied to a SAPI through `sapi.ini_entries` will be immutable,
7+
meaning they cannot be changed at runtime. This is useful for applying settings
8+
to match hard requirements of the way your SAPI works.
9+
10+
To apply _configurable_ defaults it is recommended to use a `sapi.ini_defaults`
11+
callback instead, which will allow settings to be changed at runtime.
12+
13+
```rust,no_run
14+
use ext_php_rs::builder::{IniBuilder, SapiBuilder};
15+
16+
// Create a new IniBuilder instance.
17+
let mut builder = IniBuilder::new();
18+
19+
// Append a single key/value pair to the INIT buffer with an unquoted value.
20+
builder.unquoted("log_errors", "1");
21+
22+
// Append a single key/value pair to the INI buffer with a quoted value.
23+
builder.quoted("default_mimetype", "text/html");
24+
25+
// Append INI line text as-is. A line break will be automatically appended.
26+
builder.define("memory_limit=128MB");
27+
28+
// Prepend INI line text as-is. No line break insertion will occur.
29+
builder.prepend("error_reporting=0\ndisplay_errors=1\n");
30+
31+
// Construct a SAPI.
32+
let mut sapi = SapiBuilder::new("name", "pretty_name").build()
33+
.expect("should build SAPI");
34+
35+
// Dump INI entries from the builder into the SAPI.
36+
sapi.ini_entries = builder.finish();
37+
```

src/builders/ini.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
use std::ops::Deref;
2-
use std::ffi::{c_char, CStr, CString, NulError};
31
use crate::ffi::{
4-
php_ini_builder,
5-
php_ini_builder_prepend,
2+
php_ini_builder, php_ini_builder_define, php_ini_builder_prepend, php_ini_builder_quoted,
63
php_ini_builder_unquoted,
7-
php_ini_builder_quoted,
8-
php_ini_builder_define
94
};
5+
use std::ffi::{c_char, CStr, CString, NulError};
6+
use std::ops::Deref;
107

118
// Helpful for CString which only needs to live until immediately after C call.
129
struct CStringScope(*mut c_char);
@@ -45,7 +42,7 @@ impl IniBuilder {
4542
/// let mut builder = IniBuilder::new();
4643
/// ```
4744
pub fn new() -> IniBuilder {
48-
IniBuilder {
45+
IniBuilder {
4946
value: std::ptr::null_mut(),
5047
length: 0,
5148
}
@@ -177,7 +174,7 @@ impl IniBuilder {
177174
return std::ptr::null_mut();
178175
}
179176

180-
unsafe { CStr::from_ptr(self.value) }.as_ptr() as *mut c_char
177+
unsafe { CStr::from_ptr(self.value) }.as_ptr() as *mut i8
181178
}
182179
}
183180

@@ -202,7 +199,10 @@ mod tests {
202199

203200
let ini = builder.finish();
204201
assert!(!ini.is_null());
205-
assert_eq!(unsafe { CStr::from_ptr(ini) }.to_str().unwrap(), "baz=qux\n");
202+
assert_eq!(
203+
unsafe { CStr::from_ptr(ini) }.to_str().unwrap(),
204+
"baz=qux\n"
205+
);
206206
}
207207

208208
#[test]
@@ -212,7 +212,10 @@ mod tests {
212212

213213
let ini = builder.finish();
214214
assert!(!ini.is_null());
215-
assert_eq!(unsafe { CStr::from_ptr(ini) }.to_str().unwrap(), "quux=\"corge\"\n");
215+
assert_eq!(
216+
unsafe { CStr::from_ptr(ini) }.to_str().unwrap(),
217+
"quux=\"corge\"\n"
218+
);
216219
}
217220

218221
#[test]
@@ -222,6 +225,9 @@ mod tests {
222225

223226
let ini = builder.finish();
224227
assert!(!ini.is_null());
225-
assert_eq!(unsafe { CStr::from_ptr(ini) }.to_str().unwrap(), "grault=garply\n");
228+
assert_eq!(
229+
unsafe { CStr::from_ptr(ini) }.to_str().unwrap(),
230+
"grault=garply\n"
231+
);
226232
}
227233
}

0 commit comments

Comments
 (0)