-
Notifications
You must be signed in to change notification settings - Fork 179
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
cheack
wants to merge
2
commits into
laravel-doctrine:2.0
Choose a base branch
from
cheack:dev-debugbar
base: 2.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
src/DBAL/Middleware/LaravelDebugbarLogging/ExecutionTime.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 🤔
There was a problem hiding this comment.
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 thedatabase.connections
config.If there is no
Illuminate\Database\Connection
yet on this step, it will be created with above config.There was a problem hiding this comment.
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.