aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/util
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-05-14 19:50:40 +0900
committernsfisis <nsfisis@gmail.com>2026-05-14 19:50:40 +0900
commit34ff0250aca29044181251a6763916bd55433962 (patch)
treec010593869e584dc71e04938554f73367929c511 /crates/shirabe/src/util
parenta2f96c58f82fbd5544a9c2e7cababb0fbc445b51 (diff)
downloadphp-shirabe-34ff0250aca29044181251a6763916bd55433962.tar.gz
php-shirabe-34ff0250aca29044181251a6763916bd55433962.tar.zst
php-shirabe-34ff0250aca29044181251a6763916bd55433962.zip
feat(port): port Silencer.php
Diffstat (limited to 'crates/shirabe/src/util')
-rw-r--r--crates/shirabe/src/util/silencer.rs47
1 files changed, 47 insertions, 0 deletions
diff --git a/crates/shirabe/src/util/silencer.rs b/crates/shirabe/src/util/silencer.rs
index 4c45f23..39ec1db 100644
--- a/crates/shirabe/src/util/silencer.rs
+++ b/crates/shirabe/src/util/silencer.rs
@@ -1 +1,48 @@
//! ref: composer/src/Composer/Util/Silencer.php
+
+use std::sync::Mutex;
+use anyhow::Result;
+use shirabe_php_shim::{
+ error_reporting,
+ E_WARNING, E_NOTICE, E_USER_WARNING, E_USER_NOTICE, E_DEPRECATED, E_USER_DEPRECATED,
+};
+
+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));
+ }
+ }
+
+ pub fn call<F, T>(callable: F) -> Result<T>
+ where
+ F: FnOnce() -> Result<T>,
+ {
+ Self::suppress(None);
+ match callable() {
+ Ok(result) => {
+ Self::restore();
+ Ok(result)
+ }
+ Err(e) => {
+ Self::restore();
+ Err(e)
+ }
+ }
+ }
+}