|
| 1 | +use crate::utils; |
| 2 | +use once_cell::sync::OnceCell; |
| 3 | +use std::{ |
| 4 | + env, |
| 5 | + fs::read_to_string, |
| 6 | + io::Write, |
| 7 | + ops::{Deref, DerefMut}, |
| 8 | + path::Path, |
| 9 | + process::Command, |
| 10 | +}; |
| 11 | +use tempfile::NamedTempFile; |
| 12 | + |
| 13 | +pub struct Context { |
| 14 | + pub php_bin: String, |
| 15 | + pub ini_content: String, |
| 16 | +} |
| 17 | + |
| 18 | +impl Context { |
| 19 | + pub fn get_global() -> &'static Context { |
| 20 | + static CONTEXT: OnceCell<Context> = OnceCell::new(); |
| 21 | + &CONTEXT.get_or_init(|| { |
| 22 | + let mut ini_content = String::new(); |
| 23 | + |
| 24 | + let php_config = env::var("PHP_CONFIG").unwrap_or("php-config".to_string()); |
| 25 | + let php_bin = utils::execute_command(&[php_config.as_str(), "--php-binary"]); |
| 26 | + let ini_file = utils::execute_command(&[ |
| 27 | + php_bin.as_str(), |
| 28 | + "-d", |
| 29 | + "display_errors=stderr", |
| 30 | + "-r", |
| 31 | + "echo php_ini_loaded_file();", |
| 32 | + ]); |
| 33 | + let ini_files = utils::execute_command(&[ |
| 34 | + php_bin.as_str(), |
| 35 | + "-d", |
| 36 | + "display_errors=stderr", |
| 37 | + "-r", |
| 38 | + "echo php_ini_scanned_files();", |
| 39 | + ]); |
| 40 | + |
| 41 | + if !ini_file.is_empty() { |
| 42 | + ini_content.push_str(&read_to_string(ini_file).unwrap()); |
| 43 | + } |
| 44 | + if !ini_files.is_empty() { |
| 45 | + for file in ini_files.split(',') { |
| 46 | + let file = file.trim(); |
| 47 | + if !file.is_empty() { |
| 48 | + ini_content.push_str(&read_to_string(file).unwrap()); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + Context { |
| 54 | + php_bin, |
| 55 | + ini_content, |
| 56 | + } |
| 57 | + }) |
| 58 | + } |
| 59 | + |
| 60 | + pub fn create_tmp_php_ini_file(&self, lib_path: impl AsRef<Path>) -> NamedTempFile { |
| 61 | + let mut out_ini_temp_file = NamedTempFile::new().unwrap(); |
| 62 | + let out_ini_file = out_ini_temp_file.as_file_mut(); |
| 63 | + |
| 64 | + out_ini_file.write_all(self.ini_content.as_bytes()).unwrap(); |
| 65 | + out_ini_file |
| 66 | + .write_fmt(format_args!( |
| 67 | + "extension={}\n", |
| 68 | + lib_path.as_ref().to_str().unwrap() |
| 69 | + )) |
| 70 | + .unwrap(); |
| 71 | + |
| 72 | + out_ini_temp_file |
| 73 | + } |
| 74 | + |
| 75 | + pub fn create_command_with_tmp_php_ini_args( |
| 76 | + &self, |
| 77 | + tmp_php_ini_file: &NamedTempFile, |
| 78 | + script: impl AsRef<Path>, |
| 79 | + ) -> ContextCommand { |
| 80 | + let mut cmd = Command::new(&self.php_bin); |
| 81 | + let args = vec![ |
| 82 | + "-n".to_owned(), |
| 83 | + "-c".to_owned(), |
| 84 | + tmp_php_ini_file.path().to_str().unwrap().to_owned(), |
| 85 | + script.as_ref().to_str().unwrap().to_owned(), |
| 86 | + ]; |
| 87 | + cmd.args(&args); |
| 88 | + ContextCommand { cmd, args } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +pub struct ContextCommand { |
| 93 | + cmd: Command, |
| 94 | + args: Vec<String>, |
| 95 | +} |
| 96 | + |
| 97 | +impl ContextCommand { |
| 98 | + pub fn get_args(&self) -> &[String] { |
| 99 | + &self.args |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +impl Deref for ContextCommand { |
| 104 | + type Target = Command; |
| 105 | + |
| 106 | + fn deref(&self) -> &Self::Target { |
| 107 | + &self.cmd |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +impl DerefMut for ContextCommand { |
| 112 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 113 | + &mut self.cmd |
| 114 | + } |
| 115 | +} |
0 commit comments