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
|
//! ref: composer/src/Composer/Util/Silencer.php
use shirabe_php_shim::{
E_DEPRECATED, E_NOTICE, E_USER_DEPRECATED, E_USER_NOTICE, E_USER_WARNING, E_WARNING,
error_reporting,
};
use std::sync::Mutex;
static STACK: Mutex<Vec<i64>> = Mutex::new(Vec::new());
pub struct Silencer;
impl Silencer {
pub fn suppress(mask: Option<i64>) -> i64 {
let mask = mask.unwrap_or(
E_WARNING
| E_NOTICE
| E_USER_WARNING
| E_USER_NOTICE
| E_DEPRECATED
| E_USER_DEPRECATED,
);
let old = error_reporting(None);
STACK.lock().unwrap().push(old);
error_reporting(Some(old & !mask));
old
}
pub fn restore() {
let mut stack = STACK.lock().unwrap();
if !stack.is_empty() {
let level = stack.pop().unwrap();
drop(stack);
error_reporting(Some(level));
}
}
/// Wrap a callable only when it can reach the PHP runtime, where a plugin may emit diagnostics
/// of its own; the same holds for a region bracketed by `suppress` and `restore`. Work that
/// stays inside Rust has no `error_reporting()` level to lower and emits no diagnostic on
/// failure, and errors it raises propagate either way, so silencing it is indistinguishable
/// from running it unguarded. Run it unguarded instead.
pub fn call<F, T>(callable: F) -> anyhow::Result<T>
where
F: FnOnce() -> anyhow::Result<T>,
{
Self::suppress(None);
match callable() {
Ok(result) => {
Self::restore();
Ok(result)
}
Err(e) => {
Self::restore();
Err(e)
}
}
}
}
|