Skip to content

Commit b5f7f24

Browse files
committed
docs(guide): add guide for IniBuilder
1 parent dfae210 commit b5f7f24

File tree

10 files changed

+332
-66
lines changed

10 files changed

+332
-66
lines changed

build.rs

Lines changed: 101 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@ use std::{
1616
str::FromStr,
1717
};
1818

19-
use anyhow::{anyhow, bail, Context, Result};
19+
use anyhow::{anyhow, bail, Context, Error, Result};
2020
use bindgen::RustTarget;
2121
use impl_::Provider;
2222

23-
const MIN_PHP_API_VER: u32 = 2020_09_30;
24-
const MAX_PHP_API_VER: u32 = 2024_09_24;
25-
2623
/// Provides information about the PHP installation.
2724
pub trait PHPProvider<'a>: Sized {
2825
/// Create a new PHP provider.
@@ -170,6 +167,20 @@ impl PHPInfo {
170167
}
171168
}
172169

170+
fn add_php_version_defines(
171+
defines: &mut Vec<(&'static str, &'static str)>,
172+
info: &PHPInfo,
173+
) -> Result<()> {
174+
let version = info.zend_version()?;
175+
let supported_version: ApiVersion = version.try_into()?;
176+
177+
for supported_api in supported_version.supported_apis() {
178+
defines.push((supported_api.define_name(), "1"));
179+
}
180+
181+
Ok(())
182+
}
183+
173184
/// Builds the wrapper library.
174185
fn build_wrapper(defines: &[(&str, &str)], includes: &[PathBuf]) -> Result<()> {
175186
let mut build = cc::Build::new();
@@ -249,19 +260,90 @@ fn generate_bindings(defines: &[(&str, &str)], includes: &[PathBuf]) -> Result<S
249260
Ok(bindings)
250261
}
251262

263+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd)]
264+
enum ApiVersion {
265+
Php80 = 2020_09_30,
266+
Php81 = 2021_09_02,
267+
Php82 = 2022_08_29,
268+
Php83 = 2023_08_31,
269+
Php84 = 2024_09_24,
270+
}
271+
272+
impl ApiVersion {
273+
/// Returns the minimum API version supported by ext-php-rs.
274+
pub const fn min() -> Self {
275+
ApiVersion::Php80
276+
}
277+
278+
/// Returns the maximum API version supported by ext-php-rs.
279+
pub const fn max() -> Self {
280+
ApiVersion::Php84
281+
}
282+
283+
pub fn versions() -> Vec<Self> {
284+
vec![
285+
ApiVersion::Php80,
286+
ApiVersion::Php81,
287+
ApiVersion::Php82,
288+
ApiVersion::Php83,
289+
ApiVersion::Php84,
290+
]
291+
}
292+
293+
/// Returns the API versions that are supported by this version.
294+
pub fn supported_apis(self) -> Vec<ApiVersion> {
295+
ApiVersion::versions()
296+
.into_iter()
297+
.filter(|&v| v <= self)
298+
.collect()
299+
}
300+
301+
pub fn cfg_name(self) -> &'static str {
302+
match self {
303+
ApiVersion::Php80 => "php80",
304+
ApiVersion::Php81 => "php81",
305+
ApiVersion::Php82 => "php82",
306+
ApiVersion::Php83 => "php83",
307+
ApiVersion::Php84 => "php84",
308+
}
309+
}
310+
311+
pub fn define_name(self) -> &'static str {
312+
match self {
313+
ApiVersion::Php80 => "EXT_PHP_RS_PHP_80",
314+
ApiVersion::Php81 => "EXT_PHP_RS_PHP_81",
315+
ApiVersion::Php82 => "EXT_PHP_RS_PHP_82",
316+
ApiVersion::Php83 => "EXT_PHP_RS_PHP_83",
317+
ApiVersion::Php84 => "EXT_PHP_RS_PHP_84",
318+
}
319+
}
320+
}
321+
322+
impl TryFrom<u32> for ApiVersion {
323+
type Error = Error;
324+
325+
fn try_from(version: u32) -> Result<Self, Self::Error> {
326+
match version {
327+
x if (2020_09_30..2021_09_02).contains(&x) => Ok(ApiVersion::Php80),
328+
x if (2021_09_02..2022_08_29).contains(&x) => Ok(ApiVersion::Php81),
329+
x if (2022_08_29..2023_08_31).contains(&x) => Ok(ApiVersion::Php82),
330+
x if (2023_08_31..2024_09_24).contains(&x) => Ok(ApiVersion::Php83),
331+
2024_09_24 => Ok(ApiVersion::Php84),
332+
version => Err(anyhow!(
333+
"The current version of PHP is not supported. Current PHP API version: {}, requires a version between {} and {}",
334+
version,
335+
ApiVersion::min() as u32,
336+
ApiVersion::max() as u32
337+
))
338+
}
339+
}
340+
}
341+
252342
/// Checks the PHP Zend API version for compatibility with ext-php-rs, setting
253343
/// any configuration flags required.
254344
fn check_php_version(info: &PHPInfo) -> Result<()> {
255-
const PHP_81_API_VER: u32 = 2021_09_02;
256-
const PHP_82_API_VER: u32 = 2022_08_29;
257-
const PHP_83_API_VER: u32 = 2023_08_31;
258-
const PHP_84_API_VER: u32 = 2024_09_24;
259-
260345
let version = info.zend_version()?;
261-
262-
if !(MIN_PHP_API_VER..=MAX_PHP_API_VER).contains(&version) {
263-
bail!("The current version of PHP is not supported. Current PHP API version: {}, requires a version between {} and {}", version, MIN_PHP_API_VER, MAX_PHP_API_VER);
264-
}
346+
let version: ApiVersion = version.try_into()?;
265347

266348
// Infra cfg flags - use these for things that change in the Zend API that don't
267349
// rely on a feature and the crate user won't care about (e.g. struct field
@@ -275,26 +357,13 @@ fn check_php_version(info: &PHPInfo) -> Result<()> {
275357
println!(
276358
"cargo::rustc-check-cfg=cfg(php80, php81, php82, php83, php84, php_zts, php_debug, docs)"
277359
);
278-
println!("cargo:rustc-cfg=php80");
279-
280-
if (MIN_PHP_API_VER..PHP_81_API_VER).contains(&version) {
281-
println!("cargo:warning=PHP version 8.0 is EOL and will no longer be supported in a future release. Please upgrade to a supported version of PHP. See https://www.php.net/supported-versions.php for information on version support timelines.");
282-
}
283-
284-
if version >= PHP_81_API_VER {
285-
println!("cargo:rustc-cfg=php81");
286-
}
287-
288-
if version >= PHP_82_API_VER {
289-
println!("cargo:rustc-cfg=php82");
290-
}
291360

292-
if version >= PHP_83_API_VER {
293-
println!("cargo:rustc-cfg=php83");
361+
if version == ApiVersion::Php80 {
362+
println!("cargo:warning=PHP 8.0 is EOL and will no longer be supported in a future release. Please upgrade to a supported version of PHP. See https://www.php.net/supported-versions.php for information on version support timelines.");
294363
}
295364

296-
if version >= PHP_84_API_VER {
297-
println!("cargo:rustc-cfg=php84");
365+
for supported_version in version.supported_apis() {
366+
println!("cargo:rustc-cfg={}", supported_version.cfg_name());
298367
}
299368

300369
Ok(())
@@ -337,7 +406,8 @@ fn main() -> Result<()> {
337406
let provider = Provider::new(&info)?;
338407

339408
let includes = provider.get_includes()?;
340-
let defines = provider.get_defines()?;
409+
let mut defines = provider.get_defines()?;
410+
add_php_version_defines(&mut defines, &info)?;
341411

342412
check_php_version(&info)?;
343413
build_wrapper(&defines, &includes)?;

docsrs_bindings.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2302,6 +2302,17 @@ impl _php_stream {
23022302
}
23032303
}
23042304
#[inline]
2305+
pub fn fatal_error(&self) -> u16 {
2306+
unsafe { ::std::mem::transmute(self._bitfield_1.get(9usize, 1u8) as u16) }
2307+
}
2308+
#[inline]
2309+
pub fn set_fatal_error(&mut self, val: u16) {
2310+
unsafe {
2311+
let val: u16 = ::std::mem::transmute(val);
2312+
self._bitfield_1.set(9usize, 1u8, val as u64)
2313+
}
2314+
}
2315+
#[inline]
23052316
pub fn new_bitfield_1(
23062317
is_persistent: u16,
23072318
in_free: u16,
@@ -2310,6 +2321,7 @@ impl _php_stream {
23102321
fclose_stdiocast: u16,
23112322
has_buffered_data: u16,
23122323
fclose_stdiocast_flush_in_progress: u16,
2324+
fatal_error: u16,
23132325
) -> __BindgenBitfieldUnit<[u8; 2usize]> {
23142326
let mut __bindgen_bitfield_unit: __BindgenBitfieldUnit<[u8; 2usize]> = Default::default();
23152327
__bindgen_bitfield_unit.set(0usize, 1u8, {
@@ -2341,6 +2353,10 @@ impl _php_stream {
23412353
unsafe { ::std::mem::transmute(fclose_stdiocast_flush_in_progress) };
23422354
fclose_stdiocast_flush_in_progress as u64
23432355
});
2356+
__bindgen_bitfield_unit.set(9usize, 1u8, {
2357+
let fatal_error: u16 = unsafe { ::std::mem::transmute(fatal_error) };
2358+
fatal_error as u64
2359+
});
23442360
__bindgen_bitfield_unit
23452361
}
23462362
}
@@ -2880,3 +2896,42 @@ pub struct _sapi_post_entry {
28802896
),
28812897
>,
28822898
}
2899+
#[doc = " A class which helps with constructing INI entries from the command\n line."]
2900+
#[repr(C)]
2901+
#[derive(Debug, Copy, Clone)]
2902+
pub struct php_ini_builder {
2903+
pub value: *mut ::std::os::raw::c_char,
2904+
pub length: usize,
2905+
}
2906+
extern "C" {
2907+
#[doc = " Prepend a string.\n\n @param src the source string\n @param length the size of the source string"]
2908+
pub fn php_ini_builder_prepend(
2909+
b: *mut php_ini_builder,
2910+
src: *const ::std::os::raw::c_char,
2911+
length: usize,
2912+
);
2913+
}
2914+
extern "C" {
2915+
#[doc = " Append an unquoted name/value pair."]
2916+
pub fn php_ini_builder_unquoted(
2917+
b: *mut php_ini_builder,
2918+
name: *const ::std::os::raw::c_char,
2919+
name_length: usize,
2920+
value: *const ::std::os::raw::c_char,
2921+
value_length: usize,
2922+
);
2923+
}
2924+
extern "C" {
2925+
#[doc = " Append a quoted name/value pair."]
2926+
pub fn php_ini_builder_quoted(
2927+
b: *mut php_ini_builder,
2928+
name: *const ::std::os::raw::c_char,
2929+
name_length: usize,
2930+
value: *const ::std::os::raw::c_char,
2931+
value_length: usize,
2932+
);
2933+
}
2934+
extern "C" {
2935+
#[doc = " Parse an INI entry from the command-line option \"--define\"."]
2936+
pub fn php_ini_builder_define(b: *mut php_ini_builder, arg: *const ::std::os::raw::c_char);
2937+
}

guide/src/ini-builder.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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,ignore
14+
use ext_php_rs::builder::{IniBuilder, SapiBuilder};
15+
16+
# fn main() {
17+
// Create a new IniBuilder instance.
18+
let mut builder = IniBuilder::new();
19+
20+
// Append a single key/value pair to the INIT buffer with an unquoted value.
21+
builder.unquoted("log_errors", "1");
22+
23+
// Append a single key/value pair to the INI buffer with a quoted value.
24+
builder.quoted("default_mimetype", "text/html");
25+
26+
// Append INI line text as-is. A line break will be automatically appended.
27+
builder.define("memory_limit=128MB");
28+
29+
// Prepend INI line text as-is. No line break insertion will occur.
30+
builder.prepend("error_reporting=0\ndisplay_errors=1\n");
31+
32+
// Construct a SAPI.
33+
let mut sapi = SapiBuilder::new("name", "pretty_name").build()
34+
.expect("should build SAPI");
35+
36+
// Dump INI entries from the builder into the SAPI.
37+
sapi.ini_entries = builder.finish();
38+
# }

0 commit comments

Comments
 (0)