From 954971b76455552c14e407f217bd9e08b883d616 Mon Sep 17 00:00:00 2001 From: jmjoy Date: Thu, 28 Nov 2024 22:41:11 +0800 Subject: [PATCH] Adapt to `Swoole\Coroutine\Http\Server` (#120) --- .github/workflows/pecl.yml | 2 +- src/plugin/plugin_swoole.rs | 49 +++++++++++++++++++++++++------------ 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/.github/workflows/pecl.yml b/.github/workflows/pecl.yml index 58dffa2..1b45382 100644 --- a/.github/workflows/pecl.yml +++ b/.github/workflows/pecl.yml @@ -48,7 +48,7 @@ jobs: matrix: os: - ubuntu-20.04 - - macos-12 + # - macos-12 version: - php: "8.2" swoole: "5.1.1" diff --git a/src/plugin/plugin_swoole.rs b/src/plugin/plugin_swoole.rs index c8f4485..46112f1 100644 --- a/src/plugin/plugin_swoole.rs +++ b/src/plugin/plugin_swoole.rs @@ -30,7 +30,7 @@ pub struct SwooleServerPlugin; impl Plugin for SwooleServerPlugin { #[inline] fn class_names(&self) -> Option<&'static [&'static str]> { - Some(&["Swoole\\Server"]) + Some(&[r"Swoole\Server", r"Swoole\Coroutine\Http\Server"]) } #[inline] @@ -39,10 +39,11 @@ impl Plugin for SwooleServerPlugin { } fn hook( - &self, _class_name: Option<&str>, function_name: &str, + &self, class_name: Option<&str>, function_name: &str, ) -> Option<(Box, Box)> { - match function_name { - "on" => Some(self.hook_on()), + match (class_name, function_name) { + (Some(r"Swoole\Server"), "on") => Some(self.hook_on()), + (Some(r"Swoole\Coroutine\Http\Server"), "handle") => Some(self.hook_handle()), _ => None, } } @@ -64,25 +65,43 @@ impl SwooleServerPlugin { return Ok(Box::new(())); } - // Hack the closure with the - // [`crate::request::skywalking_hack_swoole_on_request`]. let closure = execute_data.get_mut_parameter(1); - let ori_closure = replace( - closure, - ZVal::from(ZString::new(HACK_SWOOLE_ON_REQUEST_FUNCTION_NAME)), - ); + Self::hack_callback(closure); - ORI_SWOOLE_ON_REQUEST.store( - Box::into_raw(Box::new(ori_closure)).cast(), - Ordering::Relaxed, - ); - IS_SWOOLE.store(true, Ordering::Relaxed); + Ok(Box::new(())) + }), + Noop::noop(), + ) + } + + fn hook_handle(&self) -> (Box, Box) { + ( + Box::new(|_, execute_data| { + validate_num_args(execute_data, 2)?; + + let closure = execute_data.get_mut_parameter(1); + Self::hack_callback(closure); Ok(Box::new(())) }), Noop::noop(), ) } + + /// Hack the closure with the + /// [`crate::request::skywalking_hack_swoole_on_request`]. + fn hack_callback(closure: &mut ZVal) { + let ori_closure = replace( + closure, + ZVal::from(ZString::new(HACK_SWOOLE_ON_REQUEST_FUNCTION_NAME)), + ); + + ORI_SWOOLE_ON_REQUEST.store( + Box::into_raw(Box::new(ori_closure)).cast(), + Ordering::Relaxed, + ); + IS_SWOOLE.store(true, Ordering::Relaxed); + } } #[derive(Default, Clone)]