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 %} -