use std::ffi::{OsStr, OsString};
use std::process::Command;
#[derive(Debug)]
pub struct PhpConfigRunner {
inner: Command,
args: Vec<OsString>,
}
impl PhpConfigRunner {
#[must_use]
pub fn new() -> Self {
Self {
inner: Command::new("php-config"),
args: vec![],
}
}
pub fn arg<S>(&mut self, arg: S) -> &mut Self
where
S: AsRef<OsStr>,
{
self.args.push(arg.as_ref().to_os_string());
let _: &mut Command = self.inner.arg(arg);
self
}
#[expect(clippy::panic, reason = "Panic is at build time")]
pub fn run(&mut self) -> String {
let output = self.inner.output().unwrap_or_else(|err| {
panic!(
"php-config {} should succeed, got {err}",
self.args
.iter()
.map(|arg| arg.display().to_string())
.collect::<Vec<_>>()
.join(" ")
)
});
let stdout_full = std::str::from_utf8(&output.stdout).unwrap_or_else(|err| {
panic!(
"php-config {} should result in UTF-8, got {err}",
self.args
.iter()
.map(|arg| arg.display().to_string())
.collect::<Vec<_>>()
.join(" ")
)
});
stdout_full.trim().to_owned()
}
}
impl Default for PhpConfigRunner {
fn default() -> Self {
Self::new()
}
}