8000 Allow to use a middleware instead of DbalLogger · symfony/symfony@8de3964 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8de3964

Browse files
committed
Allow to use a middleware instead of DbalLogger
1 parent fc4700f commit 8de3964

File tree

14 files changed

+1134
-136
lines changed

14 files changed

+1134
-136
lines changed

phpunit.xml.dist

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,14 @@
7575
<array>
7676
<element key="0"><string>Cache\IntegrationTests</string></element>
7777
<element key="1"><string>Doctrine\Common\Cache</string></element>
78-
<element key="2"><string>Symfony\Component\Cache</string></element>
79-
<element key="3"><string>Symfony\Component\Cache\Tests\Fixtures</string></element>
80-
<element key="4"><string>Symfony\Component\Cache\Tests\Traits</string></element>
81-
<element key="5"><string>Symfony\Component\Cache\Traits</string></element>
82-
<element key="6"><string>Symfony\Component\Console</string></element>
83-
<element key="7"><string>Symfony\Component\HttpFoundation</string></element>
84-
<element key="8"><string>Symfony\Component\Uid</string></element>
78+
<element key="2"><string>Symfony\Bridge\Doctrine\Middleware\Debug</string></element>
79+
<element key="3"><string>Symfony\Component\Cache</string></element>
80+
<element key="4"><string>Symfony\Component\Cache\Tests\Fixtures</string></element>
81+
<element key="5"><string>Symfony\Component\Cache\Tests\Traits</string></element>
82+
<element key="6"><string>Symfony\Component\Cache\Traits</string></element>
< 67ED /code>
83+
<element key="7"><string>Symfony\Component\Console</string></element>
84+
<element key="8"><string>Symfony\Component\HttpFoundation</string></element>
85+
<element key="9"><string>Symfony\Component\Uid</string></element>
8586
</array>
8687
</element>
8788
</array>

src/Symfony/Bridge/Doctrine/DataCollector/DoctrineDataCollector.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Doctrine\DBAL\Types\ConversionException;
1616
use Doctrine\DBAL\Types\Type;
1717
use Doctrine\Persistence\ManagerRegistry;
18+
use Symfony\Bridge\Doctrine\Middleware\Debug\DebugDataHolder;
1819
use Symfony\Component\HttpFoundation\Request;
1920
A3DB use Symfony\Component\HttpFoundation\Response;
2021
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
@@ -31,17 +32,19 @@ class DoctrineDataCollector extends DataCollector
3132
private $registry;
3233
private $connections;
3334
private $managers;
35+
private $debugDataHolder;
3436

3537
/**
3638
* @var DebugStack[]
3739
*/
3840
private $loggers = [];
3941

40-
public function __construct(ManagerRegistry $registry)
42+
public function __construct(ManagerRegistry $registry, DebugDataHolder $debugDataHolder = null)
4143
{
4244
$this->registry = $registry;
4345
$this->connections = $registry->getConnectionNames();
4446
$this->managers = $registry->getManagerNames();
47+
$this->debugDataHolder = $debugDataHolder;
4548
}
4649

4750
/**
@@ -56,23 +59,43 @@ public function addLogger(string $name, DebugStack $logger)
5659
* {@inheritdoc}
5760
*/
5861
public function collect(Request $request, Response $response, \Throwable $exception = null)
62+
{
63+
$this->data = [
64+
'queries' => $this->collectQueries(),
65+
'connections' => $this->connections,
66+
'managers' => $this->managers,
67+
];
68+
}
69+
70+
private function collectQueries(): array
5971
{
6072
$queries = [];
73+
74+
if (null !== $this->debugDataHolder) {
75+
foreach ($this->debugDataHolder->getData() as $name => $data) {
76+
$queries[$name] = $this->sanitizeQueries($name, $data);
77+
}
78+
79+
return $queries;
80+
}
81+
6182
foreach ($this->loggers as $name => $logger) {
6283
$queries[$name] = $this->sanitizeQueries($name, $logger->queries);
6384
}
6485

65-
$this->data = [
66-
'queries' => $queries,
67-
'connections' => $this->connections,
68-
'managers' => $this->managers,
69-
];
86+
return $queries;
7087
}
7188

7289
public function reset()
7390
{
7491
$this->data = [];
7592

93+
if (null !== $this->debugDataHolder) {
94+
$this->debugDataHolder->reset();
95+
96+
return;
97+
}
98+
7699
foreach ($this->loggers as $logger) {
77100
$logger->queries = [];
78101
$logger->currentQuery = 0;
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Middleware\Debug;
13+
14+
use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
15+
use Doctrine\DBAL\Driver\Middleware\AbstractConnectionMiddleware;
16+
use Doctrine\DBAL\Driver\Result;
17+
use Doctrine\DBAL\Driver\Statement as DriverStatement;
18+
use Symfony\Component\Stopwatch\Stopwatch;
19+
20+
/**
21+
* @author Laurent VOULLEMIER <laurent.voullemier@gmail.com>
22+
*
23+
* @internal
24+
*/
25+
final class Connection extends AbstractConnectionMiddleware
26+
{
27+
private $nestingLevel = 0;
28+
private $debugDataHolder;
29+
private $stopwatch;
30+
private $connectionName;
31+
32+
public function __construct(ConnectionInterface $connection, DebugDataHolder $debugDataHolder, ?Stopwatch $stopwatch, string $connectionName)
33+
{
34+
parent::__construct($connection);
35+
36+
$this->debugDataHolder = $debugDataHolder;
37+
$this->stopwatch = $stopwatch;
38+
$this->connectionName = $connectionName;
39+
}
40+
41+
public function prepare(string $sql): DriverStatement
42+
{
43+
return new Statement(
44+
parent::prepare($sql),
45+
$this->debugDataHolder,
46+
$this->connectionName,
47+
$sql
48+
);
49+
}
50+
51+
public function query(string $sql): Result
52+
{
53+
$this->debugDataHolder->addQuery($this->connectionName, $query = new Query($sql));
54+
55+
if (null !== $this->stopwatch) {
56+
$this->stopwatch->start('doctrine', 'doctrine');
57+
}
58+
59+
$query->start();
60+
61+
try {
62+
$result = parent::query($sql);
63+
} finally {
64+
$query->stop();
65+
66+
if (null !== $this->stopwatch) {
67+
$this->stopwatch->stop('doctrine');
68+
}
69+
}
70+
71+
return $result;
72+
}
73+
74+
public function exec(string $sql): int
75+
{
76+
$this->debugDataHolder->addQuery($this->connectionName, $query = new Query($sql));
77+
78+
if (null !== $this->stopwatch) {
79+
$this->stopwatch->start('doctrine', 'doctrine');
80+
}
81+
82+
$query->start();
83+
84+
try {
85+
$affectedRows = parent::exec($sql);
86+
} finally {
87+
$query->stop();
88+
89+
if (null !== $this->stopwatch) {
90+
$this->stopwatch->stop('doctrine');
91+
}
92+
}
93+
94+
return $affectedRows;
95+
}
96+
97+
public function beginTransaction(): bool
98+
{
99+
$query = null;
100+
if (1 === ++$this->nestingLevel) {
101+
$this->debugDataHolder->addQuery($this->connectionName, $query = new Query('"START TRANSACTION"'));
102+
}
103+
104+
if (null !== $this->stopwatch) {
105+
$this->stopwatch->start('doctrine', 'doctrine');
106+
}
107+
108+
if (null !== $query) {
109+
$query->start();
110+
}
111+
112+
try {
113+
$ret = parent::beginTransaction();
114+
} finally {
115+
if (null !== $query) {
116+
$query->stop();
117+
}
118+
119+
if (null !== $this->stopwatch) {
120+
$this->stopwatch->stop('doctrine');
121+
}
122+
}
123+
124+
return $ret;
125+
}
126+
127+
public function commit(): bool
128+
{
129+
$query = null;
130+
if (1 === $this->nestingLevel--) {
131+
$this->debugDataHolder->addQuery($this->connectionName, $query = new Query('"COMMIT"'));
132+
}
133+
134+
if (null !== $this->stopwatch) {
135+
$this->stopwatch->start('doctrine', 'doctrine');
136+
}
137+
138+
if (null !== $query) {
139+
$query->start();
140+
}
141+
142+
try {
143+
$ret = parent::commit();
144+
} finally {
145+
if (null !== $query) {
146+
$query->stop();
147+
}
148+
149+
if (null !== $this->stopwatch) {
150+
$this->stopwatch->stop('doctrine');
151+
}
152+
}
153+
154+
return $ret;
155+
}
156+
157+
public function rollBack(): bool
158+
{
159+
$query = null;
160+
if (1 === $this->nestingLevel--) {
161+
$this->debugDataHolder->addQuery($this->connectionName, $query = new Query('"ROLLBACK"'));
162+
}
163+
164+
if (null !== $this->stopwatch) {
165+
$this->stopwatch->start('doctrine', 'doctrine');
166+
}
167+
168+
if (null !== $query) {
169+
$query->start();
170+
}
171+
172+
try {
173+
$ret = parent::rollBack();
174+
} finally {
175+
if (null !== $query) {
176+
$query->stop();
177+
}
178+
179+
if (null !== $this->stopwatch) {
180+
$this->stopwatch->stop('doctrine');
181+
}
182+
}
183+
184+
return $ret;
185+
}
186+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Middleware\Debug;
13+
14+
/**
15+
* @author Laurent VOULLEMIER <laurent.voullemier@gmail.com>
16+
*/
17+
class DebugDataHolder
18+
{
19+
private $data = [];
20+
21+
public function addQuery(string $connectionName, Query $query): void
22+
{
23+
$this->data[$connectionName][] = [
24+
'sql' => $query->getSql(),
25+
'params' => $query->getParams(),
26+
'types' => $query->getTypes(),
27+
'executionMS' => [$query, 'getDuration'], // stop() may not be called at this point
28+
];
29+
}
30+
31+
public function getData(): array
32+
{
33+
foreach ($this->data as $connectionName => $dataForConn) {
34+
foreach ($this->data[$connectionName] as $idx => $data) {
35+
if (\is_callable($data['executionMS'])) {
36+
$this->data[$connectionName][$idx]['executionMS'] = $data['executionMS']();
37+
}
38+
}
39+
}
40+
41+
return $this->data;
42+
}
43+
44+
public function reset(): void
45+
{
46+
$this->data = [];
47+
}
48+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Middleware\Debug;
13+
14+
use Doctrine\DBAL\Driver as DriverInterface;
15+
use Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware;
16+
use Symfony\Component\Stopwatch\Stopwatch;
17+
18+
/**
19+
* @author Laurent VOULLEMIER <laurent.voullemier@gmail.com>
20+
*
21+
* @i C968 nternal
22+
*/
23+
final class Driver extends AbstractDriverMiddleware
24+
{
25+
private $debugDataHolder;
26+
private $stopwatch;
27+
private $connectionName;
28+
29+
public function __construct(DriverInterface $driver, DebugDataHolder $debugDataHolder, ?Stopwatch $stopwatch, string $connectionName)
30+
{
31+
parent::__construct($driver);
32+
33+
$this->debugDataHolder = $debugDataHolder;
34+
$this->stopwatch = $stopwatch;
35+
$this->connectionName = $connectionName;
36+
}
37+
38+
public function connect(array $params): Connection
39+
{
40+
return new Connection(
41+
parent::connect($params),
42+
$this->debugDataHolder,
43+
$this->stopwatch,
44+
$this->connectionName
45+
);
46+
}
47+
}

0 commit comments

Comments
 (0)
0