Skip to content

Commit 14e0b61

Browse files
authored
feat(builders): add IniBuilder
Refs: #442
1 parent fcd3214 commit 14e0b61

File tree

17 files changed

+606
-65
lines changed

17 files changed

+606
-65
lines changed

allowed_bindings.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,5 +325,10 @@ bind! {
325325
php_default_post_reader,
326326
php_default_treat_data,
327327
php_default_input_filter,
328-
php_error
328+
php_error,
329+
php_ini_builder,
330+
php_ini_builder_prepend,
331+
php_ini_builder_unquoted,
332+
php_ini_builder_quoted,
333+
php_ini_builder_define
329334
}

build.rs

Lines changed: 102 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();
@@ -217,6 +228,7 @@ fn generate_bindings(defines: &[(&str, &str)], includes: &[PathBuf]) -> Result<S
217228
)
218229
.clang_args(defines.iter().map(|(var, val)| format!("-D{var}={val}")))
219230
.formatter(bindgen::Formatter::Rustfmt)
231+
.no_copy("php_ini_builder")
220232
.no_copy("_zval_struct")
221233
.no_copy("_zend_string")
222234
.no_copy("_zend_array")
@@ -249,19 +261,90 @@ fn generate_bindings(defines: &[(&str, &str)], includes: &[PathBuf]) -> Result<S
249261
Ok(bindings)
250262
}
251263

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

266349
// Infra cfg flags - use these for things that change in the Zend API that don't
267350
// rely on a feature and the crate user won't care about (e.g. struct field
@@ -275,26 +358,13 @@ fn check_php_version(info: &PHPInfo) -> Result<()> {
275358
println!(
276359
"cargo::rustc-check-cfg=cfg(php80, php81, php82, php83, php84, php_zts, php_debug, docs)"
277360
);
278-
println!("cargo:rustc-cfg=php80");
279361

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.");
362+
if version == ApiVersion::Php80 {
363+
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.");
282364
}
283365

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-
}
291-
292-
if version >= PHP_83_API_VER {
293-
println!("cargo:rustc-cfg=php83");
294-
}
295-
296-
if version >= PHP_84_API_VER {
297-
println!("cargo:rustc-cfg=php84");
366+
for supported_version in version.supported_apis() {
367+
println!("cargo:rustc-cfg={}", supported_version.cfg_name());
298368
}
299369

300370
Ok(())
@@ -339,7 +409,8 @@ fn main() -> Result<()> {
339409
let provider = Provider::new(&info)?;
340410

341411
let includes = provider.get_includes()?;
342-
let defines = provider.get_defines()?;
412+
let mut defines = provider.get_defines()?;
413+
add_php_version_defines(&mut defines, &info)?;
343414

344415
check_php_version(&info)?;
345416
build_wrapper(&defines, &includes)?;

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+
# }

src/alloc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub unsafe fn efree(ptr: *mut u8) {
5858
0,
5959
std::ptr::null_mut(),
6060
0,
61-
)
61+
);
6262
} else {
6363
#[allow(clippy::used_underscore_items)]
6464
_efree(ptr.cast::<c_void>());

0 commit comments

Comments
 (0)