Skip to content

[DoctrineBridge] Allow to use a middleware instead of DbalLogger #45491

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

Merged
merged 1 commit into from
Mar 25, 2022
Merged
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
15 changes: 8 additions & 7 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,14 @@
<array>
<element key="0"><string>Cache\IntegrationTests</string></element>
<element key="1"><string>Doctrine\Common\Cache</string></element>
<element key="2"><string>Symfony\Component\Cache</string></element>
<element key="3"><string>Symfony\Component\Cache\Tests\Fixtures</string></element>
<element key="4"><string>Symfony\Component\Cache\Tests\Traits</string></element>
<element key="5"><string>Symfony\Component\Cache\Traits</string></element>
<element key="6"><string>Symfony\Component\Console</string></element>
<element key="7"><string>Symfony\Component\HttpFoundation</string></element>
<element key="8"><string>Symfony\Component\Uid</string></element>
<element key="2"><string>Symfony\Bridge\Doctrine\Middleware\Debug</string></element>
<element key="3"><string>Symfony\Component\Cache</string></element>
<element key="4"><string>Symfony\Component\Cache\Tests\Fixtures</string></element>
<element key="5"><string>Symfony\Component\Cache\Tests\Traits</string></element>
<element key="6"><string>Symfony\Component\Cache\Traits</string></element>
<element key="7"><string>Symfony\Component\Console</string></element>
<element key="8"><string>Symfony\Component\HttpFoundation</string></element>
<element key="9"><string>Symfony\Component\Uid</string></element>
</array>
</element>
</array>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bridge\Doctrine\Middleware\Debug\DebugDataHolder;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
Expand All @@ -31,17 +32,19 @@ class DoctrineDataCollector extends DataCollector
private $registry;
private $connections;
private $managers;
private $debugDataHolder;

/**
* @var DebugStack[]
*/
private $loggers = [];

public function __construct(ManagerRegistry $registry)
public function __construct(ManagerRegistry $registry, DebugDataHolder $debugDataHolder = null)
{
$this->registry = $registry;
$this->connections = $registry->getConnectionNames();
$this->managers = $registry->getManagerNames();
$this->debugDataHolder = $debugDataHolder;
}

/**
Expand All @@ -56,23 +59,43 @@ public function addLogger(string $name, DebugStack $logger)
* {@inheritdoc}
*/
public function collect(Request $request, Response $response, \Throwable $exception = null)
{
$this->data = [
'queries' => $this->collectQueries(),
'connections' => $this->connections,
'managers' => $this->managers,
];
}

private function collectQueries(): array
{
$queries = [];

if (null !== $this->debugDataHolder) {
foreach ($this->debugDataHolder->getData() as $name => $data) {
$queries[$name] = $this->sanitizeQueries($name, $data);
}

return $queries;
}

foreach ($this->loggers as $name => $logger) {
$queries[$name] = $this->sanitizeQueries($name, $logger->queries);
}

$this->data = [
'queries' => $queries,
'connections' => $this->connections,
'managers' => $this->managers,
];
return $queries;
}

public function reset()
{
$this->data = [];

if (null !== $this->debugDataHolder) {
$this->debugDataHolder->reset();

return;
}

foreach ($this->loggers as $logger) {
$logger->queries = [];
$logger->currentQuery = 0;
Expand Down
186 changes: 186 additions & 0 deletions src/Symfony/Bridge/Doctrine/Middleware/Debug/Connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Middleware\Debug;

use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
use Doctrine\DBAL\Driver\Middleware\AbstractConnectionMiddleware;
use Doctrine\DBAL\Driver\Result;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Symfony\Component\Stopwatch\Stopwatch;

/**
* @author Laurent VOULLEMIER <laurent.voullemier@gmail.com>
*
* @internal
*/
final class Connection extends AbstractConnectionMiddleware
{
private $nestingLevel = 0;
private $debugDataHolder;
private $stopwatch;
private $connectionName;

public function __construct(ConnectionInterface $connection, DebugDataHolder $debugDataHolder, ?Stopwatch $stopwatch, string $connectionName)
{
parent::__construct($connection);

$this->debugDataHolder = $debugDataHolder;
$this->stopwatch = $stopwatch;
$this->connectionName = $connectionName;
}

public function prepare(string $sql): DriverStatement
{
return new Statement(
parent::prepare($sql),
$this->debugDataHolder,
$this->connectionName,
$sql
);
}

public function query(string $sql): Result
{
$this->debugDataHolder->addQuery($this->connectionName, $query = new Query($sql));

if (null !== $this->stopwatch) {
$this->stopwatch->start('doctrine', 'doctrine');
}

$query->start();

try {
$result = parent::query($sql);
} finally {
$query->stop();

if (null !== $this->stopwatch) {
$this->stopwatch->stop('doctrine');
}
}

return $result;
}

public function exec(string $sql): int
{
$this->debugDataHolder->addQuery($this->connectionName, $query = new Query($sql));

if (null !== $this->stopwatch) {
$this->stopwatch->start('doctrine', 'doctrine');
}

$query->start();

try {
$affectedRows = parent::exec($sql);
} finally {
$query->stop();

if (null !== $this->stopwatch) {
$this->stopwatch->stop('doctrine');
}
}

return $affectedRows;
}

public function beginTransaction(): bool
{
$query = null;
if (1 === ++$this->nestingLevel) {
$this->debugDataHolder->addQuery($this->connectionName, $query = new Query('"START TRANSACTION"'));
Copy link
Member

Choose a reason for hiding this comment

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

why the double quotes ? They are not part of the SQL query starting a transaction.

Copy link
Member

Choose a reason for hiding this comment

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

same for COMMIT and ROLLBACK

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's the format that is currently used in the profiler: https://github.com/doctrine/dbal/blob/2afc6f00e8ff145ab386e9d25af293db98418bf5/src/Connection.php#L1297; should I change it anyway ?

}

if (null !== $this->stopwatch) {
$this->stopwatch->start('doctrine', 'doctrine');
}

if (null !== $query) {
$query->start();
}

try {
$ret = parent::beginTransaction();
} finally {
if (null !== $query) {
$query->stop();
}

if (null !== $this->stopwatch) {
$this->stopwatch->stop('doctrine');
}
}

return $ret;
}

public function commit(): bool
{
$query = null;
if (1 === $this->nestingLevel--) {
$this->debugDataHolder->addQuery($this->connectionName, $query = new Query('"COMMIT"'));
}

if (null !== $this->stopwatch) {
$this->stopwatch->start('doctrine', 'doctrine');
}

if (null !== $query) {
$query->start();
}

try {
$ret = parent::commit();
} finally {
if (null !== $query) {
$query->stop();
}

if (null !== $this->stopwatch) {
$this->stopwatch->stop('doctrine');
}
}

return $ret;
}

public function rollBack(): bool
{
$query = null;
if (1 === $this->nestingLevel--) {
$this->debugDataHolder->addQuery($this->connectionName, $query = new Query('"ROLLBACK"'));
}

if (null !== $this->stopwatch) {
$this->stopwatch->start('doctrine', 'doctrine');
}

if (null !== $query) {
$query->start();
}

try {
$ret = parent::rollBack();
} finally {
if (null !== $query) {
$query->stop();
}

if (null !== $this->stopwatch) {
$this->stopwatch->stop('doctrine');
}
}

return $ret;
}
}
48 changes: 48 additions & 0 deletions src/Symfony/Bridge/Doctrine/Middleware/Debug/DebugDataHolder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Middleware\Debug;

/**
* @author Laurent VOULLEMIER <laurent.voullemier@gmail.com>
*/
class DebugDataHolder
{
private $data = [];

public function addQuery(string $connectionName, Query $query): void
{
$this->data[$connectionName][] = [
'sql' => $query->getSql(),
'params' => $query->getParams(),
'types' => $query->getTypes(),
'executionMS' => [$query, 'getDuration'], // stop() may not be called at this point
];
}

public function getData(): array
{
foreach ($this->data as $connectionName => $dataForConn) {
foreach ($dataForConn as $idx => $data) {
if (\is_callable($data['executionMS'])) {
$this->data[$connectionName][$idx]['executionMS'] = $data['executionMS']();
}
}
}

return $this->data;
}

public function reset(): void
{
$this->data = [];
}
}
47 changes: 47 additions & 0 deletions src/Symfony/Bridge/Doctrine/Middleware/Debug/Driver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Middleware\Debug;

use Doctrine\DBAL\Driver as DriverInterface;
use Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware;
use Symfony\Component\Stopwatch\Stopwatch;

/**
* @author Laurent VOULLEMIER <laurent.voullemier@gmail.com>
*
* @internal
*/
final class Driver extends AbstractDriverMiddleware
{
private $debugDataHolder;
private $stopwatch;
private $connectionName;

public function __construct(DriverInterface $driver, DebugDataHolder $debugDataHolder, ?Stopwatch $stopwatch, string $connectionName)
{
parent::__construct($driver);

$this->debugDataHolder = $debugDataHolder;
$this->stopwatch = $stopwatch;
$this->connectionName = $connectionName;
}

public function connect(array $params): Connection
{
return new Connection(
parent::connect($params),
$this->debugDataHolder,
$this->stopwatch,
$this->connectionName
);
}
}
Loading