-
-
Notifications
You must be signed in to change notification settings - Fork 481
Expand file tree
/
Copy pathProfilerController.php
More file actions
132 lines (110 loc) · 3.98 KB
/
ProfilerController.php
File metadata and controls
132 lines (110 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
declare(strict_types=1);
namespace Doctrine\Bundle\DoctrineBundle\Controller;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Platforms\SQLitePlatform;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\Persistence\ConnectionRegistry;
use Exception;
use Symfony\Bridge\Doctrine\DataCollector\DoctrineDataCollector;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use Symfony\Component\VarDumper\Cloner\Data;
use Throwable;
use Twig\Environment;
use function assert;
/** @internal */
class ProfilerController
{
public function __construct(
private readonly Environment $twig,
private readonly ConnectionRegistry $registry,
private readonly Profiler $profiler,
) {
}
/**
* Renders the profiler panel for the given token.
*
* @param string $token The profiler token
*
* @return Response A Response instance
*/
public function explainAction(string $token, string $connectionName, int $query): Response
{
$this->profiler->disable();
$profile = $this->profiler->loadProfile($token);
if ($profile === null) {
return new Response('Profile not found.', 404);
}
$collector = $profile->getCollector('db');
assert($collector instanceof DoctrineDataCollector);
$queries = $collector->getQueries();
if (! isset($queries[$connectionName][$query])) {
return new Response('This query does not exist.');
}
$query = $queries[$connectionName][$query];
if (! $query['explainable']) {
return new Response('This query cannot be explained.');
}
$connection = $this->registry->getConnection($connectionName);
assert($connection instanceof Connection);
try {
$platform = $connection->getDatabasePlatform();
if ($platform instanceof SQLitePlatform) {
$results = $this->explainSQLitePlatform($connection, $query);
} elseif ($platform instanceof SQLServerPlatform) {
throw new Exception('Explain for SQLServerPlatform is currently not supported. Contributions are welcome.');
} elseif ($platform instanceof OraclePlatform) {
$results = $this->explainOraclePlatform($connection, $query);
} else {
$results = $this->explainOtherPlatform($connection, $query);
}
} catch (Throwable) {
return new Response('This query cannot be explained.');
}
return new Response($this->twig->render('@Doctrine/Collector/explain.html.twig', [
'data' => $results,
'query' => $query,
]));
}
/**
* @param mixed[] $query
*
* @return mixed[]
*/
private function explainSQLitePlatform(Connection $connection, array $query): array
{
$params = $query['params'];
if ($params instanceof Data) {
$params = $params->getValue(true);
}
return $connection->executeQuery('EXPLAIN QUERY PLAN ' . $query['sql'], $params, $query['types'])
->fetchAllAssociative();
}
/**
* @param mixed[] $query
*
* @return mixed[]
*/
private function explainOtherPlatform(Connection $connection, array $query): array
{
$params = $query['params'];
if ($params instanceof Data) {
$params = $params->getValue(true);
}
return $connection->executeQuery('EXPLAIN ' . $query['sql'], $params, $query['types'])
->fetchAllAssociative();
}
/**
* @param mixed[] $query
*
* @return mixed[]
*/
private function explainOraclePlatform(Connection $connection, array $query): array
{
$connection->executeQuery('EXPLAIN PLAN FOR ' . $query['sql']);
return $connection->executeQuery('SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY())')
->fetchAllAssociative();
}
}