Skip to content

Commit

Permalink
Fix hook for Doctrine PDO class (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
matikij authored May 20, 2023
1 parent eabb210 commit ab64a20
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/plugin/plugin_pdo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use dashmap::DashMap;
use once_cell::sync::Lazy;
use phper::{
arrays::ZArr,
classes::ClassEntry,
objects::ZObj,
sys,
values::{ExecuteData, ZVal},
Expand Down Expand Up @@ -91,7 +92,7 @@ impl PdoPlugin {

let dsn = execute_data.get_parameter(0);
let dsn = dsn.as_z_str().context("dsn isn't str")?.to_str()?;
debug!(dsn, "construct PDO");
debug!(dsn, handle, "construct PDO");

let dsn: Dsn = dsn.parse()?;
debug!(?dsn, "parse PDO dsn");
Expand Down Expand Up @@ -198,8 +199,13 @@ fn after_hook(
);
}
} else if let Some(obj) = return_value.as_mut_z_obj() {
if obj.get_class().get_name() == &"PDOStatement" {
let cls = obj.get_class();
let pdo_cls = ClassEntry::from_globals("PDOStatement").unwrap();
if cls.is_instance_of(pdo_cls) {
return after_hook_when_pdo_statement(get_this_mut(execute_data)?, obj);
} else {
let cls = cls.get_name().to_str()?;
debug!(cls, "not a subclass of PDOStatement");
}
}

Expand Down Expand Up @@ -235,7 +241,9 @@ fn after_hook_when_pdo_statement(pdo: &mut ZObj, pdo_statement: &mut ZObj) -> cr
.get(&pdo.handle())
.map(|r| r.value().clone())
.context("DSN not found")?;
DSN_MAP.insert(pdo_statement.handle(), dsn);
let handle = pdo_statement.handle();
debug!(?dsn, handle, "Hook PDOStatement class");
DSN_MAP.insert(handle, dsn);
hack_dtor(pdo_statement, Some(pdo_statement_dtor));
Ok(())
}
Expand Down Expand Up @@ -267,7 +275,7 @@ fn with_dsn<T>(handle: u32, f: impl FnOnce(&Dsn) -> anyhow::Result<T>) -> anyhow
DSN_MAP
.get(&handle)
.map(|r| f(r.value()))
.context("dns not exists")?
.context("dsn not exists")?
}

#[derive(Debug, Clone)]
Expand Down

0 comments on commit ab64a20

Please # to comment.