Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zabaala/8b512f199c5b7d071eb85e1cac27d110 to your computer and use it in GitHub Desktop.
Save zabaala/8b512f199c5b7d071eb85e1cac27d110 to your computer and use it in GitHub Desktop.
Debug all Symfony Queries
<?php
namespace Doctrine\DBAL\Logging;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\HttpKernel\KernelInterface;
use function microtime;
use function exec;
/**
* Includes executed SQLs in a Debug Stack.
*/
class DebugStack implements SQLLogger
{
/**
* Executed SQL queries.
*
* @var mixed[][]
*/
public $queries = [];
/** @var float|null */
public $start = null;
/** @var int */
public $currentQuery = 0;
/**
* {@inheritdoc}
*/
public function startQuery($sql, ?array $params = null, ?array $types = null)
{
/**
* To start debug, comment return below...
*/
return;
/**
* Set full path to log file.
* Full path definition improve better performance to loggin.
* At least 8 times faster.
*/
$rootDir = '/Users/mauriciorodrigues/_dev/';
$file = "{$rootDir}/dump.log";
$this->start = microtime(true);
$this->queries[++$this->currentQuery] = ['sql' => $sql, 'params' => $params, 'types' => $types, 'executionMS' => 0];
$content = sprintf('{"sql": %s, "params":[%s]}',
($sql ?? null),
is_array($params) ? join(',', $params) : $params
);
file_put_contents($file, $content . "\n", FILE_APPEND);
}
public function stopQuery()
{
// TODO: Implement stopQuery() method.
}
}
<?php
namespace Doctrine\DBAL\Logging;
use Doctrine\Common\Collections\ArrayCollection;
use function microtime;
/**
* Includes executed SQLs in a Debug Stack.
*/
class DebugStack implements SQLLogger
{
/**
* Executed SQL queries.
*
* @var mixed[][]
*/
public $queries = [];
public $distinctedQueries = [];
/**
* If Debug Stack is enabled (log queries) or not.
*
* @var bool
*/
public $enabled = true;
/** @var float|null */
public $start = null;
/** @var int */
public $currentQuery = 0;
/**
* {@inheritdoc}
*/
public function startQuery($sql, ?array $params = null, ?array $types = null)
{
if (! $this->enabled) {
return;
}
$this->start = microtime(true);
$this->queries[++$this->currentQuery] = ['sql' => $sql, 'params' => $params, 'types' => $types, 'executionMS' => 0];
if (isset($params[0])) {
$this->distinctedQueries[$sql]['params'][] = $params[0];
} else {
$this->distinctedQueries[$sql]['params'] = null;
}
}
/**
* {@inheritdoc}
*/
public function stopQuery()
{
if (! $this->enabled) {
return;
}
$this->queries[$this->currentQuery]['executionMS'] = microtime(true) - $this->start;
}
public function __destruct()
{
$queries = [];
foreach ($this->distinctedQueries as $query => $params) {
if (is_array($params["params"])) {
$query = str_replace(
'= ?',
'in (' . implode(', ', $params["params"]) . ');',
$query
);
}
$queries[] = $query;
}
dump($queries);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment