From 322a8b7ef4104347f5385729a6076cda64d92a97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Muriano=20Criado?= Date: Tue, 3 Oct 2023 10:29:00 +0200 Subject: [PATCH 1/2] feat(integration/pdo): add support to autowatch PdoStatement::bindParam function --- src/Trace/Builder/Builder.php | 19 +++++++-- src/Trace/Integration/PDO.php | 78 ++++++++++++++++++++++++++++++----- 2 files changed, 84 insertions(+), 13 deletions(-) diff --git a/src/Trace/Builder/Builder.php b/src/Trace/Builder/Builder.php index 3318e99..42f6d68 100644 --- a/src/Trace/Builder/Builder.php +++ b/src/Trace/Builder/Builder.php @@ -390,8 +390,21 @@ public function withRedisIntegration( ); } - public function withPDOIntegration(): self - { - return $this->withIntegration(new PDO()); + public function withPDOIntegration( + bool $tracePdoConnect = false, + bool $tracePdoQuery = false, + bool $tracePdoCommit = false, + bool $tracePdoStatementQuery = false, + bool $tracePdoStatementBindParam = false, + ): self { + return $this->withIntegration( + new PDO( + $tracePdoConnect, + $tracePdoQuery, + $tracePdoCommit, + $tracePdoStatementQuery, + $tracePdoStatementBindParam, + ) + ); } } \ No newline at end of file diff --git a/src/Trace/Integration/PDO.php b/src/Trace/Integration/PDO.php index ed68e85..89fd3ff 100644 --- a/src/Trace/Integration/PDO.php +++ b/src/Trace/Integration/PDO.php @@ -10,6 +10,15 @@ class PDO extends AbstractIntegration { + public function __construct( + private bool $tracePdoConnect = false, + private bool $tracePdoQuery = false, + private bool $tracePdoCommit = false, + private bool $tracePdoStatementQuery = false, + private bool $tracePdoStatementBindParam = false, + ) { + } + /** @throws JsonException */ public function tracePdoConnect( TargetPDO $pdo, @@ -77,19 +86,68 @@ public function tracePdoStatementQuery( ); } + public function tracePdoStatementBindParam( + PDOStatement $statement, + string|int $param, + mixed $var, + int $type = TargetPDO::PARAM_STR, + int $maxLength = 0, + mixed $driverOptions = null + ): array { + return $this->generateTraceParams( + 'pdo/bind-param', + [ + 'pdo.statement.instance' => spl_object_hash($statement), + 'pdo.param' => $this->convertToValue($param), + 'pdo.value' => $this->convertToValue($var), + 'pdo.type' => $this->mapPdoBindType($type), + 'pdo.max_length' => $this->convertToValue($maxLength), + 'pdo.driver_options' => $this->convertToValue($driverOptions), + ] + ); + } + + private function mapPdoBindType(int $type): string + { + return match ($type) { + TargetPDO::PARAM_BOOL => 'bool', + TargetPDO::PARAM_NULL => 'null', + TargetPDO::PARAM_INT => 'int', + TargetPDO::PARAM_STR => 'string', + TargetPDO::PARAM_LOB => 'lob', + default => 'unknown', + }; + } + protected function getMethods(): array { - return [ - TargetPDO::class => [ - 'exec' => [$this, 'tracePdoQuery',], - 'query' => [$this, 'tracePdoQuery',], - 'commit' => [$this, 'tracePdoCommit'], - '__construct' => [$this, 'tracePdoConnect',], - ], - PDOStatement::class => [ - 'execute' => [$this, 'tracePdoStatementQuery'], - ], + $methods = [ + TargetPDO::class => [], + PDOStatement::class => [], ]; + + if ($this->tracePdoConnect) { + $methods[TargetPDO::class]['__construct'] = [$this, 'tracePdoConnect']; + } + + if ($this->tracePdoQuery) { + $methods[TargetPDO::class]['exec'] = [$this, 'tracePdoQuery']; + $methods[TargetPDO::class]['query'] = [$this, 'tracePdoQuery']; + } + + if ($this->tracePdoCommit) { + $methods[TargetPDO::class]['commit'] = [$this, 'tracePdoCommit']; + } + + if ($this->tracePdoStatementQuery) { + $methods[PDOStatement::class]['execute'] = [$this, 'tracePdoStatementQuery']; + } + + if ($this->tracePdoStatementBindParam) { + $methods[PDOStatement::class]['bindParam'] = [$this, 'tracePdoStatementBindParam']; + } + + return $methods; } protected function getFunctions(): array From 26788148341b9b685275bf8bb41837e3d6f6f2a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Muriano=20Criado?= Date: Tue, 3 Oct 2023 10:36:37 +0200 Subject: [PATCH 2/2] style: empty line at file end --- src/Trace/Builder/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Trace/Builder/Builder.php b/src/Trace/Builder/Builder.php index 42f6d68..e32163a 100644 --- a/src/Trace/Builder/Builder.php +++ b/src/Trace/Builder/Builder.php @@ -407,4 +407,4 @@ public function withPDOIntegration( ) ); } -} \ No newline at end of file +}