1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use std::hash::Hash;
use std::path::Path;
use std::path::PathBuf;
use crate::error::InstallationError;
use crate::utils::exec;
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct Version {
pub major: u32,
pub minor: u32,
pub release: u32,
pub extra: Option<String>,
}
impl ::std::fmt::Display for Version {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}.{}.{}{}",
self.major,
self.minor,
self.release,
self.extra.as_ref().unwrap_or(&"".to_string())
)
}
}
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct Build {
pub version: Version,
pub binary: PathBuf,
pub directory: PathBuf,
pub is_debug: bool,
pub is_thread_safety_enabled: bool,
pub php_api: u32,
pub zend_api: u32,
}
impl Build {
pub fn from_binary<P: AsRef<Path>>(binary: P) -> Result<Self, InstallationError> {
let binary = binary.as_ref().to_path_buf();
if !is_executable::is_executable(&binary) {
return Err(InstallationError::BinaryIsNotExecutable(binary));
}
let directory = binary.parent().unwrap().to_path_buf();
let version_string = exec(&binary, &["-r", VERSION_CODE])?;
let parts = version_string.split(".").collect::<Vec<&str>>();
let version = Version {
major: parts[0].parse().unwrap(),
minor: parts[1].parse().unwrap(),
release: parts[2].parse().unwrap(),
extra: {
let extra = parts[3].to_string();
if extra.is_empty() {
None
} else {
Some(extra)
}
},
};
let information = exec(&binary, &["-i"])?;
let mut is_debug = false;
let mut is_thread_safety_enabled = false;
let mut php_api = None;
let mut zend_api = None;
for line in information.lines() {
if line.contains("Thread Safety =>") {
is_thread_safety_enabled = !line.contains("disabled");
} else if line.contains("Debug Build =>") {
is_debug = !line.contains("no");
} else if line.contains("Zend Extension =>") {
zend_api = line.get(18..).and_then(|s| s.parse::<u32>().ok());
} else if line.contains("PHP Extension =>") {
php_api = line.get(17..).and_then(|s| s.parse::<u32>().ok());
}
}
Ok(Build {
version,
binary,
directory,
is_debug,
is_thread_safety_enabled,
php_api: php_api.ok_or(InstallationError::FailedToRetrieveAPIVersion)?,
zend_api: zend_api.ok_or(InstallationError::FailedToRetrieveAPIVersion)?,
})
}
pub fn config(&self) -> Option<PathBuf> {
self.bin("php-config")
}
pub fn cgi(&self) -> Option<PathBuf> {
self.bin("php-cgi")
}
pub fn phpize(&self) -> Option<PathBuf> {
self.bin("phpize")
}
pub fn phpdbg(&self) -> Option<PathBuf> {
self.bin("phpdbg")
}
fn bin(&self, name: &str) -> Option<PathBuf> {
let filename = self
.binary
.file_name()?
.to_string_lossy()
.replace("php", name);
let config = self.directory.join(filename);
if config.exists() {
Some(config)
} else {
None
}
}
}
impl AsRef<Path> for Build {
fn as_ref(&self) -> &Path {
&self.binary.as_path()
}
}
const VERSION_CODE: &str =
"echo PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION.'.'.PHP_RELEASE_VERSION.'.'.PHP_EXTRA_VERSION;";