From 81e528da656b24a42389a8a87e32c15e3d04e799 Mon Sep 17 00:00:00 2001 From: Vladimir Pakhomchik Date: Wed, 18 Oct 2023 19:41:42 +0200 Subject: [PATCH 1/2] Laravel Debugbar Logging Middleware --- .../LaravelDebugbarLogging/Connection.php | 32 +++++++++++++ .../LaravelDebugbarLogging/Driver.php | 16 +++++++ .../LaravelDebugbarLogging/ExecutionTime.php | 29 ++++++++++++ .../LaravelDebugbarLogging/Middleware.php | 17 +++++++ .../LaravelDebugbarLogging/Statement.php | 45 +++++++++++++++++++ 5 files changed, 139 insertions(+) create mode 100644 src/DBAL/Middleware/LaravelDebugbarLogging/Connection.php create mode 100644 src/DBAL/Middleware/LaravelDebugbarLogging/Driver.php create mode 100644 src/DBAL/Middleware/LaravelDebugbarLogging/ExecutionTime.php create mode 100644 src/DBAL/Middleware/LaravelDebugbarLogging/Middleware.php create mode 100644 src/DBAL/Middleware/LaravelDebugbarLogging/Statement.php diff --git a/src/DBAL/Middleware/LaravelDebugbarLogging/Connection.php b/src/DBAL/Middleware/LaravelDebugbarLogging/Connection.php new file mode 100644 index 00000000..b673734b --- /dev/null +++ b/src/DBAL/Middleware/LaravelDebugbarLogging/Connection.php @@ -0,0 +1,32 @@ +statement = new Statement(parent::prepare($sql), $sql); + + return $this->statement; + } + + /** + * {@inheritDoc} + */ + public function query(string $sql): Result + { + return $this->time(fn() => parent::query($sql), $sql, $this->statement?->params); + } +} diff --git a/src/DBAL/Middleware/LaravelDebugbarLogging/Driver.php b/src/DBAL/Middleware/LaravelDebugbarLogging/Driver.php new file mode 100644 index 00000000..20f89804 --- /dev/null +++ b/src/DBAL/Middleware/LaravelDebugbarLogging/Driver.php @@ -0,0 +1,16 @@ +params[$param] = $value; + + return parent::bindValue($param, $value, $type); + } + + /** + * {@inheritDoc} + */ + public function execute($params = null): Result + { + return $this->time(fn() => parent::execute($params), $this->sql, $this->params); + } +} From ea9c55d29c32728661b8ede53443b3394c92692c Mon Sep 17 00:00:00 2001 From: Vladimir Pakhomchik Date: Thu, 19 Oct 2023 13:16:50 +0200 Subject: [PATCH 2/2] Fixed Connection middleware: removed unneeded Statement. --- src/DBAL/Middleware/LaravelDebugbarLogging/Connection.php | 8 ++------ .../Middleware/LaravelDebugbarLogging/ExecutionTime.php | 2 +- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/DBAL/Middleware/LaravelDebugbarLogging/Connection.php b/src/DBAL/Middleware/LaravelDebugbarLogging/Connection.php index b673734b..39b0adb3 100644 --- a/src/DBAL/Middleware/LaravelDebugbarLogging/Connection.php +++ b/src/DBAL/Middleware/LaravelDebugbarLogging/Connection.php @@ -10,16 +10,12 @@ class Connection extends AbstractConnectionMiddleware { use ExecutionTime; - private ?Statement $statement = null; - /** * {@inheritDoc} */ public function prepare(string $sql): StatementInterface { - $this->statement = new Statement(parent::prepare($sql), $sql); - - return $this->statement; + return new Statement(parent::prepare($sql), $sql); } /** @@ -27,6 +23,6 @@ public function prepare(string $sql): StatementInterface */ public function query(string $sql): Result { - return $this->time(fn() => parent::query($sql), $sql, $this->statement?->params); + return $this->time(fn() => parent::query($sql), $sql); } } diff --git a/src/DBAL/Middleware/LaravelDebugbarLogging/ExecutionTime.php b/src/DBAL/Middleware/LaravelDebugbarLogging/ExecutionTime.php index aa8da803..fbeb4175 100644 --- a/src/DBAL/Middleware/LaravelDebugbarLogging/ExecutionTime.php +++ b/src/DBAL/Middleware/LaravelDebugbarLogging/ExecutionTime.php @@ -16,7 +16,7 @@ trait ExecutionTime * * @return mixed */ - protected function time(callable $callable, string $sql, ?array $params) + protected function time(callable $callable, string $sql, ?array $params = null) { $start = microtime(true); $result = $callable();