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

[FEATURE] Laravel Debugbar Logging Middleware #567

Open
wants to merge 2 commits into
base: 2.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 28 additions & 0 deletions src/DBAL/Middleware/LaravelDebugbarLogging/Connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace LaravelDoctrine\ORM\DBAL\Middleware\LaravelDebugbarLogging;

use Doctrine\DBAL\Driver\Middleware\AbstractConnectionMiddleware;
use Doctrine\DBAL\Driver\Result;
use Doctrine\DBAL\Driver\Statement as StatementInterface;

class Connection extends AbstractConnectionMiddleware
{
use ExecutionTime;

/**
* {@inheritDoc}
*/
public function prepare(string $sql): StatementInterface
{
return new Statement(parent::prepare($sql), $sql);
}

/**
* {@inheritDoc}
*/
public function query(string $sql): Result
{
return $this->time(fn() => parent::query($sql), $sql);
}
}
16 changes: 16 additions & 0 deletions src/DBAL/Middleware/LaravelDebugbarLogging/Driver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace LaravelDoctrine\ORM\DBAL\Middleware\LaravelDebugbarLogging;

use Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware;

class Driver extends AbstractDriverMiddleware
{
/**
* {@inheritDoc}
*/
public function connect(array $params): Connection
{
return new Connection(parent::connect($params));
}
}
29 changes: 29 additions & 0 deletions src/DBAL/Middleware/LaravelDebugbarLogging/ExecutionTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace LaravelDoctrine\ORM\DBAL\Middleware\LaravelDebugbarLogging;

use Illuminate\Database\Connection as DatabaseConnection;
use Illuminate\Database\Events\QueryExecuted;

trait ExecutionTime
{
/**
* Measure the execution time of a callable and log it as a query execution event.
*
* @param callable $callable
* @param string $sql
* @param array|null $params
*
* @return mixed
*/
protected function time(callable $callable, string $sql, ?array $params = null)
{
$start = microtime(true);
$result = $callable();
$end = microtime(true);

event(new QueryExecuted($sql, $params ?: [], ($end - $start) * 1000, app(DatabaseConnection::class)));
Copy link
Member

Choose a reason for hiding this comment

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

This is the wrong connection, most likely. And for us without Illuminate\Database\Connection in container at all will it crash 😭

Need to subclass this Connection with a "proxy" into the doctrine connection or something 🤔

Copy link
Author

@cheack cheack Oct 19, 2023

Choose a reason for hiding this comment

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

In fact, there will be a valid connection here, which is specified in the doctrine.managers.default.connection config, which in turn is configured in the database.connections config.
If there is no Illuminate\Database\Connection yet on this step, it will be created with above config.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds to me you have \Illuminate\Database\DatabaseServiceProvider enabled in config/app.php?

I do not, and it would be required for this to work, which seems weird.


return $result;
}
}
17 changes: 17 additions & 0 deletions src/DBAL/Middleware/LaravelDebugbarLogging/Middleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace LaravelDoctrine\ORM\DBAL\Middleware\LaravelDebugbarLogging;

use Doctrine\DBAL\Driver as DriverInterface;
use Doctrine\DBAL\Driver\Middleware as MiddlewareInterface;

class Middleware implements MiddlewareInterface
{
/**
* {@inheritDoc}
*/
public function wrap(DriverInterface $driver): DriverInterface
{
return new Driver($driver);
}
}
45 changes: 45 additions & 0 deletions src/DBAL/Middleware/LaravelDebugbarLogging/Statement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace LaravelDoctrine\ORM\DBAL\Middleware\LaravelDebugbarLogging;

use Doctrine\DBAL\Driver\Middleware\AbstractStatementMiddleware;
use Doctrine\DBAL\Driver\Result;
use Doctrine\DBAL\Driver\Statement as StatementInterface;
use Doctrine\DBAL\ParameterType;

class Statement extends AbstractStatementMiddleware
{
use ExecutionTime;

public array $params = [];

/**
* @param StatementInterface $statement
* @param string $sql
*/
public function __construct(
StatementInterface $statement,
private string $sql,
)
{
parent::__construct($statement);
}

/**
* {@inheritDoc}
*/
public function bindValue($param, $value, $type = ParameterType::STRING)
{
$this->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);
}
}