From 9520f5ebba065357151886ee35f12f32aab10d68 Mon Sep 17 00:00:00 2001
From: Louis-Proffit <77028679+Louis-Proffit@users.noreply.github.com>
Date: Sun, 6 Oct 2024 20:02:00 +0200
Subject: [PATCH 1/3] Add a "run query" option in the profiler
---
src/Controller/ProfilerController.php | 34 +++++++++++++++++++
templates/Collector/db.html.twig | 48 ++++++++++++++++++++++++---
templates/Collector/run.html.twig | 23 +++++++++++++
3 files changed, 101 insertions(+), 4 deletions(-)
create mode 100644 templates/Collector/run.html.twig
diff --git a/src/Controller/ProfilerController.php b/src/Controller/ProfilerController.php
index d85cda0a..8cf2ab45 100644
--- a/src/Controller/ProfilerController.php
+++ b/src/Controller/ProfilerController.php
@@ -83,6 +83,40 @@ public function explainAction($token, $connectionName, $query)
]));
}
+
+ /**
+ * Renders the profiler panel for the given token.
+ *
+ * @param string $token The profiler token
+ * @param string $connectionName
+ * @param int $query
+ *
+ * @return Response A Response instance
+ */
+ public function runAction($token, $connectionName, $query)
+ {
+ $this->profiler->disable();
+
+ $profile = $this->profiler->loadProfile($token);
+ $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];
+
+ $connection = $this->registry->getConnection($connectionName);
+
+ $result = $connection->executeQuery($query["sql"]);
+
+ return new Response($this->twig->render('@Doctrine/Collector/run.html.twig', ["result" => $result->fetchAllAssociative()]));
+ }
+
/**
* @param mixed[] $query
*
diff --git a/templates/Collector/db.html.twig b/templates/Collector/db.html.twig
index 90dbc189..103a3cad 100644
--- a/templates/Collector/db.html.twig
+++ b/templates/Collector/db.html.twig
@@ -90,6 +90,13 @@
connectionName: request.query.get('connection'),
query: request.query.get('query')
})) }}
+ {% elseif 'run' == page %}
+ {{ render(controller('Doctrine\\Bundle\\DoctrineBundle\\Controller\\ProfilerController::runAction', {
+ token: token,
+ panel: 'db',
+ connectionName: request.query.get('connection'),
+ query: request.query.get('query')
+ })) }}
{% else %}
{{ block('queries') }}
{% endif %}
@@ -100,8 +107,8 @@
.time-container { position: relative; }
.time-container .nowrap { position: relative; z-index: 1; text-shadow: 0 0 2px #fff; }
.time-bar { display: block; position: absolute; top: 0; left: 0; bottom: 0; background: #e0e0e0; }
- .sql-runnable.sf-toggle-content.sf-toggle-visible { display: flex; flex-direction: column; }
- .sql-runnable button { align-self: end; }
+ .sql-runnable.sf-toggle-content.sf-toggle-visible { display: flex; flex-direction: row; align-items: center}
+ .sql-runnable pre { flex-grow: 1}
{% if profiler_markup_version >= 3 %}
.highlight .keyword { color: var(--highlight-keyword); font-weight: bold; }
.highlight .word { color: var(--color-text); }
@@ -252,10 +259,13 @@
{% if query.runnable %}
-
+
{% set runnable_sql = (query.sql ~ ';')|doctrine_replace_query_parameters(query.params) %}
{{ runnable_sql|doctrine_prettify_sql }}
-
+
{% endif %}
@@ -263,6 +273,8 @@
{% endif %}
+
+
{% if query.backtrace is defined %}
@@ -468,6 +480,34 @@
return false;
}
+ function run(link) {
+ "use strict";
+
+ var targetId = link.getAttribute('data-target-id');
+ var targetElement = document.getElementById(targetId);
+
+ if (targetElement.style.display != 'block') {
+ if (targetElement.getAttribute('data-sfurl') !== link.href) {
+ fetch(link.href, {
+ headers: {'X-Requested-With': 'XMLHttpRequest'}
+ }).then(async function (response) {
+ targetElement.innerHTML = await response.text()
+ targetElement.setAttribute('data-sfurl', link.href)
+ }, function () {
+ targetElement.innerHTML = 'An error occurred while loading the query explanation.';
+ })
+ }
+
+ targetElement.style.display = 'block';
+ link.innerHTML = 'Hide query result';
+ } else {
+ targetElement.style.display = 'none';
+ link.innerHTML = 'Run query';
+ }
+
+ return false;
+ }
+
function sortTable(header, column, targetId) {
"use strict";
diff --git a/templates/Collector/run.html.twig b/templates/Collector/run.html.twig
new file mode 100644
index 00000000..34cebc5d
--- /dev/null
+++ b/templates/Collector/run.html.twig
@@ -0,0 +1,23 @@
+{% if result is not empty %}
+
+
+
+ {% for key, value in result[0] %}
+ {{ key }} |
+ {% endfor %}
+
+
+
+
+ {% for row in result %}
+
+ {% for key, value in row %}
+ {{ value }} |
+ {% endfor %}
+
+ {% endfor %}
+
+
+{% else %}
+ Result is empty
+{% endif %}
From 6c6c67070b242dec7864567c91c912c657dc3a75 Mon Sep 17 00:00:00 2001
From: Louis-Proffit <77028679+Louis-Proffit@users.noreply.github.com>
Date: Sun, 6 Oct 2024 20:07:21 +0200
Subject: [PATCH 2/3] Adapt code style
---
src/Controller/ProfilerController.php | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/src/Controller/ProfilerController.php b/src/Controller/ProfilerController.php
index 8cf2ab45..b9cba948 100644
--- a/src/Controller/ProfilerController.php
+++ b/src/Controller/ProfilerController.php
@@ -87,9 +87,9 @@ public function explainAction($token, $connectionName, $query)
/**
* Renders the profiler panel for the given token.
*
- * @param string $token The profiler token
+ * @param string $token The profiler token
* @param string $connectionName
- * @param int $query
+ * @param int $query
*
* @return Response A Response instance
*/
@@ -97,14 +97,14 @@ public function runAction($token, $connectionName, $query)
{
$this->profiler->disable();
- $profile = $this->profiler->loadProfile($token);
+ $profile = $this->profiler->loadProfile($token);
$collector = $profile->getCollector('db');
assert($collector instanceof DoctrineDataCollector);
$queries = $collector->getQueries();
- if (!isset($queries[$connectionName][$query])) {
+ if (! isset($queries[$connectionName][$query])) {
return new Response('This query does not exist.');
}
@@ -112,9 +112,9 @@ public function runAction($token, $connectionName, $query)
$connection = $this->registry->getConnection($connectionName);
- $result = $connection->executeQuery($query["sql"]);
+ $result = $connection->executeQuery($query['sql']);
- return new Response($this->twig->render('@Doctrine/Collector/run.html.twig', ["result" => $result->fetchAllAssociative()]));
+ return new Response($this->twig->render('@Doctrine/Collector/run.html.twig', ['result' => $result->fetchAllAssociative()]));
}
/**
From 4abab498ba4a1a14774a32c21e171a14b3dcd9a1 Mon Sep 17 00:00:00 2001
From: Louis-Proffit <77028679+Louis-Proffit@users.noreply.github.com>
Date: Sun, 6 Oct 2024 20:08:32 +0200
Subject: [PATCH 3/3] Adapt code style
---
src/Controller/ProfilerController.php | 1 -
1 file changed, 1 deletion(-)
diff --git a/src/Controller/ProfilerController.php b/src/Controller/ProfilerController.php
index b9cba948..bdbc8593 100644
--- a/src/Controller/ProfilerController.php
+++ b/src/Controller/ProfilerController.php
@@ -83,7 +83,6 @@ public function explainAction($token, $connectionName, $query)
]));
}
-
/**
* Renders the profiler panel for the given token.
*