-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Logging as Middleware support for newer versions of DBAL
- Loading branch information
Showing
7 changed files
with
208 additions
and
1 deletion.
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,81 @@ | ||
<?php | ||
|
||
namespace Intaro\PinbaBundle\Doctrine\Middleware; | ||
|
||
use Doctrine\DBAL\Driver\Connection as ConnectionInterface; | ||
use Doctrine\DBAL\Driver\Middleware\AbstractConnectionMiddleware; | ||
use Doctrine\DBAL\Driver\Result; | ||
use Intaro\PinbaBundle\Stopwatch\StopwatchEvent; | ||
use Intaro\PinbaBundle\Stopwatch\Stopwatch; | ||
|
||
final class Connection extends AbstractConnectionMiddleware | ||
{ | ||
private string $databaseHost; | ||
private ?Stopwatch $stopwatch; | ||
|
||
public function __construct( | ||
ConnectionInterface $connection, | ||
string $databaseHost, | ||
?Stopwatch $stopwatch | ||
) { | ||
parent::__construct($connection); | ||
|
||
$this->databaseHost = $databaseHost; | ||
$this->stopwatch = $stopwatch; | ||
} | ||
|
||
public function prepare(string $sql): Statement | ||
{ | ||
return new Statement( | ||
parent::prepare($sql), | ||
$this->databaseHost, | ||
$sql, | ||
$this->stopwatch, | ||
); | ||
} | ||
|
||
public function query(string $sql): Result | ||
{ | ||
$stopwatchEvent = $this->getStopwatchEvent($sql); | ||
|
||
try { | ||
return parent::query($sql); | ||
} finally { | ||
if ($stopwatchEvent) { | ||
$stopwatchEvent->stop(); | ||
} | ||
} | ||
} | ||
|
||
public function exec(string $sql): int | ||
{ | ||
$stopwatchEvent = $this->getStopwatchEvent($sql); | ||
|
||
try { | ||
return parent::exec($sql); | ||
} finally { | ||
if ($stopwatchEvent) { | ||
$stopwatchEvent->stop(); | ||
} | ||
} | ||
} | ||
|
||
private function getStopwatchEvent(string $sql): ?StopwatchEvent | ||
{ | ||
if (null === $this->stopwatch) { | ||
return null; | ||
} | ||
|
||
$tags = [ | ||
'server' => $this->databaseHost ?: ($_SERVER['HOSTNAME'] ?? ''), | ||
]; | ||
|
||
if (preg_match('/^\s*(\w+)\s/u', $sql, $matches)) { | ||
$tags['group'] = 'doctrine::' . strtolower($matches[1]); | ||
} else { | ||
$tags['group'] = 'doctrine::'; | ||
} | ||
|
||
return $this->stopwatch->start($tags); | ||
} | ||
} |
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,36 @@ | ||
<?php | ||
|
||
namespace Intaro\PinbaBundle\Doctrine\Middleware; | ||
|
||
use Doctrine\DBAL\Driver as DriverInterface; | ||
use Doctrine\DBAL\Driver\Connection as ConnectionInterface; | ||
use Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware; | ||
use Intaro\PinbaBundle\Stopwatch\Stopwatch; | ||
|
||
final class Driver extends AbstractDriverMiddleware | ||
{ | ||
private string $databaseHost; | ||
private ?Stopwatch $stopwatch; | ||
|
||
public function __construct( | ||
DriverInterface $driver, | ||
string $databaseHost, | ||
?Stopwatch $stopwatch | ||
) { | ||
parent::__construct($driver); | ||
|
||
$this->databaseHost = $databaseHost; | ||
$this->stopwatch = $stopwatch; | ||
} | ||
|
||
public function connect(array $params): ConnectionInterface | ||
{ | ||
$connection = parent::connect($params); | ||
|
||
return new Connection( | ||
$connection, | ||
$this->databaseHost, | ||
$this->stopwatch | ||
); | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
namespace Intaro\PinbaBundle\Doctrine\Middleware; | ||
|
||
use Doctrine\DBAL\Driver as DriverInterface; | ||
use Doctrine\DBAL\Driver\Middleware; | ||
use Intaro\PinbaBundle\Stopwatch\Stopwatch; | ||
|
||
class LoggingMiddleware implements Middleware | ||
{ | ||
private string $databaseHost; | ||
private ?Stopwatch $stopwatch; | ||
|
||
public function __construct( | ||
string $databaseHost, | ||
?Stopwatch $stopwatch = null | ||
) { | ||
$this->databaseHost = $databaseHost; | ||
$this->stopwatch = $stopwatch; | ||
} | ||
|
||
public function wrap(DriverInterface $driver): DriverInterface | ||
{ | ||
return new Driver($driver, $this->databaseHost, $this->stopwatch); | ||
} | ||
} |
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,54 @@ | ||
<?php | ||
|
||
namespace Intaro\PinbaBundle\Doctrine\Middleware; | ||
|
||
use Doctrine\DBAL\Driver\Middleware\AbstractStatementMiddleware; | ||
use Doctrine\DBAL\Driver\Result as ResultInterface; | ||
use Doctrine\DBAL\Driver\Statement as StatementInterface; | ||
use Intaro\PinbaBundle\Stopwatch\Stopwatch; | ||
|
||
final class Statement extends AbstractStatementMiddleware | ||
{ | ||
private string $databaseHost; | ||
private string $sql; | ||
private ?Stopwatch $stopwatch; | ||
|
||
public function __construct( | ||
StatementInterface $statement, | ||
string $databaseHost, | ||
string $sql, | ||
?Stopwatch $stopwatch = null | ||
) { | ||
parent::__construct($statement); | ||
|
||
$this->databaseHost = $databaseHost; | ||
$this->sql = $sql; | ||
$this->stopwatch = $stopwatch; | ||
} | ||
|
||
public function execute($params = null): ResultInterface | ||
{ | ||
$stopwatchEvent = null; | ||
if (null !== $this->stopwatch) { | ||
$tags = [ | ||
'server' => $this->databaseHost ?: ($_SERVER['HOSTNAME'] ?? ''), | ||
]; | ||
|
||
if (preg_match('/^\s*(\w+)\s/u', $this->sql, $matches)) { | ||
$tags['group'] = 'doctrine::' . strtolower($matches[1]); | ||
} else { | ||
$tags['group'] = 'doctrine::'; | ||
} | ||
|
||
$stopwatchEvent = $this->stopwatch->start($tags); | ||
} | ||
|
||
try { | ||
return parent::execute($params); | ||
} finally { | ||
if ($stopwatchEvent) { | ||
$stopwatchEvent->stop(); | ||
} | ||
} | ||
} | ||
} |
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
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
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