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
|
//! ref: composer/src/Composer/Platform/Runtime.php
use indexmap::IndexMap;
use shirabe_external_packages::composer::pcre::{CaptureKey, Preg};
use shirabe_php_shim::{
PhpMixed, class_exists, function_exists, html_entity_decode, implode, instantiate_class, ltrim,
strip_tags, trim,
};
/// Seam over the PHP runtime so PlatformRepository can be tested against mocked
/// extension/constant/function probes. PHP has no such interface (the test mocks the
/// concrete `Composer\Platform\Runtime` directly); it is introduced here to keep the
/// consumer dependent only on trait methods.
pub trait RuntimeInterface: std::fmt::Debug {
fn has_constant(&self, constant_name: &str, class: Option<String>) -> bool;
fn get_constant(&self, constant_name: &str, class: Option<String>) -> PhpMixed;
/// `callable` carries the PHP callable spec (a function name string or a
/// `[class, method]` list), matching PHP `invoke($callable, $arguments)`.
fn invoke(&self, callable: PhpMixed, arguments: Vec<PhpMixed>) -> PhpMixed;
fn has_class(&self, class: &str) -> bool;
fn construct(&self, class: &str, arguments: Vec<PhpMixed>) -> anyhow::Result<PhpMixed>;
fn get_extensions(&self) -> Vec<String>;
fn get_extension_version(&self, extension: &str) -> String;
fn get_extension_info(&self, extension: &str) -> anyhow::Result<String>;
}
#[derive(Debug)]
pub struct Runtime;
impl RuntimeInterface for Runtime {
fn has_constant(&self, constant_name: &str, class: Option<String>) -> bool {
shirabe_php_rpc::has_constant(<rim(
&format!("{}::{}", class.as_deref().unwrap_or(""), constant_name),
Some(":"),
))
}
fn get_constant(&self, constant_name: &str, class: Option<String>) -> PhpMixed {
shirabe_php_rpc::get_constant(<rim(
&format!("{}::{}", class.as_deref().unwrap_or(""), constant_name),
Some(":"),
))
}
fn invoke(&self, callable: PhpMixed, arguments: Vec<PhpMixed>) -> PhpMixed {
// PHP: return $callable(...$arguments);
// Only the specific dynamic callables PlatformRepository actually reaches are
// wired through php-rpc; arbitrary PHP callables are still unsupported.
match (&callable, arguments.as_slice()) {
(PhpMixed::String(name), [PhpMixed::String(arg)]) if name == "inet_pton" => {
shirabe_php_rpc::inet_pton(arg)
}
(PhpMixed::String(name), []) if name == "curl_version" => {
let mut version = IndexMap::new();
if let Some(v) = shirabe_php_rpc::curl_version() {
version.insert("version".to_string(), PhpMixed::String(v));
}
PhpMixed::Array(version)
}
_ => todo!(),
}
}
fn has_class(&self, class: &str) -> bool {
class_exists(class)
}
fn construct(&self, class: &str, arguments: Vec<PhpMixed>) -> anyhow::Result<PhpMixed> {
if arguments.is_empty() {
Ok(instantiate_class(class, vec![]))
} else {
Ok(instantiate_class(class, arguments))
}
}
fn get_extensions(&self) -> Vec<String> {
shirabe_php_rpc::get_loaded_extensions()
}
fn get_extension_version(&self, extension: &str) -> String {
shirabe_php_rpc::phpversion(extension).unwrap_or_else(|| "0".to_string())
}
fn get_extension_info(&self, extension: &str) -> anyhow::Result<String> {
Ok(shirabe_php_rpc::get_extension_info(extension))
}
}
impl Runtime {
pub fn has_function(&self, f: &str) -> bool {
function_exists(f)
}
pub fn parse_html_extension_info(html: &str) -> String {
let mut result: Vec<String> = vec![];
let mut matches: IndexMap<CaptureKey, String> = IndexMap::new();
if Preg::match3(
r"~<h2>\s*<a[^>]*>([^<]+)</a>\s*</h2>~i",
html,
Some(&mut matches),
) {
result.push(trim(
&html_entity_decode(
matches
.get(&CaptureKey::ByIndex(1))
.map(|s| s.as_str())
.unwrap_or(""),
),
None,
));
result.push(String::new());
}
let mut matches: IndexMap<CaptureKey, Vec<String>> = IndexMap::new();
if Preg::match_all3(
r#"~<tr>\s*<td class="e">\s*(.*?)\s*</td>\s*<td class="v">\s*(.*?)\s*</td>\s*</tr>~is"#,
html,
Some(&mut matches),
) > 0
{
let group1 = matches
.get(&CaptureKey::ByIndex(1))
.cloned()
.unwrap_or_default();
let group2 = matches
.get(&CaptureKey::ByIndex(2))
.cloned()
.unwrap_or_default();
let count = std::cmp::min(group1.len(), group2.len());
for i in 0..count {
let key = trim(&html_entity_decode(&strip_tags(&group1[i])), None);
let value = trim(&html_entity_decode(&strip_tags(&group2[i])), None);
result.push(format!("{} => {}", key, value));
}
}
implode("\n", &result)
}
}
|