Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(integration/pdo): add support to autowatch PdoStatement::bindParam function #4

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/Trace/Builder/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
);
}
}
Copy link
Member

@kerunaru kerunaru Oct 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Añadir un salto de línea al final del archivo

78 changes: 68 additions & 10 deletions src/Trace/Integration/PDO.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down